Beispiel #1
0
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;
  }
}
void StocksDialog::split ()
{
    int rc = QMessageBox::warning(this,
                                  tr("Warning"),
                                  tr("Are you sure to split this stock?"),
                                  QMessageBox::Yes,
                                  QMessageBox::No,
                                  QMessageBox::NoButton);

    if (rc == QMessageBox::No)
        return;

    QDate dt = splitDate->date();

    // verify if split date < first bar
    Bar bar;
    db->getFirstBar(bar);
    if (! bar.getEmptyFlag())
    {
        QDateTime td;
        bar.getDate(td);
        if (dt < td.date())
        {
            QMessageBox::information(this, tr("Qtstalker: Error"), tr("Invalid split date."));
            return;
        }
    }

    // verify if split date > last bar
    Bar bar2;
    db->getLastBar(bar2);
    if (! bar.getEmptyFlag())
    {
        QDateTime td;
        bar2.getDate(td);
        if (dt > td.date())
        {
            QMessageBox::information(this, tr("Qtstalker: Error"), tr("Invalid split date."));
            return;
        }
    }

    // verify if the ratio format is correct ?:?
    QStringList l = QStringList::split(":", splitRatio->text(), FALSE);
    if (l.count() != 2)
    {
        QMessageBox::information(this, tr("Qtstalker: Error"), tr("Invalid split ratio format.\neg. 2:1"));
        return;
    }

    double plyer = l[1].toDouble() / l[0].toDouble();
    double volplyer = l[0].toDouble() / l[1].toDouble();

    QString s;
    db->getSymbol(s);
    BarData *bars = new BarData(s);
    db->getAllBars(bars);

    int loop;
    for (loop = 0; loop < bars->count(); loop++)
    {
        Bar bar;
        bars->getBar(loop, bar);

        QDateTime td;
        bar.getDate(td);
        if (td.date() < dt)
        {
            bar.setOpen(bar.getOpen() * plyer);
            bar.setHigh(bar.getHigh() * plyer);
            bar.setLow(bar.getLow() * plyer);
            bar.setClose(bar.getClose() * plyer);
            bar.setVolume(bar.getVolume() * volplyer);
            db->setBar(bar);
        }
    }

    delete bars;

    // adjust any chart objects
    QDateTime adt(dt, QTime(0,0,0,0));
    QString fn;
    db->getIndexKey(fn);
    index->getChartObjects(fn, l);
    for (loop = 0; loop < (int) l.count(); loop++)
    {
        Setting set;
        set.parse(l[loop]);
        COBase tco;
        COBase *co = tco.getCO(set);
        if (! co)
            continue;

        co->adjustForSplit(adt, plyer);

        set.clear();
        co->getSettings(set);
        s = "Name";
        QString s2;
        set.getData(s, s2);
        index->setChartObject(fn, s2, set);
    }

    QMessageBox::information(this, tr("Qtstalker: Split Complete"), tr("Split complete."));

    reloadFlag = TRUE;
}