lib/Spread.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 "Spread.h"
00023 #include "Config.h"
00024 #include "Bar.h"
00025 #include "PrefDialog.h"
00026 #include "DbPlugin.h"
00027 #include <qdir.h>
00028 #include <qdict.h>
00029 #include <qinputdialog.h>
00030 #include <qobject.h>
00031 #include <qmessagebox.h>
00032 
00033 
00034 Spread::Spread ()
00035 {
00036 }
00037 
00038 Spread::~Spread ()
00039 {
00040 }
00041 
00042 void Spread::getHistory (BarData *barData, QDateTime &startDate, QString &fs, QString &ss,
00043                      int barRange, BarData::BarLength barLength)
00044 {
00045   // get the first symbol bars
00046   QString s;
00047   DbPlugin db;
00048   if (db.openChart(fs))
00049   {
00050     qDebug("DbPlugin::getSpreadHistory: cannot open first symbol chart");
00051     db.close();
00052     return;
00053   }
00054 
00055   BarData *bar = new BarData(fs);
00056   bar->setBarLength(barLength);
00057   db.setBarRange(barRange);
00058   db.getHistory(bar, startDate);
00059   db.close();
00060 
00061   // get the second symbol bars
00062   if (db.openChart(ss))
00063   {
00064     qDebug("DbPlugin::getSpreadHistory: cannot open second symbol chart");
00065     db.close();
00066     delete bar;
00067     return;
00068   }
00069 
00070   BarData *bar2 = new BarData(ss);
00071   bar2->setBarLength(barLength);
00072   db.setBarRange(barRange);
00073   db.getHistory(bar2, startDate);
00074   db.close();
00075 
00076   // create lookup dict for first symbol bars
00077   QDict<Bar> lookup;
00078   lookup.setAutoDelete(TRUE);
00079   int loop;
00080   for (loop = 0; loop < bar->count(); loop++)
00081   {
00082     Bar *r = new Bar;
00083     QDateTime dt;
00084     bar->getDate(loop, dt);
00085     r->setDate(dt);
00086     r->setClose(bar->getClose(loop));
00087     r->getDateTimeString(FALSE, s);
00088     lookup.insert(s, r);
00089   }
00090 
00091   // match all second symbol bars
00092   for (loop = bar2->count() - 1; loop > -1; loop--)
00093   {
00094     Bar r;
00095     QDateTime dt;
00096     bar2->getDate(loop, dt);
00097     s = dt.toString("yyyyMMddhhmmss");
00098     Bar *tr = lookup.find(s);
00099     if (tr)
00100     {
00101       double t = tr->getClose() - bar2->getClose(loop);
00102       r.setDate(dt);
00103       r.setOpen(t);
00104       r.setHigh(t);
00105       r.setLow(t);
00106       r.setClose(t);
00107       barData->prepend(r);
00108     }
00109   }
00110 
00111   delete bar;
00112   delete bar2;
00113 }
00114 
00115 bool Spread::createNew (QString &path, QString &symbol)
00116 {
00117   bool ok = FALSE;
00118   symbol = QInputDialog::getText(QObject::tr("New Spread"),
00119                                  QObject::tr("Enter symbol name for the new Spread"),
00120                                  QLineEdit::Normal,
00121                                  QString::null,
00122                                  &ok,
00123                                  0);
00124   if (! symbol.length() || ok == FALSE)
00125     return FALSE;
00126 
00127   QDir dir;
00128   Config config;
00129   QString s;
00130   config.getData(Config::DataPath, s);
00131   s.append("/Spread");
00132   if (! dir.exists(s))
00133   {
00134     if (! dir.mkdir(s, TRUE))
00135     {
00136       QMessageBox::information(0,
00137                                QObject::tr("Qtstalker: Error"),
00138                                QObject::tr("Could not create Spread directory."));
00139       return FALSE;
00140     }
00141   }
00142   
00143   s.append("/" + symbol);
00144   if (dir.exists(s))
00145   {
00146     QMessageBox::information(0,
00147                              QObject::tr("Qtstalker: Error"),
00148                              QObject::tr("This Spread already exists."));
00149     return FALSE;
00150   }
00151 
00152   path = s;
00153   return TRUE;
00154 }
00155 
00156 bool Spread::prefDialog (QString &fs, QString &ss)
00157 {
00158   QString pl = QObject::tr("Parms");
00159   QString fsl = QObject::tr("First Symbol");
00160   QString ssl = QObject::tr("Second Symbol");
00161   PrefDialog *dialog = new PrefDialog(0);
00162   dialog->setCaption(QObject::tr("Edit Spread"));
00163   dialog->createPage (pl);
00164   dialog->setHelpFile(helpFile);
00165 
00166   Config config;
00167   QString s;
00168   config.getData(Config::DataPath, s);
00169   dialog->addSymbolItem(fsl, pl, s, fs);
00170   dialog->addSymbolItem(ssl, pl, s, ss);
00171 
00172   int rc = dialog->exec();
00173   if (rc == QDialog::Accepted)
00174   {
00175     dialog->getSymbol(fsl, fs);
00176     dialog->getSymbol(ssl, ss);
00177     delete dialog;
00178     return TRUE;
00179   }
00180   delete dialog;
00181   return FALSE;
00182 }
00183 
00184 
00185