lib/VOL.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 "VOL.h"
00023 #include "PrefDialog.h"
00024 #include <qdict.h>
00025 #include <qobject.h>
00026 
00027 VOL::VOL ()
00028 {
00029   pluginName = "VOL";
00030   helpFile = "vol.html";
00031 
00032   methodList.append("VOL");
00033   methodList.append("NVI");
00034   methodList.append("PVI");
00035   methodList.append("PVT");
00036   methodList.sort();
00037 
00038   upColorLabel = "upColor";
00039   downColorLabel = "downColor";
00040   vtColorLabel = "vtColor";
00041   volLabelLabel = "volLabel";
00042   volLineTypeLabel = "volLineType";
00043   vtLineTypeLabel = "vtLineType";
00044   maColorLabel = "maColor";
00045   maPeriodLabel = "maPeriod";
00046   maLabelLabel = "maLabel";
00047   maLineTypeLabel = "maLineType";
00048   maTypeLabel = "maType";
00049   labelLabel = "label";
00050   methodLabel = "method";
00051   pluginLabel = "plugin";
00052 
00053   formatList.append(FormatString);
00054   
00055   setDefaults();
00056 }
00057 
00058 VOL::~VOL ()
00059 {
00060 }
00061 
00062 void VOL::setDefaults ()
00063 {
00064   upColor.setNamedColor("green");
00065   downColor.setNamedColor("red");
00066   maColor.setNamedColor("yellow");
00067   vtColor.setNamedColor("red");
00068   volLineType = PlotLine::HistogramBar;
00069   maLineType = PlotLine::Line;
00070   vtLineType = PlotLine::Line;
00071   volLabel = "VOL";
00072   label = volLabel;
00073   maLabel = "MAVol";
00074   period = 0;
00075   maType = 1;
00076   method = "VOL";
00077 }
00078 
00079 Indicator * VOL::calculate ()
00080 {
00081   Indicator *output = new Indicator;
00082   output->setDateFlag(dateFlag);
00083   output->setLogScale(logScale);
00084 
00085   PlotLine *line = 0;
00086   if (! method.compare("VOL"))
00087     line = calculateVOL();
00088   else
00089   {
00090     if (! method.compare("NVI"))
00091       line = calculateNVI();
00092     else
00093     {
00094       if (! method.compare("PVI"))
00095         line = calculatePVI();
00096       else
00097         line = calculatePVT();
00098     }
00099   }
00100 
00101   output->addLine(line);
00102 
00103   if (period < 1)
00104     return output;
00105 
00106   PlotLine *ma = getMA(line, maType, period);
00107   ma->setColor(maColor);
00108   ma->setType(maLineType);
00109   ma->setLabel(maLabel);
00110   output->addLine(ma);
00111 
00112   return output;
00113 }
00114 
00115 PlotLine * VOL::calculateVOL ()
00116 {
00117   PlotLine *pl = data->getInput(BarData::Volume);
00118   pl->setType(volLineType);
00119   pl->setLabel(volLabel);
00120   pl->setColorFlag(TRUE);
00121 
00122   int loop;
00123   for (loop = 0; loop < (int) data->count(); loop++)
00124   {
00125     if (loop > 0)
00126     {
00127       if (data->getClose(loop) > data->getClose(loop - 1))
00128         pl->setColorBar(loop, upColor);
00129       else
00130         pl->setColorBar(loop, downColor);
00131     }
00132     else
00133       pl->setColorBar(loop, upColor);
00134   }
00135 
00136   return pl;
00137 }
00138 
00139 PlotLine * VOL::calculateNVI ()
00140 {
00141   PlotLine *nvi = new PlotLine();
00142   nvi->setColor(vtColor);
00143   nvi->setType(vtLineType);
00144   nvi->setLabel(label);
00145 
00146   int loop;
00147   double nv = 1000;
00148   for (loop = 1; loop < (int) data->count(); loop++)
00149   {
00150     double volume = data->getVolume(loop);
00151     double close = data->getClose(loop);
00152     double yvolume = data->getVolume(loop - 1);
00153     double yclose = data->getClose(loop - 1);
00154 
00155     if (volume < yvolume)
00156       nv = nv + ((close - yclose) / yclose) * nv;
00157 
00158     nvi->append(nv);
00159   }
00160   
00161   return nvi;
00162 }
00163 
00164 PlotLine * VOL::calculatePVI ()
00165 {
00166   PlotLine *pvi = new PlotLine();
00167   pvi->setColor(vtColor);
00168   pvi->setType(vtLineType);
00169   pvi->setLabel(label);
00170 
00171   int loop = 0;
00172   double pv = 1000;
00173   for (loop = 1; loop < (int) data->count(); loop++)
00174   {
00175     double volume = data->getVolume(loop);
00176     double close = data->getClose(loop);
00177     double yvolume = data->getVolume(loop - 1);
00178     double yclose = data->getClose(loop - 1);
00179 
00180     if (volume > yvolume)
00181       pv = pv + ((close - yclose) / yclose) * pv;
00182 
00183     pvi->append(pv);
00184   }
00185 
00186   return pvi;
00187 }
00188 
00189 PlotLine * VOL::calculatePVT ()
00190 {
00191   PlotLine *pvt = new PlotLine();
00192   pvt->setColor(vtColor);
00193   pvt->setType(vtLineType);
00194   pvt->setLabel(label);
00195 
00196   int loop = 0;
00197   double pv = 0;
00198   for (loop = 1; loop < (int) data->count(); loop++)
00199   {
00200     double close = data->getClose(loop);
00201     double volume = data->getVolume(loop);
00202     double yclose = data->getClose(loop - 1);
00203 
00204     pv = pv + (((close - yclose) / yclose) * volume);
00205     pvt->append(pv);
00206   }
00207 
00208   return pvt;
00209 }
00210 
00211 int VOL::indicatorPrefDialog (QWidget *w)
00212 {
00213   QString pl = QObject::tr("VOL");
00214   QString ml = QObject::tr("Method");
00215   QString ucl = QObject::tr("Up Color");
00216   QString dcl = QObject::tr("Down Color");
00217   QString cl = QObject::tr("Color");
00218   QString vll = QObject::tr("VOL Label");
00219   QString vltl = QObject::tr("VOL Line Type");
00220   QString ltl = QObject::tr("Line Type");
00221   QString pl2 = QObject::tr("MA");
00222   QString mcl = QObject::tr("MA Color");
00223   QString mpl = QObject::tr("MA Period");
00224   QString mll = QObject::tr("MA Label");
00225   QString mltl = QObject::tr("MA Line Type");
00226   QString mtl = QObject::tr("MA Type");
00227 
00228   PrefDialog *dialog = new PrefDialog(w);
00229   dialog->setCaption(QObject::tr("VOL Indicator"));
00230   dialog->setHelpFile(helpFile);
00231 
00232   dialog->createPage (pl);
00233   dialog->addComboItem(ml, pl, methodList, method);
00234   dialog->addColorItem(ucl, pl, upColor);
00235   dialog->addColorItem(dcl, pl, downColor);
00236   dialog->addColorItem(cl, pl, vtColor);
00237   dialog->addTextItem(vll, pl, volLabel);
00238   dialog->addComboItem(vltl, pl, lineTypes, volLineType);
00239   dialog->addComboItem(ltl, pl, lineTypes, vtLineType);
00240   
00241   dialog->createPage (pl2);
00242   dialog->addColorItem(mcl, pl2, maColor);
00243   dialog->addIntItem(mpl, pl2, period, 0, 99999999);
00244   dialog->addTextItem(mll, pl2, maLabel);
00245   dialog->addComboItem(mltl, pl2, lineTypes, maLineType);
00246   QStringList l;
00247   getMATypes(l);
00248   dialog->addComboItem(mtl, pl2, l, maType);
00249   
00250   int rc = dialog->exec();
00251   
00252   if (rc == QDialog::Accepted)
00253   {
00254     dialog->getCombo(ml, method);
00255     dialog->getColor(ucl, upColor);
00256     dialog->getColor(dcl, downColor);
00257     dialog->getColor(cl, vtColor);
00258     dialog->getText(vll, volLabel);
00259     label = volLabel;
00260     volLineType = (PlotLine::LineType) dialog->getComboIndex(vltl);
00261     vtLineType = (PlotLine::LineType) dialog->getComboIndex(ltl);
00262 
00263     dialog->getColor(mcl, maColor);
00264     period = dialog->getInt(mpl);
00265     dialog->getText(mll, maLabel);
00266     maLineType = (PlotLine::LineType) dialog->getComboIndex(mltl);
00267     maType = dialog->getComboIndex(mtl);
00268     
00269     rc = TRUE;
00270   }
00271   else
00272     rc = FALSE;
00273   
00274   delete dialog;
00275   return rc;
00276 }
00277 
00278 void VOL::setIndicatorSettings (Setting &dict)
00279 {
00280   setDefaults();
00281   
00282   if (! dict.count())
00283     return;
00284   
00285   QString s;
00286   dict.getData(upColorLabel, s);
00287   if (s.length())
00288     upColor.setNamedColor(s);
00289     
00290   dict.getData(downColorLabel, s);
00291   if (s.length())
00292     downColor.setNamedColor(s);
00293     
00294   dict.getData(vtColorLabel, s);
00295   if (s.length())
00296     vtColor.setNamedColor(s);
00297 
00298   dict.getData(volLabelLabel, s);
00299   if (s.length())
00300     volLabel = s;
00301         
00302   dict.getData(volLineTypeLabel, s);
00303   if (s.length())
00304     volLineType = (PlotLine::LineType) s.toInt();
00305         
00306   dict.getData(vtLineTypeLabel, s);
00307   if (s.length())
00308     vtLineType = (PlotLine::LineType) s.toInt();
00309 
00310   dict.getData(maColorLabel, s);
00311   if (s.length())
00312     maColor.setNamedColor(s);
00313         
00314   dict.getData(maPeriodLabel, s);
00315   if (s.length())
00316     period = s.toInt();
00317         
00318   dict.getData(maLabelLabel, s);
00319   if (s.length())
00320     maLabel = s;
00321         
00322   dict.getData(maLineTypeLabel, s);
00323   if (s.length())
00324     maLineType = (PlotLine::LineType) s.toInt();
00325         
00326   dict.getData(maTypeLabel, s);
00327   if (s.length())
00328     maType = s.toInt();
00329 
00330   dict.getData(labelLabel, s);
00331   if (s.length())
00332     label = s;
00333 
00334   dict.getData(methodLabel, s);
00335   if (s.length())
00336     method = s;
00337 }
00338 
00339 void VOL::getIndicatorSettings (Setting &dict)
00340 {
00341   QString ts = upColor.name();
00342   dict.setData(upColorLabel, ts);
00343   ts = downColor.name();
00344   dict.setData(downColorLabel, ts);
00345   ts = vtColor.name();
00346   dict.setData(vtColorLabel, ts);
00347   dict.setData(volLabelLabel, volLabel);
00348   ts = QString::number(volLineType);
00349   dict.setData(volLineTypeLabel, ts);
00350   ts = QString::number(vtLineType);
00351   dict.setData(vtLineTypeLabel, ts);
00352   ts = maColor.name();
00353   dict.setData(maColorLabel, ts);
00354   ts = QString::number(period);
00355   dict.setData(maPeriodLabel, ts);
00356   dict.setData(maLabelLabel, maLabel);
00357   ts = QString::number(maLineType);
00358   dict.setData(maLineTypeLabel, ts);
00359   ts = QString::number(maType);
00360   dict.setData(maTypeLabel, ts);
00361   dict.setData(labelLabel, label);
00362   dict.setData(methodLabel, method);
00363   dict.setData(pluginLabel, pluginName);
00364 }
00365 
00366 PlotLine * VOL::calculateCustom (QString &p, QPtrList<PlotLine> &d)
00367 {
00368   // format1: METHOD
00369 
00370   if (checkFormat(p, d, 1, 1))
00371     return 0;
00372 
00373   method = formatStringList[0];
00374   if (methodList.findIndex(method) == -1)
00375   {
00376     qDebug("VOL::calculateCustom: invalid METHOD parm");
00377     return 0;
00378   }
00379 
00380   PlotLine *line = 0;
00381   if (! method.compare("VOL"))
00382     line = calculateVOL();
00383   else
00384   {
00385     if (! method.compare("NVI"))
00386       line = calculateNVI();
00387     else
00388     {
00389       if (! method.compare("PVI"))
00390         line = calculatePVI();
00391       else
00392         line = calculatePVT();
00393     }
00394   }
00395   
00396   return line;
00397 }
00398 
00399 void VOL::formatDialog (QStringList &, QString &rv, QString &rs)
00400 {
00401   rs.truncate(0);
00402   rv.truncate(0);
00403   QString pl = QObject::tr("Parms");
00404   QString vnl = QObject::tr("Variable Name");
00405   QString ml = QObject::tr("Method");
00406   PrefDialog *dialog = new PrefDialog(0);
00407   dialog->setCaption(QObject::tr("VOL Format"));
00408   dialog->createPage (pl);
00409   dialog->setHelpFile(helpFile);
00410 
00411   // format1: METHOD
00412 
00413   QString s;
00414   dialog->addTextItem(vnl, pl, s);
00415   dialog->addComboItem(ml, pl, methodList, method);
00416 
00417   int rc = dialog->exec();
00418   
00419   if (rc == QDialog::Accepted)
00420   {
00421     dialog->getText(vnl, rv);
00422     dialog->getCombo(ml, rs);
00423   }
00424 
00425   delete dialog;
00426 }
00427