void GUIWidget::downloadHistory () { _log->clear(); setEnabled(FALSE); _okButton->setEnabled(FALSE); _cancelButton->setEnabled(TRUE); DateRange dr; QDateTime ed = QDateTime::currentDateTime(); QDateTime sd = dr.interval(ed, _range->currentIndex()); _timer->start(100); YahooHistoryDownload function(this); connect(&function, SIGNAL(signalMessage(QString)), _log, SLOT(append(const QString &))); connect(_cancelButton, SIGNAL(clicked()), &function, SLOT(stop())); function.download(_symbolButton->files(), sd, ed); _timer->stop(); setEnabled(TRUE); _okButton->setEnabled(TRUE); _cancelButton->setEnabled(FALSE); }
void GUIWidget::downloadHistory () { _log->clear(); setEnabled(FALSE); _okButton->setEnabled(FALSE); DateRange dr; QDateTime ed = QDateTime::currentDateTime(); QDateTime sd = dr.interval(ed, DateRange::_YEAR5); _timer->start(100); NetfondsHistoryDownload function(this); connect(&function, SIGNAL(signalMessage(QString)), _log, SLOT(append(const QString &))); function.download(getTickers()); //, sd, ed); _timer->stop(); setEnabled(TRUE); _okButton->setEnabled(TRUE); }
int Database::getBars (Bars *bd) { if (! bd) return 0; int length = bd->barLength(); if (length == -1) { qDebug() << "Database::getBars: invalid length"; return 0; } if (! getSymbol(bd)) return 0; if (! init()) return 0; // get last date in db QDateTime endDate = getMaxDate(bd); if (! endDate.isValid()) return 0; DateRange dr; //Don't bother to load less the all values into memory //All computers are bound to be able handle this in year 2012 //If ever porting to a mobile device, change at that point //bd->plotRange() QDateTime startDate = dr.interval(endDate, DateRange::_ALL); if (! startDate.isValid()) { qDebug() << "Database::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() << "Database::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->barLength(), 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; }
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; }