lib/THERM.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 /* The "Market Thermometer" indicator is described in
00023    Dr. Alexander Elder's book _Come Into My Trading Room_, p.162 */
00024 
00025 #include "THERM.h"
00026 #include "PrefDialog.h"
00027 #include <qdict.h>
00028 #include <qobject.h>
00029 #include <math.h>
00030 
00031 THERM::THERM ()
00032 {
00033   pluginName = "THERM";
00034   helpFile = "therm.html";
00035   
00036   upColorLabel = "upColor";
00037   downColorLabel = "downColor";
00038   threshColorLabel = "threshColor";
00039   labelLabel = "label";
00040   thresholdLabel = "threshold";
00041   smoothingLabel = "smoothing";
00042   smoothTypeLabel = "smoothType";
00043   maColorLabel = "maColor";
00044   maLineTypeLabel = "maLineType";
00045   maPeriodLabel = "maPeriod";
00046   maLabelLabel = "maLabel";
00047   maTypeLabel = "maType";
00048   pluginLabel = "plugin";
00049 
00050   // format1: MA_TYPE, MA_PERIOD, THRESHOLD, SMOOTHING_TYPE, SMOOTHING_PERIOD
00051   formatList.append(FormatMAType);
00052   formatList.append(FormatInteger);
00053   formatList.append(FormatDouble);
00054   formatList.append(FormatMAType);
00055   formatList.append(FormatInteger);
00056 
00057   setDefaults();
00058 }
00059 
00060 THERM::~THERM ()
00061 {
00062 }
00063 
00064 void THERM::setDefaults ()
00065 {
00066   downColor.setNamedColor("green");
00067   upColor.setNamedColor("magenta");
00068   threshColor.setNamedColor("red");
00069   maColor.setNamedColor("yellow");
00070   lineType = PlotLine::HistogramBar;
00071   maLineType = PlotLine::Line;
00072   label = pluginName;
00073   maLabel = QObject::tr("THERM MA");
00074   threshold = 3;
00075   smoothing = 2;
00076   maPeriod = 22;
00077   maType = 0;
00078   smoothType = 0;
00079 }
00080 
00081 Indicator * THERM::calculate ()
00082 {
00083   Indicator *output = new Indicator;
00084   output->setDateFlag(dateFlag);
00085   output->setLogScale(logScale);
00086 
00087   QPtrList<PlotLine> pll;
00088   pll.setAutoDelete(FALSE);
00089   getTHERM(pll);
00090 
00091   int loop;
00092   for (loop = 0; loop < (int) pll.count(); loop++)
00093     output->addLine(pll.at(loop));
00094 
00095   return output;
00096 }
00097 
00098 void THERM::getTHERM (QPtrList<PlotLine> &pll)
00099 {
00100   PlotLine *therm = new PlotLine();
00101   int loop;
00102   double thermometer = 0;
00103   for (loop = 1; loop < (int) data->count(); loop++)
00104   {
00105     double high = fabs(data->getHigh(loop) - data->getHigh(loop - 1));
00106     double lo = fabs(data->getLow(loop - 1) - data->getLow(loop));
00107     
00108     if (high > lo)
00109       thermometer = high;
00110     else
00111       thermometer = lo;
00112 
00113     therm->append(thermometer);
00114   }
00115 
00116   if (smoothing > 1)
00117   {
00118     PlotLine *ma = getMA(therm, smoothType, smoothing);
00119     pll.append(ma);
00120     delete therm;
00121     therm = ma;
00122   }
00123   else
00124     pll.append(therm);
00125 
00126   PlotLine *therm_ma = getMA(therm, maType, maPeriod);
00127   therm_ma->setColor(maColor);
00128   therm_ma->setType(maLineType);
00129   therm_ma->setLabel(maLabel);
00130   pll.append(therm_ma);
00131 
00132   // assign the therm colors
00133 
00134   therm->setColorFlag(TRUE);
00135   therm->setType(lineType);
00136   therm->setLabel(label);
00137 
00138   int thermLoop = therm->getSize() - 1;
00139   int maLoop = therm_ma->getSize() - 1;
00140   while (thermLoop > -1)
00141   {
00142     if (maLoop > -1)
00143     {
00144       double thrm = therm->getData(thermLoop);
00145       double thrmma = therm_ma->getData(maLoop);
00146 
00147       if (thrm > (thrmma * threshold))
00148         therm->setColorBar(thermLoop, threshColor);
00149       else
00150       {
00151         if (thrm > thrmma)
00152           therm->setColorBar(thermLoop, upColor);
00153         else
00154           therm->setColorBar(thermLoop, downColor);
00155       }
00156     }
00157     else
00158       therm->setColorBar(thermLoop, downColor);
00159 
00160     thermLoop--;
00161     maLoop--;
00162   }
00163 }
00164 
00165 int THERM::indicatorPrefDialog (QWidget *w)
00166 {
00167   QString pl = QObject::tr("THERM Parms");
00168   QString pl2 = QObject::tr("MA Parms");
00169   QString cal = QObject::tr("Color Above MA");
00170   QString cbl = QObject::tr("Color Below MA");
00171   QString ctl = QObject::tr("Color Threshold");
00172   QString ll = QObject::tr("Label");
00173   QString tl = QObject::tr("Threshold");
00174   QString sl = QObject::tr("Smoothing");
00175   QString stl = QObject::tr("Smoothing Type");
00176   QString mcl = QObject::tr("MA Color");
00177   QString mltl = QObject::tr("MA Line Type");
00178   QString mll = QObject::tr("MA Label");
00179   QString mpl = QObject::tr("MA Period");
00180   QString mtl = QObject::tr("MA Type");
00181 
00182   PrefDialog *dialog = new PrefDialog(w);
00183   dialog->setCaption(QObject::tr("THERM Indicator"));
00184   dialog->setHelpFile(helpFile);
00185 
00186   dialog->createPage (pl);
00187   dialog->addColorItem(cal, pl, upColor);
00188   dialog->addColorItem(cbl, pl, downColor);
00189   dialog->addColorItem(ctl, pl, threshColor);
00190   dialog->addTextItem(ll, pl, label);
00191   dialog->addDoubleItem(tl, pl, threshold, 1, 99999999);
00192   dialog->addIntItem(sl, pl, smoothing, 0, 99999999);
00193   QStringList l;
00194   getMATypes(l);
00195   dialog->addComboItem(stl, pl, l, smoothType);
00196   
00197   dialog->createPage (pl2);
00198   dialog->addColorItem(mcl, pl2, maColor);
00199   dialog->addComboItem(mltl, pl2, lineTypes, maLineType);
00200   dialog->addTextItem(mll, pl2, maLabel);
00201   dialog->addIntItem(mpl, pl2, maPeriod, 0, 99999999);
00202   dialog->addComboItem(mtl, pl2, l, maType);
00203   
00204   int rc = dialog->exec();
00205   
00206   if (rc == QDialog::Accepted)
00207   {
00208     dialog->getColor(cal, upColor);
00209     dialog->getColor(cbl, downColor);
00210     dialog->getColor(ctl, threshColor);
00211     dialog->getText(ll, label);
00212     threshold = dialog->getDouble(tl);
00213     smoothing = dialog->getInt(sl);
00214     smoothType = dialog->getComboIndex(stl);
00215     
00216     dialog->getColor(mcl, maColor);
00217     maLineType = (PlotLine::LineType) dialog->getComboIndex(mltl);
00218     dialog->getText(mll, maLabel);
00219     maPeriod = dialog->getInt(mpl);
00220     maType = dialog->getComboIndex(mtl);
00221     rc = TRUE;
00222   }
00223   else
00224     rc = FALSE;
00225   
00226   delete dialog;
00227   return rc;
00228 }
00229 
00230 void THERM::setIndicatorSettings (Setting &dict)
00231 {
00232   setDefaults();
00233   
00234   if (! dict.count())
00235     return;
00236   
00237   QString s;
00238   dict.getData(upColorLabel, s);
00239   if (s.length())
00240     upColor.setNamedColor(s);
00241     
00242   dict.getData(downColorLabel, s);
00243   if (s.length())
00244     downColor.setNamedColor(s);
00245   
00246   dict.getData(threshColorLabel, s);
00247   if (s.length())
00248     threshColor.setNamedColor(s);
00249   
00250   dict.getData(maColorLabel, s);
00251   if (s.length())
00252     maColor.setNamedColor(s);
00253     
00254   dict.getData(labelLabel, s);
00255   if (s.length())
00256     label = s;
00257     
00258   dict.getData(thresholdLabel, s);
00259   if (s.length())
00260     threshold = s.toFloat();
00261   
00262   dict.getData(smoothingLabel, s);
00263   if (s.length())
00264     smoothing = s.toInt();
00265   
00266   dict.getData(smoothTypeLabel, s);
00267   if (s.length())
00268     smoothType = s.toInt();
00269   
00270   dict.getData(maLineTypeLabel, s);
00271   if (s.length())
00272     maLineType = (PlotLine::LineType) s.toInt();
00273 
00274   dict.getData(maLabelLabel, s);
00275   if (s.length())
00276     maLabel = s;
00277     
00278   dict.getData(maPeriodLabel, s);
00279   if (s.length())
00280     maPeriod = s.toInt();
00281 
00282   dict.getData(maTypeLabel, s);
00283   if (s.length())
00284     maType = s.toInt();
00285 }
00286 
00287 void THERM::getIndicatorSettings (Setting &dict)
00288 {
00289   QString ts = upColor.name();
00290   dict.setData(upColorLabel, ts);
00291   ts = downColor.name();
00292   dict.setData(downColorLabel, ts);
00293   ts = threshColor.name();
00294   dict.setData(threshColorLabel, ts);
00295   dict.setData(labelLabel, label);
00296   ts = QString::number(threshold);
00297   dict.setData(thresholdLabel, ts);
00298   ts = QString::number(smoothing);
00299   dict.setData(smoothingLabel, ts);
00300   ts = QString::number(smoothType);
00301   dict.setData(smoothTypeLabel, ts);
00302   
00303   ts = maColor.name();
00304   dict.setData(maColorLabel, ts);
00305   ts = QString::number(maLineType);
00306   dict.setData(maLineTypeLabel, ts);
00307   ts = QString::number(maPeriod);
00308   dict.setData(maPeriodLabel, ts);
00309   dict.setData(maLabelLabel, maLabel);
00310   ts = QString::number(maType);
00311   dict.setData(maTypeLabel, ts);
00312   dict.setData(pluginLabel, pluginName);
00313 }
00314 
00315 PlotLine * THERM::calculateCustom (QString &p, QPtrList<PlotLine> &d)
00316 {
00317   // format1: MA_TYPE, MA_PERIOD, THRESHOLD, SMOOTHING_TYPE, SMOOTHING_PERIOD
00318 
00319   if (checkFormat(p, d, 5, 5))
00320     return 0;
00321 
00322   QStringList mal;
00323   getMATypes(mal);
00324   maType = mal.findIndex(formatStringList[0]);
00325   maPeriod = formatStringList[1].toInt();
00326   threshold = formatStringList[2].toDouble();
00327   smoothType = mal.findIndex(formatStringList[3]);
00328   smoothing = formatStringList[4].toInt();
00329 
00330   QPtrList<PlotLine> pll;
00331   pll.setAutoDelete(FALSE);
00332   getTHERM(pll);
00333 
00334   int loop;
00335   for (loop = pll.count() - 1; loop > 0; loop--)
00336     pll.remove(loop);
00337 
00338   return pll.at(0);
00339 }
00340 
00341 void THERM::formatDialog (QStringList &, QString &rv, QString &rs)
00342 {
00343   rs.truncate(0);
00344   rv.truncate(0);
00345   QString pl = QObject::tr("Parms");
00346   QString vnl = QObject::tr("Variable Name");
00347   QString tl = QObject::tr("Threshold");
00348   QString sl = QObject::tr("Smoothing");
00349   QString stl = QObject::tr("Smoothing Type");
00350   QString mpl = QObject::tr("MA Period");
00351   QString mtl = QObject::tr("MA Type");
00352   PrefDialog *dialog = new PrefDialog(0);
00353   dialog->setCaption(QObject::tr("THERM Format"));
00354   dialog->createPage (pl);
00355   dialog->setHelpFile(helpFile);
00356 
00357   QString s;
00358   QStringList l;
00359   getMATypes(l);
00360   dialog->addTextItem(vnl, pl, s);
00361   dialog->addComboItem(mtl, pl, l, maType);
00362   dialog->addIntItem(mpl, pl, maPeriod, 0, 99999999);
00363   dialog->addDoubleItem(tl, pl, threshold, 1, 99999999);
00364   dialog->addComboItem(stl, pl, l, smoothType);
00365   dialog->addIntItem(sl, pl, smoothing, 0, 99999999);
00366 
00367   int rc = dialog->exec();
00368   
00369   if (rc == QDialog::Accepted)
00370   {
00371     dialog->getText(vnl, rv);
00372 
00373     dialog->getCombo(mtl, rs);
00374 
00375     int t = dialog->getInt(mpl);
00376     rs.append("," + QString::number(t));
00377 
00378     double d = dialog->getDouble(tl);
00379     rs.append("," + QString::number(d));
00380 
00381     dialog->getCombo(stl, s);
00382     rs.append("," + s);
00383 
00384     t = dialog->getInt(sl);
00385     rs.append("," + QString::number(t));
00386   }
00387 
00388   delete dialog;
00389 }
00390 
00391 /* Alert Notes
00392 
00393 1) enter when therm falls below MA
00394 2) exit when threshold is triggered
00395 3) explosive move expected when therm stays below MA for 5-7 days
00396 
00397 */
00398 
00399