lib/Bar.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 "Bar.h"
00023 
00024 Bar::Bar ()
00025 {
00026   clear();
00027 }
00028 
00029 Bar::~Bar ()
00030 {
00031 }
00032 
00033 int Bar::setDate (QDateTime &d)
00034 {
00035   if (! d.isValid())
00036     return TRUE;
00037 
00038   date = d;
00039   return FALSE;
00040 }
00041 
00042 int Bar::setDate (QString &d)
00043 {
00044   QString s = d;
00045   while (s.contains("-"))
00046     s = s.remove(s.find("-", 0, TRUE), 1);
00047   
00048   while (s.contains(":"))
00049     s = s.remove(s.find(":", 0, TRUE), 1);
00050 
00051   while (s.contains(" "))
00052     s = s.remove(s.find(" ", 0, TRUE), 1);
00053   
00054   if (s.length() != 14)
00055   {
00056     qDebug("Bar::setDate:bad string length %i", s.length());
00057     return TRUE;
00058   }
00059   
00060   QDate dt = QDate(s.left(4).toInt(), s.mid(4, 2).toInt(), s.mid(6, 2).toInt());
00061   if (! dt.isValid())
00062   {
00063     qDebug("Bar::setDate: invalid date %s", s.latin1());
00064     return TRUE;
00065   }
00066   
00067   int hour = s.mid(8, 2).toInt();
00068   if (hour < 0 || hour > 23)
00069   {
00070     qDebug("Bar::setDate: hour out of range %i", hour);
00071     return TRUE;
00072   }
00073     
00074   int min = s.mid(10, 2).toInt();
00075   if (min < 0 || min > 59)
00076   {
00077     qDebug("Bar::setDate: minute out of range %i", min);
00078     return TRUE;
00079   }
00080 
00081   int sec = s.right(2).toInt();
00082   if (sec < 0 || sec > 59)
00083   {
00084     qDebug("Bar::setDate: second out of range %i", min);
00085     return TRUE;
00086   }
00087   
00088   QTime t(hour, min, sec, 0);
00089   if (! t.isValid())
00090   {
00091     qDebug("Bar::setDate: invalid time");
00092     return TRUE;
00093   }
00094   
00095   date.setDate(dt);
00096   date.setTime(t);
00097     
00098   return FALSE;
00099 }
00100 
00101 void Bar::getDate (QDateTime &d)
00102 {
00103   d = date;
00104 }
00105 
00106 void Bar::setOpen (double d)
00107 {
00108   open = d;
00109   openFlag = TRUE;
00110   emptyFlag = FALSE;
00111 }
00112 
00113 double Bar::getOpen ()
00114 {
00115   return open;
00116 }
00117 
00118 void Bar::setHigh (double d)
00119 {
00120   high = d;
00121   highFlag = TRUE;
00122   emptyFlag = FALSE;
00123 }
00124 
00125 double Bar::getHigh ()
00126 {
00127   return high;
00128 }
00129 
00130 void Bar::setLow (double d)
00131 {
00132   low = d;
00133   lowFlag = TRUE;
00134   emptyFlag = FALSE;
00135 }
00136 
00137 double Bar::getLow ()
00138 {
00139   return low;
00140 }
00141 
00142 void Bar::setClose (double d)
00143 {
00144   close = d;
00145   closeFlag = TRUE;
00146   emptyFlag = FALSE;
00147 }
00148 
00149 double Bar::getClose ()
00150 {
00151   return close;
00152 }
00153 
00154 void Bar::setVolume (double d)
00155 {
00156   volume = d;
00157   volumeFlag = TRUE;
00158   emptyFlag = FALSE;
00159 }
00160 
00161 double Bar::getVolume ()
00162 {
00163   return volume;
00164 }
00165 
00166 void Bar::setOI (int d)
00167 {
00168   oi = d;
00169   oiFlag = TRUE;
00170   emptyFlag = FALSE;
00171 }
00172 
00173 double Bar::getOI ()
00174 {
00175   return oi;
00176 }
00177 
00178 void Bar::getString (QString &s)
00179 {
00180   getDateTimeString(TRUE, s);
00181 
00182   if (openFlag)
00183   {
00184     s.append(" ");
00185     s.append(QString::number(open, 'g'));
00186   }
00187 
00188   if (highFlag)
00189   {
00190     s.append(" ");
00191     s.append(QString::number(high, 'g'));
00192   }
00193   
00194   if (lowFlag)
00195   {
00196     s.append(" ");
00197     s.append(QString::number(low, 'g'));
00198   }
00199 
00200   if (closeFlag)
00201   {
00202     s.append(" ");
00203     s.append(QString::number(close, 'g'));
00204   }
00205 
00206   if (volumeFlag)
00207   {
00208     s.append(" ");
00209     s.append(QString::number(volume, 'g'));
00210   }
00211 
00212   if (oiFlag)
00213   {
00214     s.append(" ");
00215     s.append(QString::number(oi));
00216   }
00217 }
00218 
00219 bool Bar::getTickFlag ()
00220 {
00221   return tickFlag;
00222 }
00223 
00224 void Bar::setTickFlag (bool d)
00225 {
00226   tickFlag = d;
00227 }
00228 
00229 bool Bar::getEmptyFlag ()
00230 {
00231   return emptyFlag;
00232 }
00233 
00234 void Bar::setEmptyFlag (bool d)
00235 {
00236   emptyFlag = d;
00237 }
00238 
00239 void Bar::getDateString (bool sepFlag, QString &d)
00240 {
00241   if (sepFlag)
00242     d = date.toString("yyyy-MM-dd");
00243   else
00244     d = date.toString("yyyyMMdd");
00245 }
00246 
00247 void Bar::getDateTimeString (bool sepFlag, QString &d)
00248 {
00249   QString s;
00250   getDateString(sepFlag, s);
00251   
00252   if (sepFlag)
00253     s.append(" ");
00254     
00255   QString s2;
00256   getTimeString(sepFlag, s2);
00257   s.append(s2);
00258   d = s;
00259 }
00260 
00261 void Bar::getTimeString (bool sepFlag, QString &d)
00262 {
00263   if (sepFlag)
00264     d = date.toString("hh:mm:ss");
00265   else
00266     d = date.toString("hhmmss");
00267 }
00268 
00269 void Bar::clear ()
00270 {
00271   date = QDateTime::currentDateTime();
00272   date.setTime(QTime(0,0,0,0));
00273 
00274   tickFlag = FALSE;
00275   open = 0;
00276   high = 0;
00277   low = 0;
00278   close = 0;
00279   volume = 0;
00280   oi = 0;
00281   openFlag = FALSE;
00282   highFlag = FALSE;
00283   lowFlag = FALSE;
00284   closeFlag = FALSE;
00285   volumeFlag = FALSE;
00286   oiFlag = FALSE;
00287   emptyFlag = TRUE;
00288 }
00289 
00290 bool Bar::verify ()
00291 {
00292   bool rc = TRUE;
00293 
00294   if (open == 0 || high == 0 || low == 0 || close == 0)
00295     return rc;
00296 
00297   if (open > high)
00298     open = high;
00299   if (open < low)
00300     open = low;
00301         
00302   if (close > high)
00303     close = high;
00304   if (close < low)
00305     close = low;
00306 
00307   rc = FALSE;
00308   return rc;
00309 }
00310