plugins/quote/NYBOT/NYBOT.cpp

Go to the documentation of this file.
00001 /*
00002  *  Qtstalker stock charter
00003  *
00004  *  Copyright (C) 2001-2007 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 "NYBOT.h"
00023 #include "Setting.h"
00024 #include "Bar.h"
00025 #include "DBIndexItem.h"
00026 #include "Exchange.h"
00027 #include <qfile.h>
00028 #include <qtextstream.h>
00029 #include <qtimer.h>
00030 #include <qstringlist.h>
00031 #include <qstring.h>
00032 #include <qdir.h>
00033 #include <qsettings.h>
00034 #include <qfileinfo.h>
00035 #include <qnetwork.h>
00036 #include <qlabel.h>
00037 
00038 
00039 NYBOT::NYBOT ()
00040 {
00041   pluginName = "NYBOT";
00042   helpFile = "nybot.html";
00043   cancelFlag = FALSE;
00044 
00045   connect(this, SIGNAL(signalGetFileDone(bool)), this, SLOT(fileDone(bool)));
00046   connect(this, SIGNAL(signalTimeout()), this, SLOT(timeoutError()));
00047 
00048   buildGui();
00049   loadSettings();
00050   qInitNetworkProtocols();
00051   resize(400, 400);
00052 }
00053 
00054 NYBOT::~NYBOT ()
00055 {
00056   plug.close();
00057   saveSettings();
00058 }
00059 
00060 void NYBOT::buildGui ()
00061 {
00062   setCaption(tr("NYBOT Quotes"));
00063   
00064   QLabel *label = new QLabel(tr("Date"), baseWidget);
00065   grid->addWidget(label, 0, 0);
00066 
00067   date = new QDateEdit(QDate::currentDate(), baseWidget);
00068   date->setAutoAdvance(TRUE);
00069   date->setOrder(QDateEdit::YMD);
00070   grid->addWidget(date, 0, 1);
00071   
00072   QDate dt = QDate::currentDate();
00073   if (dt.dayOfWeek() == 6)
00074     dt = dt.addDays(-1);
00075   else
00076   {
00077     if (dt.dayOfWeek() == 7)
00078       dt = dt.addDays(-2);
00079   }
00080   date->setDate(dt);
00081 }
00082 
00083 void NYBOT::update ()
00084 {
00085   plug.close();
00086   errorLoop = 0;  
00087 
00088   config.getData(Config::Home, file);
00089   file.append("/nybotDownload");
00090 
00091   // http://www.nybot.com/reports/dmrs/download/downloadAllSelection.asp?commodityType=future
00092   // http://www.nybot.com/reports/dmrs/files/2005,05,13_ALL_futures.csv
00093   url = "http://www.nybot.com/reports/dmrs/files/";
00094   QDate dt = date->date();
00095   url.append(dt.toString("yyyy,MM,dd"));
00096   url.append("_ALL_futures.csv");
00097   getFile(url);
00098 }
00099 
00100 void NYBOT::fileDone (bool d)
00101 {
00102   if (d)
00103   {
00104     QString ss(tr("Network error"));
00105     printStatusLogMessage(ss);
00106     downloadComplete();
00107     return;
00108   }
00109 
00110   QFile f(file);
00111   if (! f.open(IO_WriteOnly))
00112   {
00113     QString ss(tr("Cant write to file"));
00114     printStatusLogMessage(ss);
00115     downloadComplete();
00116     return;
00117   }
00118   QTextStream stream(&f);
00119   
00120   stream << data;
00121   f.close();
00122   
00123   parse();
00124 }
00125 
00126 void NYBOT::timeoutError ()
00127 {
00128   errorLoop++;
00129   if (errorLoop == retrySpin->value())
00130   {
00131     QString ss(tr("Timeout: retry limit"));
00132     printStatusLogMessage(ss);
00133     downloadComplete();
00134     return;
00135   }
00136   else
00137   {
00138     QString s = tr("Timeout: retry ") + QString::number(errorLoop + 1);
00139     printStatusLogMessage(s);
00140     getFile(url);
00141   }
00142 }
00143 
00144 void NYBOT::parse ()
00145 {
00146   if (cancelFlag)
00147     return;
00148   
00149   QFile f(file);
00150   if (! f.open(IO_ReadOnly))
00151     return;
00152   QTextStream stream(&f);
00153     
00154   QString ts = stream.readLine();
00155   QString s;
00156   stripJunk(ts, s);
00157   QStringList keys = QStringList::split(",", s, FALSE);
00158 
00159   while(stream.atEnd() == 0)
00160   {
00161     ts = stream.readLine();
00162     stripJunk(ts, s);
00163 
00164     QStringList l = QStringList::split(",", s, FALSE);
00165 
00166     if (l.count() != keys.count())
00167       continue;
00168 
00169     Setting data;
00170     int loop2;
00171     for (loop2 = 0; loop2 < (int) keys.count(); loop2++)
00172       data.setData(keys[loop2], l[loop2]);
00173 
00174     // symbol
00175     QString symbol;
00176     QString ts = "commoditySymbol";
00177     data.getData(ts, symbol);
00178     symbol = symbol.stripWhiteSpace();
00179     if (symbol.length() == 0)
00180       continue;
00181 
00182     if (! symbol.compare("CC") || ! symbol.compare("CR") || ! symbol.compare("CT") ||
00183         ! symbol.compare("DX") || ! symbol.compare("KC") || ! symbol.compare("OJ") ||
00184         ! symbol.compare("SB") || ! symbol.compare("YX"))
00185     {
00186     }
00187     else
00188       continue;
00189 
00190     // date
00191     QString date;
00192     ts = "tradeDate";
00193     data.getData(ts, date);
00194     date.append("000000");
00195 
00196     Bar bar;
00197     if (bar.setDate(date))
00198     {
00199       QString ss = tr("Bad date") + " " + date;
00200       printStatusLogMessage(ss);
00201       continue;
00202     }
00203 
00204     ts = "dailyOpenPrice1";
00205     data.getData(ts, s);
00206     if (s.toFloat() == 0)
00207     {
00208       ts = "dailyOpenPrice2";
00209       data.getData(ts, s);
00210     }
00211     if (setTFloat(s, FALSE))
00212       continue;
00213     else
00214       bar.setOpen(tfloat);
00215 
00216     ts = "dailyHigh";
00217     data.getData(ts, s);
00218     if (setTFloat(s, FALSE))
00219       continue;
00220     else
00221       bar.setHigh(tfloat);
00222 
00223     ts = "dailyLow";
00224     data.getData(ts, s);
00225     if (setTFloat(s, FALSE))
00226       continue;
00227     else
00228       bar.setLow(tfloat);
00229 
00230     ts = "settlementPrice";
00231     data.getData(ts, s);
00232     if (setTFloat(s, FALSE))
00233       continue;
00234     else
00235       bar.setClose(tfloat);
00236 
00237     ts = "tradeVolume";
00238     data.getData(ts, s);
00239     if (setTFloat(s, FALSE))
00240       continue;
00241     else
00242       bar.setVolume(tfloat);
00243 
00244     ts = "openInterest";
00245     data.getData(ts, s);
00246     if (setTFloat(s, FALSE))
00247       continue;
00248     else
00249       bar.setOI((int) tfloat);
00250 
00251     if (symbol.compare("CC"))
00252     {
00253       bar.setOpen(bar.getOpen() / 100);
00254       bar.setHigh(bar.getHigh() / 100);
00255       bar.setLow(bar.getLow() / 100);
00256       bar.setClose(bar.getClose() / 100);
00257     }
00258 
00259     if (bar.verify())
00260       continue;
00261 
00262     //futures month
00263     ts = "contractMonth";
00264     data.getData(ts, s);
00265     QString year = s.left(4);
00266     QString month = s.right(2);
00267     QString fmonth;
00268 
00269     switch (month.toInt())
00270     {
00271       case 1:
00272         fmonth = "F";
00273         break;
00274       case 2:
00275         fmonth = "G";
00276         break;
00277       case 3:
00278         fmonth = "H";
00279         break;
00280       case 4:
00281         fmonth = "J";
00282         break;
00283       case 5:
00284         fmonth = "K";
00285         break;
00286       case 6:
00287         fmonth = "M";
00288         break;
00289       case 7:
00290         fmonth = "N";
00291         break;
00292       case 8:
00293         fmonth = "Q";
00294         break;
00295       case 9:
00296         fmonth = "U";
00297         break;
00298       case 10:
00299         fmonth = "V";
00300         break;
00301       case 11:
00302         fmonth = "X";
00303         break;
00304       case 12:
00305         fmonth = "Z";
00306         break;
00307       default:
00308         break;
00309     }
00310 
00311     if (fd.setSymbol(symbol))
00312       continue;
00313 
00314     if (year.length())
00315     {
00316       symbol.append(year);
00317 
00318       if (fmonth.length())
00319         symbol.append(fmonth);
00320       else
00321         continue;
00322     }
00323     else
00324       continue;
00325 
00326     s = "Futures/";
00327     QString s2;
00328     fd.getExchange(s2);
00329     s.append(s2 + "/");
00330     fd.getSymbol(s2);
00331     s.append(s2);
00332     QString path;
00333     createDirectory(s, path);
00334     if (! path.length())
00335     {
00336       QString ss(tr("Unable to create futures directory"));
00337       printStatusLogMessage(ss);
00338       return;
00339     }
00340         
00341     s = path + "/" + symbol;
00342     if (plug.open(s, chartIndex))
00343     {
00344       QString ss(tr("Could not open db"));
00345       printStatusLogMessage(ss);
00346       return;
00347     }
00348 
00349     DBIndexItem item;
00350     chartIndex->getIndexItem(symbol, item);
00351     item.getSymbol(s);
00352     if (! s.length())
00353     {
00354       if (plug.createNewFutures())
00355         return;
00356 
00357       chartIndex->getIndexItem(symbol, item);
00358 
00359       s = QString::number(Exchange::NYBOT);
00360       item.setExchange(s);
00361 
00362       item.setQuotePlugin(pluginName);
00363 
00364       chartIndex->setIndexItem(symbol, item);
00365     }
00366 
00367     plug.setBar(bar);
00368                  
00369     plug.close();
00370 
00371     emit signalWakeup();
00372   }
00373 
00374   f.close();
00375 
00376   downloadComplete();
00377   if (cancelFlag)
00378   {
00379     cancelFlag = FALSE;
00380     QString ss(tr("Update cancelled"));
00381     printStatusLogMessage(ss);
00382   }
00383   else
00384   {
00385     QString ss(tr("Done"));
00386     printStatusLogMessage(ss);
00387   }
00388 }
00389 
00390 void NYBOT::cancelUpdate ()
00391 {
00392   cancelFlag = TRUE;
00393 }
00394 
00395 void NYBOT::loadSettings ()
00396 {
00397   QSettings settings;
00398   settings.beginGroup("/Qtstalker/NYBOT plugin");
00399   
00400   QString s = settings.readEntry("/Retry", "3");
00401   retrySpin->setValue(s.toInt());
00402   
00403   s = settings.readEntry("/Timeout", "15");
00404   timeoutSpin->setValue(s.toInt());
00405   
00406   settings.endGroup();
00407 }
00408 
00409 void NYBOT::saveSettings ()
00410 {
00411   QSettings settings;
00412   settings.beginGroup("/Qtstalker/NYBOT plugin");
00413   settings.writeEntry("/Retry", retrySpin->text());
00414   settings.writeEntry("/Timeout", timeoutSpin->text());
00415   settings.endGroup();
00416 }
00417 
00418 //**********************************************************
00419 //**********************************************************
00420 //**********************************************************
00421 
00422 QuotePlugin * createQuotePlugin ()
00423 {
00424   NYBOT *o = new NYBOT;
00425   return ((QuotePlugin *) o);
00426 }
00427 
00428