lib/BarData.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 "BarData.h"
00023 #include <qobject.h>
00024 
00025 BarData::BarData (QString &sym)
00026 {
00027   symbol = sym;
00028   high = -99999999;
00029   low = 99999999;
00030   dateList.setAutoDelete(TRUE);
00031   barType = Daily;
00032   barLength = DailyBar;
00033 }
00034 
00035 BarData::~BarData ()
00036 {
00037 }
00038 
00039 void BarData::getInputFields (QStringList &l)
00040 {
00041   l.clear();
00042   l.append(QObject::tr("Open"));
00043   l.append(QObject::tr("High"));
00044   l.append(QObject::tr("Low"));
00045   l.append(QObject::tr("Close"));
00046   l.append(QObject::tr("Volume"));
00047   l.append(QObject::tr("OpenInterest"));
00048   l.append(QObject::tr("Day"));
00049   l.append(QObject::tr("Week"));
00050   l.append(QObject::tr("Month"));
00051   l.append(QObject::tr("DayOfWeek"));
00052 }
00053 
00054 PlotLine * BarData::getInput (BarData::InputType field)
00055 {
00056   PlotLine *in = new PlotLine();
00057   int loop;
00058   QDateTime dt;
00059   for (loop = 0; loop < (int) barList.count(); loop++)
00060   {
00061     switch(field)
00062     {
00063       case Open:
00064         in->append(getOpen(loop));
00065         break;
00066       case High:
00067         in->append(getHigh(loop));
00068         break;
00069       case Low:
00070         in->append(getLow(loop));
00071         break;
00072       case Volume:
00073         in->append(getVolume(loop));
00074         break;
00075       case OpenInterest:
00076         in->append(getOI(loop));
00077         break;
00078       case Day:
00079         getDate(loop, dt);
00080         in->append(dt.date().day());
00081         break;
00082       case Week:
00083         getDate(loop, dt);
00084         in->append(dt.date().weekNumber());
00085         break;
00086       case Month:
00087         getDate(loop, dt);
00088         in->append(dt.date().month());
00089         break;
00090       case DayOfWeek:
00091         getDate(loop, dt);
00092         in->append(dt.date().dayOfWeek());
00093         break;
00094       default:
00095         in->append(getClose(loop));
00096         break;
00097     }
00098   }
00099 
00100   return in;
00101 }
00102 
00103 int BarData::count ()
00104 {
00105   return (int) barList.count();
00106 }
00107 
00108 void BarData::prepend (Bar &bar)
00109 {
00110   QDateTime dt;
00111   bar.getDate(dt);
00112 
00113   if (! currentBar.getEmptyFlag())
00114   {
00115     if (dt >= startDate && dt < endDate)
00116     {
00117       currentBar.setOpen(bar.getOpen());
00118       if (bar.getHigh() > currentBar.getHigh())
00119         currentBar.setHigh(bar.getHigh());
00120       if (bar.getLow() < currentBar.getLow())
00121         currentBar.setLow(bar.getLow());
00122 //    if (barType == Tick)
00123         currentBar.setVolume(currentBar.getVolume() + bar.getVolume());
00124       if (bar.getOI() > currentBar.getOI())
00125         currentBar.setOI((int) bar.getOI());
00126     }
00127     else
00128     {
00129       if (currentBar.getHigh() > high)
00130         high = currentBar.getHigh();
00131       if (currentBar.getLow() < low)
00132         low = currentBar.getLow();
00133 
00134       barList.prepend(currentBar);
00135       currentBar.clear();
00136 
00137       setStartEndDates(dt);
00138 
00139       currentBar = bar;
00140 
00141       if (barLength > Minute60)
00142         currentBar.setDate(dt); // test
00143       else
00144         currentBar.setDate(endDate); // test
00145     }
00146   }
00147   else
00148   {
00149     currentBar.clear();
00150     setStartEndDates(dt);
00151     currentBar = bar;
00152     if (barLength > Minute60)
00153       currentBar.setDate(dt); // test
00154     else
00155       currentBar.setDate(endDate); // test
00156   }
00157 }
00158 
00159 void BarData::prependRaw (Bar &bar)
00160 {
00161   barList.prepend(bar);
00162 }
00163 
00164 void BarData::appendRaw (Bar &bar)
00165 {
00166   barList.append(bar);
00167 }
00168 
00169 void BarData::createDateList ()
00170 {
00171   if (! currentBar.getEmptyFlag())
00172   {
00173     if (currentBar.getHigh() > high)
00174       high = currentBar.getHigh();
00175     if (currentBar.getLow() < low)
00176       low = currentBar.getLow();
00177     barList.prepend(currentBar);
00178     currentBar.clear();
00179   }
00180 
00181   dateList.clear();
00182   
00183   int loop;
00184   for (loop = 0; loop < (int) barList.count(); loop++)
00185   {
00186     Bar bar = barList[loop];
00187     
00188     X *x = new X;
00189     x->x = loop;
00190 
00191     QString s;
00192     bar.getDateTimeString(FALSE, s);
00193     dateList.replace(s, x);
00194   }
00195 }
00196 
00197 void BarData::getDate (int i, QDateTime &dt)
00198 {
00199   barList[i].getDate(dt);
00200 }
00201 
00202 int BarData::getX (QDateTime &date)
00203 {
00204   X *x = 0;
00205   QString s = date.toString("yyyyMMddhhmmss");
00206   x = dateList[s];
00207   
00208   if (x)
00209     return x->x;
00210   else
00211     return -1;
00212 
00213 /*    
00214   if (barCompression == Weekly)
00215   {
00216     QDateTime dt = date;
00217     dt = dt.addDays(- (dt.date().dayOfWeek() - 1));
00218   
00219     int loop = 0;
00220     for (loop = 0; loop < 6; loop++)
00221     {
00222       X *x = dateList[dt.toString("yyyyMMdd")];
00223       if (x)
00224        return x->x;
00225       else
00226         dt = dt.addDays(1);
00227     }
00228     
00229     return -1;
00230   }
00231 
00232   if (barCompression == Monthly)
00233   {
00234     QDateTime dt = date;
00235     dt.date().setYMD(date.date().year(), 1, 1);
00236   
00237     int loop = 0;
00238     for (loop = 0; loop < dt.date().daysInMonth(); loop++)
00239     {
00240       X *x = dateList[dt.toString("yyyyMMdd")];
00241       if (x)
00242        return x->x;
00243       else
00244         dt = dt.addDays(1);
00245     }
00246     
00247     return -1;
00248   }
00249   
00250   return -1;
00251 */  
00252 }
00253 
00254 double BarData::getOpen (int i)
00255 {
00256   return barList[i].getOpen();
00257 }
00258 
00259 double BarData::getHigh (int i)
00260 {
00261   return barList[i].getHigh();
00262 }
00263 
00264 double BarData::getLow (int i)
00265 {
00266   return barList[i].getLow();
00267 }
00268 
00269 double BarData::getClose (int i)
00270 {
00271   return barList[i].getClose();
00272 }
00273 
00274 double BarData::getVolume (int i)
00275 {
00276   return barList[i].getVolume();
00277 }
00278 
00279 double BarData::getOI (int i)
00280 {
00281   return barList[i].getOI();
00282 }
00283 
00284 double BarData::getMax ()
00285 {
00286   return high;
00287 }
00288 
00289 double BarData::getMin ()
00290 {
00291   return low;
00292 }
00293 
00294 void BarData::setBarType (BarData::BarType d)
00295 {
00296   barType = d;
00297 }
00298 
00299 BarData::BarType BarData::getBarType ()
00300 {
00301   return barType;
00302 }
00303 
00304 BarData::InputType BarData::getInputType (QString &d)
00305 {
00306   InputType t = Close;
00307   
00308   while (1)
00309   {
00310     if (! d.compare(QObject::tr("Open")))
00311     {
00312       t = Open;
00313       break;
00314     }
00315     
00316     if (! d.compare(QObject::tr("High")))
00317     {
00318       t = High;
00319       break;
00320     }
00321 
00322     if (! d.compare(QObject::tr("Low")))
00323     {
00324       t = Low;
00325       break;
00326     }
00327 
00328     if (! d.compare(QObject::tr("Close")))
00329     {
00330       t = Close;
00331       break;
00332     }
00333 
00334     if (! d.compare(QObject::tr("Volume")))
00335     {
00336       t = Volume;
00337       break;
00338     }
00339 
00340     if (! d.compare(QObject::tr("OpenInterest")))
00341     {
00342       t = OpenInterest;
00343       break;
00344     }
00345     
00346     if (! d.compare(QObject::tr("Day")))
00347     {
00348       t = Day;
00349       break;
00350     }
00351     
00352     if (! d.compare(QObject::tr("Week")))
00353     {
00354       t = Week;
00355       break;
00356     }
00357     
00358     if (! d.compare(QObject::tr("Month")))
00359     {
00360       t = Month;
00361       break;
00362     }
00363     
00364     if (! d.compare(QObject::tr("DayofWeek")))
00365     {
00366       t = DayOfWeek;
00367       break;
00368     }
00369     
00370     break;
00371   }
00372   
00373   return t;
00374 }
00375 
00376 void BarData::getBarLengthList (QStringList &l)
00377 {
00378   l.clear();
00379   l.append(QObject::tr("1 Minute"));
00380   l.append(QObject::tr("5 Minute"));
00381   l.append(QObject::tr("10 Minute"));
00382   l.append(QObject::tr("15 Minute"));
00383   l.append(QObject::tr("30 Minute"));
00384   l.append(QObject::tr("60 Minute"));
00385   l.append(QObject::tr("Daily"));
00386   l.append(QObject::tr("Weekly"));
00387   l.append(QObject::tr("Monthly"));
00388 }
00389 
00390 void BarData::getBar (int d, Bar &bar)
00391 {
00392   bar = barList[d];
00393 }
00394 
00395 void BarData::setBar (int d, Bar &bar)
00396 {
00397   barList[d] = bar;
00398 }
00399 
00400 void BarData::setMinMax ()
00401 {
00402   int loop;
00403   for (loop = 0; loop < (int) barList.count(); loop++)
00404   {
00405     Bar bar = barList[loop];
00406 
00407     if (bar.getHigh() > high)
00408       high = bar.getHigh();
00409 
00410     if (bar.getLow() < low)
00411       low = bar.getLow();
00412   }
00413 }
00414 
00415 void BarData::clear ()
00416 {
00417   high = -99999999;
00418   low = 99999999;
00419   dateList.clear();
00420   barList.clear();
00421   currentBar.clear();
00422 }
00423 
00424 void BarData::setBarLength (BarData::BarLength d)
00425 {
00426   barLength = d;
00427 }
00428 
00429 BarData::BarLength BarData::getBarLength ()
00430 {
00431   return barLength;
00432 }
00433 
00434 void BarData::setStartEndDates (QDateTime &d)
00435 {
00436   QString s, s2;
00437   int tint = 0;
00438   QDateTime dt = d;
00439 
00440   switch (barLength)
00441   {
00442     case Minute1:
00443       dt.setTime(QTime(dt.time().hour(), dt.time().minute(), 0, 0));
00444       startDate = dt;
00445       dt = dt.addSecs(60);
00446       endDate = dt;
00447       break;
00448     case Minute5:
00449       tint = dt.time().minute() / 5;
00450       dt.setTime(QTime(dt.time().hour(), tint * 5, 0, 0));
00451       startDate = dt;
00452       dt = dt.addSecs(300);
00453       endDate = dt;
00454       break;
00455     case Minute10:
00456       tint = dt.time().minute() / 10;
00457       dt.setTime(QTime(dt.time().hour(), tint * 10, 0, 0));
00458       startDate = dt;
00459       dt = dt.addSecs(600);
00460       endDate = dt;
00461       break;
00462     case Minute15:
00463       tint = dt.time().minute() / 15;
00464       dt.setTime(QTime(dt.time().hour(), tint * 15, 0, 0));
00465       startDate = dt;
00466       dt = dt.addSecs(900);
00467       endDate = dt;
00468       break;
00469     case Minute30:
00470       tint = dt.time().minute() / 30;
00471       dt.setTime(QTime(dt.time().hour(), tint * 30, 0, 0));
00472       startDate = dt;
00473       dt = dt.addSecs(1800);
00474       endDate = dt;
00475       break;
00476     case Minute60:
00477       dt.setTime(QTime(dt.time().hour(), 0, 0, 0));
00478       startDate = dt;
00479       dt = dt.addSecs(3600);
00480       endDate = dt;
00481       break;
00482     case DailyBar:
00483       dt.setTime(QTime(0, 0, 0, 0));
00484       startDate = dt;
00485       dt.setTime(QTime(23, 59, 59, 0));
00486       endDate = dt;
00487       break;
00488     case WeeklyBar:
00489       dt.setTime(QTime(0, 0, 0, 0));
00490       dt = dt.addDays(- dt.date().dayOfWeek());
00491       startDate = dt;
00492       dt.setTime(QTime(23, 59, 59, 0));
00493       dt = dt.addDays(6);
00494       endDate = dt;
00495       break;
00496     case MonthlyBar:
00497       dt.setTime(QTime(0, 0, 0, 0));
00498       dt = dt.addDays(- (dt.date().day() - 1));
00499       startDate = dt;
00500       dt.setTime(QTime(23, 59, 59, 0));
00501       dt = dt.addDays(dt.date().daysInMonth() - 1);
00502       endDate = dt;
00503       break;
00504     default:
00505       break;
00506   }
00507 }
00508 
00509 void BarData::getSymbol (QString &d)
00510 {
00511   d = symbol;
00512 }
00513