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 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; } }
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 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; }
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(); }