Пример #1
0
int
DBStock::newTable (PluginData *pd)
{
  if (! init())
    return 0;

  if (! pd->bars)
    return 0;
  
  Bars *symbol = pd->bars;

  if (symbol->table().isEmpty())
  {
    qDebug() << "DBStock::newTable: invalid table";
    return 0;
  }
  
  QSqlQuery q(_db);

  // create new table
  QString s = "CREATE TABLE IF NOT EXISTS " + symbol->table() + " (";
  s.append("date INT PRIMARY KEY UNIQUE");
  s.append(", open REAL");
  s.append(", high REAL");
  s.append(", low REAL");
  s.append(", close REAL");
  s.append(", volume INT");
  s.append(")");
  q.exec(s);
  if (q.lastError().isValid())
  {
    qDebug() << "DBStock::newTable:" + q.lastError().text();
    return 0;
  }

  return 1;
}
Пример #2
0
int
DBStock::setBars (PluginData *pd)
{
  if (! init())
    return 0;

  if (! pd->bars)
    return 0;
  
  Bars *symbol = pd->bars;

  QSqlQuery q(_db);

  QList<int> keys = symbol->keys();

  BarType bt;
  int loop = 0;
  for (; loop < keys.size(); loop++)
  {
    CBar *bar = symbol->bar(keys.at(loop));

    QDateTime dt = bar->date();
    
    QString date = dt.toString("yyyyMMddHHmmss");

    // first check if record exists so we know to do an update or insert
    QString s = "SELECT date FROM " + symbol->table() + " WHERE date =" + date;
    q.exec(s);
    if (q.lastError().isValid())
    {
      qDebug() << "DBStock::setBars:" << q.lastError().text();
      qDebug() << s;
      continue;
    }

    if (q.next()) // record exists, use update
    {
      s = "UPDATE " + symbol->table() + " SET ";

      QStringList tl;
      double v = 0;
      if (bar->get(bt.indexToString(BarType::_OPEN), v))
        tl << "open=" + QString::number(v);
      
      v = 0;
      if (bar->get(bt.indexToString(BarType::_HIGH), v))
        tl << "high=" + QString::number(v);
      
      v = 0;
      if (bar->get(bt.indexToString(BarType::_LOW), v))
        tl << "low=" + QString::number(v);
      
      v = 0;
      if (bar->get(bt.indexToString(BarType::_CLOSE), v))
        tl << "close=" + QString::number(v);
      
      v = 0;
      if (bar->get(bt.indexToString(BarType::_VOLUME), v))
        tl << "volume=" + QString::number(v);
      
      s.append(tl.join(","));
      s.append(" WHERE date=" + date);
    }
    else // new record, use insert
    {
      QStringList tl;
      s = "INSERT INTO " + symbol->table() + " (date,open,high,low,close,volume) VALUES(";

      tl << date;
      
      double v = 0;
      bar->get(bt.indexToString(BarType::_OPEN), v);
      tl << QString::number(v);
      
      v = 0;
      bar->get(bt.indexToString(BarType::_HIGH), v);
      tl << QString::number(v);

      v = 0;
      bar->get(bt.indexToString(BarType::_LOW), v);
      tl << QString::number(v);

      v = 0;
      bar->get(bt.indexToString(BarType::_CLOSE), v);
      tl << QString::number(v);

      v = 0;
      bar->get(bt.indexToString(BarType::_VOLUME), v);
      tl << QString::number(v);

      s.append(tl.join(","));
      s.append(")");
    }

    q.exec(s);
    if (q.lastError().isValid())
    {
      qDebug() << "DBStock::setBars:" << q.lastError().text();
      qDebug() << s;
      continue;
    }
  }
  
  return 1;
}
Пример #3
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;
}