lib/CC.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  *
00004  *  Copyright (C) 2001-2006 Stefan S. Stratigakos
00005  *
00006  *  This program is free software; you can redistribute it and/or modify
00007  *  it under the terms of the GNU General Public License as published by
00008  *  the Free Software Foundation; either version 2 of the License, or
00009  *  (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019  *  USA.
00020  */
00021 
00022 #include "CC.h"
00023 #include "FuturesData.h"
00024 #include "Config.h"
00025 #include "Bar.h"
00026 #include "PrefDialog.h"
00027 #include "DbPlugin.h"
00028 #include <qdir.h>
00029 #include <qvaluelist.h>
00030 #include <qobject.h>
00031 #include <qmessagebox.h>
00032 
00033 
00034 CC::CC ()
00035 {
00036 }
00037 
00038 CC::~CC ()
00039 {
00040 }
00041 
00042 void CC::getHistory (BarData *barData, QDateTime &startDate, QString &fs, bool adjustFlag,
00043                      int barRange, BarData::BarLength barLength)
00044 {
00045   FuturesData fd;
00046   QString s;
00047   if (fd.setSymbol(fs))
00048   {
00049     qDebug("DbPlugin::getCCHistory: invalid futures symbol");
00050     return;
00051   }
00052     
00053   Config config;
00054   QString baseDir;
00055   config.getData(Config::DataPath, baseDir);
00056   baseDir.append("/Futures/");
00057   fd.getExchange(s);
00058   baseDir.append(s + "/");
00059   fd.getSymbol(s);
00060   baseDir.append(s);
00061   QDir dir(baseDir);
00062   if (! dir.exists(baseDir, TRUE))
00063     return;
00064   QStringList dirList = dir.entryList();
00065 
00066   QString lastChart;
00067   fd.getCurrentContract(startDate, lastChart);
00068   QString ey = lastChart.right(5);
00069   ey.truncate(4);
00070 
00071   QValueList<Bar> indexList;
00072   int indexCount = -1;
00073 
00074   int dirLoop = dirList.findIndex(lastChart);
00075   if (dirLoop == -1)
00076     dirLoop = dirList.count() - 1;
00077   lastChart = dirList[dirLoop];
00078 
00079   while (dirLoop > 1)
00080   {
00081     if (indexCount >= barRange)
00082       break;
00083 
00084     s = baseDir + "/" + dirList[dirLoop];
00085 
00086     DbPlugin tdb;
00087     if (tdb.openChart(s))
00088     {
00089       tdb.close();
00090       dirLoop--;
00091       lastChart = dirList[dirLoop];
00092       continue;
00093     }
00094 
00095     BarData *recordList = new BarData(s);
00096     tdb.setBarRange(barRange);
00097     tdb.setBarLength(barLength);
00098     tdb.getHistory(recordList, startDate);
00099     tdb.close();
00100 
00101     int loop;
00102     QDateTime dt = startDate;
00103     int lastBar = -1;
00104     bool dataFlag = FALSE;
00105     for (loop = recordList->count() - 1; loop > -1; loop--)
00106     {
00107       if (indexCount >= barRange)
00108         break;
00109 
00110       recordList->getDate(loop, dt);
00111       fd.getCurrentContract(dt, s);
00112       if (! s.compare(lastChart))
00113       {
00114         Bar bar;
00115         recordList->getBar(loop, bar);
00116         indexList.prepend(bar);
00117         indexCount++;
00118         startDate = dt;
00119         lastBar = loop;
00120         dataFlag = TRUE;
00121       }
00122     }
00123 
00124     if (dataFlag)
00125     {
00126       if (adjustFlag)
00127       {
00128         Bar bar;
00129         double t = 0;
00130         if (lastBar - 1 > -1)
00131           t = recordList->getClose(lastBar) - recordList->getClose(lastBar - 1);
00132         bar.setClose(t);
00133         bar.setEmptyFlag(TRUE);
00134         indexList.prepend(bar);
00135       }
00136     }
00137 
00138     delete recordList;
00139 
00140     dirLoop--;
00141     lastChart = dirList[dirLoop];
00142   }
00143 
00144   if (! adjustFlag)
00145   {
00146     int loop;
00147     for (loop = 0; loop < (int) indexList.count(); loop++)
00148     {
00149       Bar bar = indexList[loop];
00150       barData->appendRaw(bar);
00151     }
00152 
00153     return;
00154   }
00155 
00156   // adjust the data
00157   double adjust = 0;
00158   double t = 0;
00159   bool flag = FALSE;
00160   Bar prevBar;
00161   int loop;
00162   for (loop = 1; loop < (int) indexList.count(); loop++)
00163   {
00164     Bar bar = indexList[loop];
00165 
00166     if (bar.getEmptyFlag())
00167     {
00168       t = bar.getClose();
00169       flag = TRUE;
00170       continue;
00171     }
00172 
00173     if (flag)
00174     {
00175       adjust = prevBar.getClose() - bar.getClose();
00176       bar.setOpen(bar.getOpen() + t);
00177       bar.setHigh(bar.getHigh() + t);
00178       bar.setLow(bar.getLow() + t);
00179       bar.setClose(bar.getClose() + t);
00180       flag = FALSE;
00181       t = 0;
00182     }
00183 
00184     bar.setOpen(bar.getOpen() + adjust);
00185     bar.setHigh(bar.getHigh() + adjust);
00186     bar.setLow(bar.getLow() + adjust);
00187     bar.setClose(bar.getClose() + adjust);
00188     barData->appendRaw(bar);
00189     prevBar = bar;
00190   }
00191 }
00192 
00193 bool CC::createNew (QString &path, QString &symbol, QString &adjustment)
00194 {
00195   FuturesData fd;
00196   QStringList l;
00197   fd.getSymbolList(l);
00198 
00199   QString pl = QObject::tr("Parms");
00200   QString fsl = QObject::tr("Futures Symbol");
00201   QString gl = QObject::tr("Gapless");
00202 
00203   PrefDialog *dialog = new PrefDialog(0);
00204   dialog->setCaption(QObject::tr("New CC"));
00205   dialog->createPage (pl);
00206   dialog->setHelpFile(helpFile);
00207   dialog->addComboItem(fsl, pl, l, 0);
00208   dialog->addCheckItem(gl, pl, TRUE);
00209   int rc = dialog->exec();
00210   if (rc == QDialog::Accepted)
00211   {
00212     dialog->getCombo(fsl, symbol);
00213     bool f = dialog->getCheck(gl);
00214     adjustment = QString::number(f);
00215 
00216     QDir dir;
00217     Config config;
00218     QString s;
00219     config.getData(Config::DataPath, s);
00220     s.append("/CC");
00221     if (! dir.exists(s))
00222     {
00223       if (! dir.mkdir(s, TRUE))
00224       {
00225         QMessageBox::information(0,
00226                                  QObject::tr("Qtstalker: Error"),
00227                                  QObject::tr("Could not create ~/.qtstalker/data/CC directory."));
00228         delete dialog;
00229         return FALSE;
00230       }
00231     }
00232   
00233     s.append("/" + symbol);
00234     if (dir.exists(s))
00235     {
00236       QMessageBox::information(0,
00237                                QObject::tr("Qtstalker: Error"),
00238                                QObject::tr("This CC already exists."));
00239       delete dialog;
00240       return FALSE;
00241     }
00242     path = s;
00243 
00244     delete dialog;
00245     return TRUE;
00246   }
00247 
00248   delete dialog;
00249   return FALSE;
00250 }
00251 
00252 bool CC::prefDialog (QString &adjustment)
00253 {
00254   QString pl = QObject::tr("Parms");
00255   QString gl = QObject::tr("Gapless");
00256   PrefDialog *dialog = new PrefDialog(0);
00257   dialog->setCaption(QObject::tr("Edit CC"));
00258   dialog->createPage (pl);
00259   dialog->setHelpFile(helpFile);
00260   dialog->addCheckItem(gl, pl, adjustment.toInt());
00261   int rc = dialog->exec();
00262   if (rc == QDialog::Accepted)
00263   {
00264     bool f = dialog->getCheck(gl);
00265     adjustment = QString::number(f);
00266     delete dialog;
00267     return TRUE;
00268   }
00269   delete dialog;
00270   return FALSE;
00271 }
00272 
00273 
00274