void NetfondsHistoryDownload::parseHistory(QByteArray &ba, QString &symbol, QString &name)
{
  //quote_date,paper,exch,open,high,low,close,volume,value
  //20121101,OMXS30,Stockholm,1050.03,1064.08,1047.28,1062.51,0,0
  QString ts(ba);
  QStringList ll = ts.split('\n');

  Bars sym;
  sym.setExchange(QString("NETFONDS"));
  sym.setTicker(symbol);
  sym.setName(name);
  sym.setSymbolType(QString("Stock"));

  BarType bt;
  int line = 0;
  int loop = 1; // skip past first line
  for (; loop < ll.size()-2; loop++)
  {
    line++;
    ts = ll[loop].remove('"');
    ts = ts.trimmed();

    if (! ts.length()){

        QStringList mess;
        mess << symbol << tr("Error!!");
        emit signalMessage(mess.join(" "));
        return;
    }

    QStringList l = ts.split(",");
    if (l.size() != 9)
    {
      QStringList mess;
      mess << symbol << ":" << tr("invalid # of fields, record skipped");
      emit signalMessage(mess.join(" "));
      return;
    }

    CBar *bar = new CBar;

    QDateTime dt = QDateTime::fromString(l.at(0).trimmed(), "yyyyMMdd");
    if (! dt.isValid())
    {
      delete bar;
      QStringList mess;
      mess << symbol << ":" << tr("invalid date") << l.at(0);
      emit signalMessage(mess.join(" "));
      return;
    }
    else
      bar->setDate(dt);

    // verify open
    int pos = 3;
    bool ok;
    double t = l.at(pos).trimmed().toDouble(&ok);
    if (! ok)
    {
      delete bar;
      QStringList mess;
      mess << tr("Error, invalid open") << l.at(pos);
      emit signalMessage(mess.join(" "));
      return;
    }
    else
      bar->set(bt.indexToString(BarType::_OPEN), t);

    // verify high
    pos = 4;
    t = l.at(pos).trimmed().toDouble(&ok);
    if (! ok)
    {
      delete bar;
      QStringList mess;
      mess << tr("Error, invalid high") << l.at(pos);
      emit signalMessage(mess.join(" "));
      return;
    }
    else
      bar->set(bt.indexToString(BarType::_HIGH), t);

    // verify low
    pos = 5;
    t = l.at(pos).trimmed().toDouble(&ok);
    if (! ok)
    {
      delete bar;
      QStringList mess;
      mess << tr("Error, invalid low") << l.at(pos);
      emit signalMessage(mess.join(" "));
      return;
    }
    else
      bar->set(bt.indexToString(BarType::_LOW), t);

    // verify close
    pos = 6;
    t = l.at(pos).trimmed().toDouble(&ok);
    if (! ok)
    {
      delete bar;
      QStringList mess;
      mess << tr("Error, invalid close") << l.at(pos);
      emit signalMessage(mess.join(" "));
      return;
    }
    else
      bar->set(bt.indexToString(BarType::_CLOSE), t);

    // verify volume
    pos = 7;
    t = l.at(pos).trimmed().toDouble(&ok);
    if (! ok)
    {
      delete bar;
      QStringList mess;
      mess << tr("Error, invalid volume") << l.at(pos);
      emit signalMessage(mess.join(" "));
      return;
    }
    else
      bar->set(bt.indexToString(BarType::_VOLUME), t);

    sym.setBar(sym.bars(), bar);
  }

  IDBPlugin *plug = dynamic_cast<IDBPlugin*>(((PluginFactory*)PluginFactory::getPluginFactory())->loadPlugin(QString("Database")));
  if (! plug)
  {
    QStringList mess;
    mess << tr("Error, Database plugin missing");
    emit signalMessage(mess.join(" "));
    return;
  }

  if (!plug->init())
    return;

  if (!plug->setBars(&sym))
    return;
}
Ejemplo n.º 2
0
int
DBStock::getBars (PluginData *pd)
{
  if (! init())
    return 0;
  
  if (! pd->bars)
    return 0;
  
  Bars *bd = pd->bars;
  
  // get last date in db
  QDateTime endDate = getMaxDate(bd);
  if (! endDate.isValid())
    return 0;
  
  DateRange dr;
  QDateTime startDate = dr.interval(endDate, bd->range());
  if (! startDate.isValid())
  {
    qDebug() << "DBStock::getBars: invalid range";
    return 0;
  }

  QSqlQuery q(_db);
  QString s = "SELECT date,open,high,low,close,volume";
  s.append(" FROM " + bd->table());
  s.append(" WHERE date >=" + startDate.toString("yyyyMMddHHmmss"));
  s.append(" AND date <=" + endDate.toString("yyyyMMddHHmmss"));
  s.append(" ORDER BY date ASC");
  q.exec(s);
  if (q.lastError().isValid())
  {
    qDebug() << "DBStock::getBars:" + q.lastError().text();
    qDebug() << s;
    return 0;
  }

  BarType bt;
  BarLength bl;
  QDateTime isDate, ieDate;
  CBar *bar = 0;
  while (q.next())
  {
    QDateTime lastDate = QDateTime::fromString(q.value(0).toString(), "yyyyMMddHHmmss");
    
    // is date greater than current bar range?
    if (lastDate >= ieDate || ! bar)
    {
      // save old bar
      if (bar)
        bd->setBar(bd->bars(), bar);
      
      // create new bar
      bl.interval(lastDate, bd->length(), isDate, ieDate);
      
      bar = new CBar;
      bar->setDate(lastDate);
      bar->set(bt.indexToString(BarType::_OPEN), q.value(1).toDouble());
      bar->set(bt.indexToString(BarType::_HIGH), q.value(2).toDouble());
      bar->set(bt.indexToString(BarType::_LOW), q.value(3).toDouble());
      bar->set(bt.indexToString(BarType::_CLOSE), q.value(4).toDouble());
      bar->set(bt.indexToString(BarType::_VOLUME), q.value(5).toDouble());
    }
    else
    {
      double v = q.value(2).toDouble();
      double v2;
      bar->get(bt.indexToString(BarType::_HIGH), v2);
      if (v > v2)
        bar->set(bt.indexToString(BarType::_HIGH), v);

      v = q.value(3).toDouble();
      bar->get(bt.indexToString(BarType::_LOW), v2);
      if (v < v2)
        bar->set(bt.indexToString(BarType::_LOW), v);

      bar->set(bt.indexToString(BarType::_CLOSE), q.value(4).toDouble());
      
      v = q.value(5).toDouble();
      bar->get(bt.indexToString(BarType::_VOLUME), v2);
      v += v2;
      bar->set(bt.indexToString(BarType::_VOLUME), v);
    }
  }

  // save any left over bar
  if (bar)
    bd->setBar(bd->bars(), bar);
  
  return 1;
}