void DbPlugin::loadIndexData (QString &symbol, QDict<Bar> &lookup, QDateTime &startDate, float weight,
                           int barRange, BarData::BarLength barLength)
{
  QFileInfo fi(symbol);
  QString fn = fi.fileName();

  DbPlugin db;
  if (db.open(symbol, chartIndex))
  {
    qDebug("Index::getIndexHistory: cannot open symbol chart");
    return;
  }

  BarData *bar = new BarData(symbol);
  bar->setBarLength(barLength);
  db.setBarRange(barRange);
  db.getHistory(bar, startDate);
  db.close();
  
  int loop;
  for (loop = 0; loop < (int) bar->count(); loop++)
  {
    QDateTime dt;
    bar->getDate(loop, dt);
    QString s = dt.toString("yyyyMMddhhmmss");
    Bar *r = lookup.find(s);
    if (! r)
    {
      r = new Bar;
      r->setDate(dt);
      r->setOpen(bar->getOpen(loop) * weight);
      r->setHigh(bar->getHigh(loop) * weight);
      r->setLow(bar->getLow(loop) * weight);
      r->setClose(bar->getClose(loop) * weight);
      r->setOI(1);
      r->getDateTimeString(FALSE, s);
      lookup.insert(s, r);
    }
    else
    {
      r->setOpen(r->getOpen() + (bar->getOpen(loop) * weight));
      r->setHigh(r->getHigh() + (bar->getHigh(loop) * weight));
      r->setLow(r->getLow() + (bar->getLow(loop) * weight));
      r->setClose(r->getClose() + (bar->getClose(loop) * weight));
      r->setOI((int) r->getOI() + 1);
    }
  }

  delete bar;
}
void SpreadDialog::newSpread ()
{
  bool ok = FALSE;
  QString spread = QInputDialog::getText(QObject::tr("New Spread"),
                                         QObject::tr("Enter symbol name for the new Spread"),
                                         QLineEdit::Normal,
                                         QString::null,
                                         &ok,
                                         0);
  if (! spread.length() || ok == FALSE)
    return;

  QDir dir;
  Config config;
  QString s;
  config.getData(Config::DataPath, s);
  s.append("/Spread");
  if (! dir.exists(s))
  {
    if (! dir.mkdir(s, TRUE))
    {
      QMessageBox::information(0,
                               QObject::tr("Qtstalker: Error"),
                               QObject::tr("Could not create Spread directory."));
      return;
    }
  }
  
  s.append("/" + spread);
  if (dir.exists(s))
  {
    QMessageBox::information(0,
                             QObject::tr("Qtstalker: Error"),
                             QObject::tr("This Spread already exists."));
    return;
  }

  DbPlugin db;
  if (db.openChart(s))
  {
    QMessageBox::information(0,
                             QObject::tr("Qtstalker: Disk Error"),
                             QObject::tr("Cannot create chart."));
    db.close();
    return;
  }

  db.setHeaderField(DbPlugin::Symbol, spread);
  s = "Spread";
  db.setHeaderField(DbPlugin::Type, s);  
  db.setHeaderField(DbPlugin::Plugin, s);  
  db.setHeaderField(DbPlugin::Title, spread);
  db.close();
}
void DbPlugin::getSpreadHistory (BarData *barData, QDateTime &startDate)
{
  QString s = "FirstSymbol";
  QString fs;
  getData(s, fs);

  QString ss;
  s = "SecondSymbol";
  getData(s, ss);

  // get the first symbol bars
  QFileInfo fi(fs);
  QString fn = fi.fileName();

  DbPlugin db;
  if (db.open(fs, chartIndex))
  {
    qDebug("Spread::getSpreadHistory: cannot open first symbol chart");
    return;
  }

  BarData *bar = new BarData(fs);
  bar->setBarLength(barLength);
  db.setBarRange(barRange);
  db.getHistory(bar, startDate);
  db.close();

  // get the second symbol bars
  QFileInfo fi2(ss);
  fn = fi2.fileName();

  if (db.open(ss, chartIndex))
  {
    qDebug("Spread::getSpreadHistory: cannot open second symbol chart");
    delete bar;
    return;
  }

  BarData *bar2 = new BarData(ss);
  bar2->setBarLength(barLength);
  db.setBarRange(barRange);
  db.getHistory(bar2, startDate);
  db.close();

  // create lookup dict for first symbol bars
  QDict<Bar> lookup;
  lookup.setAutoDelete(TRUE);
  int loop;
  for (loop = 0; loop < bar->count(); loop++)
  {
    Bar *r = new Bar;
    QDateTime dt;
    bar->getDate(loop, dt);
    r->setDate(dt);
    r->setClose(bar->getClose(loop));
    r->getDateTimeString(FALSE, s);
    lookup.insert(s, r);
  }

  // match all second symbol bars
  for (loop = bar2->count() - 1; loop > -1; loop--)
  {
    Bar r;
    QDateTime dt;
    bar2->getDate(loop, dt);
    s = dt.toString("yyyyMMddhhmmss");
    Bar *tr = lookup.find(s);
    if (tr)
    {
      double t = tr->getClose() - bar2->getClose(loop);
      r.setDate(dt);
      r.setOpen(t);
      r.setHigh(t);
      r.setLow(t);
      r.setClose(t);
      barData->prepend(r);
    }
  }

  delete bar;
  delete bar2;
}
void DbPlugin::getCCHistory (BarData *barData, QDateTime &startDate)
{
  FuturesData fd;
  if (fd.setSymbol(indexKey))
  {
    qDebug("CC::getCCHistory: invalid futures symbol");
    return;
  }
    
  Config config;
  QString s;
  QString baseDir;
  config.getData(Config::DataPath, baseDir);
  baseDir.append("/Futures/");
  fd.getExchange(s);
  baseDir.append(s + "/");
  fd.getSymbol(s);
  baseDir.append(s);
  QDir dir(baseDir);
  if (! dir.exists(baseDir, TRUE))
    return;
  QStringList dirList = dir.entryList();

  QString lastChart;
  fd.getCurrentContract(startDate, lastChart);
  QString ey = lastChart.right(5);
  ey.truncate(4);

  QValueList<Bar> indexList;
  int indexCount = -1;

  int dirLoop = dirList.findIndex(lastChart);
  if (dirLoop == -1)
    dirLoop = dirList.count() - 1;
  lastChart = dirList[dirLoop];

  s = "Adjustment";
  QString s2;
  getData(s, s2);
  bool adjustFlag = s2.toInt();

  while (dirLoop > 1)
  {
    if (indexCount >= barRange)
      break;

    s = baseDir + "/" + dirList[dirLoop];

    DbPlugin tdb;
    if (tdb.open(s, chartIndex))
    {
      tdb.close();
      dirLoop--;
      lastChart = dirList[dirLoop];
      continue;
    }

    BarData *recordList = new BarData(s);
    tdb.setBarRange(barRange);
    tdb.setBarLength(barLength);
    tdb.getHistory(recordList, startDate);
    tdb.close();

    int loop;
    QDateTime dt = startDate;
    int lastBar = -1;
    bool dataFlag = FALSE;
    for (loop = recordList->count() - 1; loop > -1; loop--)
    {
      if (indexCount >= barRange)
        break;

      recordList->getDate(loop, dt);
      fd.getCurrentContract(dt, s);
      if (! s.compare(lastChart))
      {
        Bar bar;
        recordList->getBar(loop, bar);
        indexList.prepend(bar);
        indexCount++;
        startDate = dt;
        lastBar = loop;
        dataFlag = TRUE;
      }
    }

    if (dataFlag)
    {
      if (adjustFlag)
      {
        Bar bar;
        double t = 0;
        if (lastBar - 1 > -1)
          t = recordList->getClose(lastBar) - recordList->getClose(lastBar - 1);
        bar.setClose(t);
        bar.setEmptyFlag(TRUE);
        indexList.prepend(bar);
      }
    }

    delete recordList;

    dirLoop--;
    lastChart = dirList[dirLoop];
  }

  if (! adjustFlag)
  {
    int loop;
    for (loop = 0; loop < (int) indexList.count(); loop++)
    {
      Bar bar = indexList[loop];
      barData->appendRaw(bar);
    }

    return;
  }

  // adjust the data
  double adjust = 0;
  double t = 0;
  bool flag = FALSE;
  Bar prevBar;
  int loop;
  for (loop = 1; loop < (int) indexList.count(); loop++)
  {
    Bar bar = indexList[loop];

    if (bar.getEmptyFlag())
    {
      t = bar.getClose();
      flag = TRUE;
      continue;
    }

    if (flag)
    {
      adjust = prevBar.getClose() - bar.getClose();
      bar.setOpen(bar.getOpen() + t);
      bar.setHigh(bar.getHigh() + t);
      bar.setLow(bar.getLow() + t);
      bar.setClose(bar.getClose() + t);
      flag = FALSE;
      t = 0;
    }

    bar.setOpen(bar.getOpen() + adjust);
    bar.setHigh(bar.getHigh() + adjust);
    bar.setLow(bar.getLow() + adjust);
    bar.setClose(bar.getClose() + adjust);
    barData->appendRaw(bar);
    prevBar = bar;
  }
}
Exemple #5
0
PlotLine * SYMBOL::getSYMBOL ()
{
    QString s;
    Config config;
    config.getData(Config::IndexPath, s);
    DBIndex index;
    index.open(s);

    PlotLine *line = new PlotLine();

    DbPlugin db;
    if (db.open(symbol, &index))
    {
        db.close();
        index.close();
        return line;
    }

    QDateTime date;
    data->getDate(0, date);

    QString ts;
    config.getData(Config::BarLength, ts);
    db.setBarLength((BarData::BarLength) ts.toInt());
    config.getData(Config::Bars, ts);
    db.setBarRange(ts.toInt());
    BarData *recordList = new BarData(symbol);
    QDateTime dt = QDateTime::currentDateTime();
    db.getHistory(recordList, dt);

    QDict<Setting> dict;
    dict.setAutoDelete(TRUE);

    int loop;
    ts = "Close";
    QString ts2;
    for (loop = 0; loop < (int) recordList->count(); loop++)
    {
        Setting *r = new Setting;
        ts2 = QString::number(recordList->getClose(loop));
        r->setData(ts, ts2);
        recordList->getDate(loop, dt);
        QString s = dt.toString("yyyyMMddhhmmss");
        dict.insert(s, r);
    }

    double val = 0;

    for (loop = 0; loop < (int) data->count(); loop++)
    {
        data->getDate(loop, dt);
        QString s = dt.toString("yyyyMMddhhmmss");
        Setting *r2 = dict[s];
        if (r2)
        {
            val = r2->getDouble(ts);
            line->append(val);
        }
    }

    delete recordList;
    db.close();
    index.close();

    line->setScaleFlag(TRUE);
    return line;
}
void UpgradeMessage::saveHeaderData (DbPlugin &db, QString &k, QString &d, QString &sym, DBIndexItem &item)
{
  // is this a co key?
  bool ok = FALSE;
  double t = k.toDouble(&ok);
  if (ok)
  {
    if (t < 10000)
    {
      // its a chart object
      Setting t;
      t.parse(d);
      QString s = "Plugin";
      QString s2;
      t.getData(s,s2);
      if (s2.length())
      {
        t.remove(s);
        s = "Type";
        t.setData(s, s2);
      }

      s = "Plot";
      t.getData(s, s2);
      if (! s2.compare("Main Plot"))
      {
        s2 = "Bars";
        t.setData(s, s2);
      }
      else
        return;

      index.setChartObject(sym, k, t);
      return;
    }
  }

  if (! k.compare("Type"))
  {
    item.setType(d);
    return;
  }

  if (! k.compare("FuturesType"))
  {
    item.setFuturesType(d);
    return;
  }

  if (! k.compare("FuturesMonth"))
  {
    item.setFuturesMonth(d);
    return;
  }

  if (! k.compare("BarType"))
  {
    item.setBarType(d);
    return;
  }

  if (! k.compare("Fundamentals"))
  {
    index.setFundamentals(sym, d);
    return;
  }

  if (! k.compare("LocalIndicators"))
  {
    index.addIndicator(sym, d);
    return;
  }

  if (! k.compare("QuotePlugin"))
  {
    item.setQuotePlugin(d);
    return;
  }

  if (! k.compare("Symbol"))
  {
    item.setSymbol(d);
    return;
  }

  if (! k.compare("Title"))
  {
    item.setTitle(d);
    return;
  }

  if (! k.compare("Path"))
  {
    item.setPath(d);
    return;
  }

  if (! k.compare("SpreadFirstSymbol"))
  {
    int t = d.find("/data0/", 0, TRUE);
    d.replace(t + 5, 1, "1");
    QString ts = "FirstSymbol";
    db.setData(ts, d);
    return;
  }

  if (! k.compare("SpreadSecondSymbol"))
  {
    int t = d.find("/data0/", 0, TRUE);
    d.replace(t + 5, 1, "1");
    QString ts = "SecondSymbol";
    db.setData(ts, d);
    return;
  }

  if (! k.compare("IndexList"))
  {
    while (1)
    {
      int t = d.find("/data0/", 0, TRUE);
      if (t == -1)
        break;
      else
        d.replace(t + 5, 1, "1");
    }
    QString ts = "List";
    db.setData(ts, d);
    return;
  }

  if (! k.compare("CCAdjustment"))
  {
    QString ts = "Adjustment";
    db.setData(ts, d);
    return;
  }
}
bool UpgradeMessage::createChart (QString &path)
{
  DB *olddb = 0;
  int rc = db_create(&olddb, NULL, 0);
  if (rc)
  {
   qDebug("UpgradeMessage::createChart: %s", db_strerror(rc));
    return TRUE;
  }
  
  rc = olddb->open(olddb, NULL, (char *) path.latin1(), NULL, DB_BTREE, DB_RDONLY, 0664);
  if (rc)
  {
    qDebug("UpgradeMessage::createChart: %s", db_strerror(rc));
    return TRUE;
  }

  QString newPath = path;
  int t = newPath.find("/data0/", 0, TRUE);
  newPath.replace(t + 5, 1, "1");
  DbPlugin db;
  if (db.open(newPath, &index))
  {
    olddb->close(olddb, 0);
    return TRUE;
  }

  QFileInfo fi(newPath);
  QString fn = fi.fileName();

  DBIndexItem item;

  DBT key, data;
  DBC *cur;
  memset(&key, 0, sizeof(DBT));
  memset(&data, 0, sizeof(DBT));
  olddb->cursor(olddb, NULL, &cur, 0);
  while (! cur->c_get(cur, &key, &data, DB_NEXT))
  {
    QString k = (char *) key.data;
    QString d = (char *) data.data;
    if (key.size != 15)
    {
      saveHeaderData(db, k, d, fn, item);
      continue;
    }

    Bar bar;
    if (bar.setDate(k))
      continue;
  
    QStringList l = QStringList::split(",", d, FALSE);
    bar.setOpen(l[0].toDouble());
    bar.setHigh(l[1].toDouble());
    bar.setLow(l[2].toDouble());
    bar.setClose(l[3].toDouble());
    bar.setVolume(l[4].toDouble());
    if (l.count() == 6)
      bar.setOI(l[5].toInt());
    db.setBar(bar);
  }
  cur->c_close(cur);

  olddb->close(olddb, 0);
  db.close();

  index.setIndexItem(fn, item);

  return FALSE;
}
Exemple #8
0
void Spread::getHistory (BarData *barData, QDateTime &startDate, QString &fs, QString &ss,
                     int barRange, BarData::BarLength barLength)
{
  // get the first symbol bars
  QString s;
  DbPlugin db;
  if (db.openChart(fs))
  {
    qDebug("DbPlugin::getSpreadHistory: cannot open first symbol chart");
    db.close();
    return;
  }

  BarData *bar = new BarData(fs);
  bar->setBarLength(barLength);
  db.setBarRange(barRange);
  db.getHistory(bar, startDate);
  db.close();

  // get the second symbol bars
  if (db.openChart(ss))
  {
    qDebug("DbPlugin::getSpreadHistory: cannot open second symbol chart");
    db.close();
    delete bar;
    return;
  }

  BarData *bar2 = new BarData(ss);
  bar2->setBarLength(barLength);
  db.setBarRange(barRange);
  db.getHistory(bar2, startDate);
  db.close();

  // create lookup dict for first symbol bars
  QDict<Bar> lookup;
  lookup.setAutoDelete(TRUE);
  int loop;
  for (loop = 0; loop < bar->count(); loop++)
  {
    Bar *r = new Bar;
    QDateTime dt;
    bar->getDate(loop, dt);
    r->setDate(dt);
    r->setClose(bar->getClose(loop));
    r->getDateTimeString(FALSE, s);
    lookup.insert(s, r);
  }

  // match all second symbol bars
  for (loop = bar2->count() - 1; loop > -1; loop--)
  {
    Bar r;
    QDateTime dt;
    bar2->getDate(loop, dt);
    s = dt.toString("yyyyMMddhhmmss");
    Bar *tr = lookup.find(s);
    if (tr)
    {
      double t = tr->getClose() - bar2->getClose(loop);
      r.setDate(dt);
      r.setOpen(t);
      r.setHigh(t);
      r.setLow(t);
      r.setClose(t);
      barData->prepend(r);
    }
  }

  delete bar;
  delete bar2;
}
Exemple #9
0
void Scanner::scan ()
{
  if (! fileList.count() && ! allSymbols->isChecked())
  {
    QMessageBox::information(this,
                             tr("Qtstalker: Error"),
			     tr("No symbols selected."));
    return;
  }

  // open the CUS plugin
  QString iplugin("CUS");
  IndicatorPlugin *plug = config.getIndicatorPlugin(iplugin);
  if (! plug)
  {
    config.closePlugin(iplugin);
    return;
  }

  QString s;
  list->getText(s);
  QStringList l = QStringList::split("\n", s, FALSE);
  plug->setCustomFunction(l);
  
  this->setEnabled(FALSE);
  
  // clear dir for scan symbols
  QDir dir;
  config.getData(Config::GroupPath, s);
  s.append("/Scanner");
  if (! dir.exists(s, TRUE))
    dir.mkdir(s, TRUE);
  s.append("/" + scannerName);
  if (! dir.exists(s, TRUE))
    dir.mkdir(s, TRUE);
  else
  {
    int loop;
    dir.setPath(s);
    for (loop = 2; loop < (int) dir.count(); loop++)
    {
      QString s2 = dir.absPath() + "/" + dir[loop];
      if (! dir.remove(s2, TRUE))
        qDebug("%s not removed", s2.latin1());
    }
  }
  
  if (allSymbols->isChecked())
  {
    QString ts;
    if (! basePath->currentText().compare(tr("Chart")))
      config.getData(Config::DataPath, ts);
    else
      config.getData(Config::GroupPath, ts);
    Traverse trav(Traverse::File);
    trav.traverse(ts);
    trav.getList(fileList);
  }
  
  QProgressDialog prog(tr("Scanning..."),
                       tr("Cancel"),
		       fileList.count(),
		       this,
		       "progress",
		       TRUE);
  prog.show();
  
  int minBars = bars->value();
  
  emit message(QString("Scanning..."));
  
  int loop;
  for (loop = 0; loop < (int) fileList.count(); loop++)
  {
    prog.setProgress(loop);
    emit message(QString());
    if (prog.wasCancelled())
    {
      emit message(QString("Scan cancelled"));
      break;
    }

    QFileInfo fi(fileList[loop]);
    if (fi.isDir())
      continue;

    DbPlugin db;
    QDir dir;
    if (! dir.exists(fileList[loop]))
      continue;
    db.open(fileList[loop], chartIndex);

    db.setBarRange(minBars);
    db.setBarLength((BarData::BarLength) barLengthList.findIndex(period->currentText()));

    BarData *recordList = new BarData(fileList[loop]);
    QDateTime dt = QDateTime::currentDateTime();
    db.getHistory(recordList, dt);
    db.close();
    
    // load the CUS plugin and calculate
    plug->setIndicatorInput(recordList);
    Indicator *i = plug->calculate();
    if (! i->getLines())
    {
      delete recordList;
      delete i;
      continue;
    }
    
    PlotLine *line = i->getLine(0);
    if (line && line->getSize() > 0)
    {
      if (line->getData(line->getSize() - 1) > 0)
      {
        QString ts;
        config.getData(Config::GroupPath, ts);
        QString s = "ln -s \"" + fileList[loop] + "\" " + ts + "/Scanner/" + scannerName;
        system(s);
      }
    }
    
    delete recordList;
    delete i;
    
    emit message(QString());
  }
  
  if (! prog.wasCancelled())
    emit message(QString("Scan complete"));
  
  config.closePlugin(iplugin);
  
  this->setEnabled(TRUE);

  emit scanComplete();
}