lib/FI.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 "FI.h"
00023 #include "PrefDialog.h"
00024 #include <qdict.h>
00025 #include <qobject.h>
00026 
00027 FI::FI ()
00028 {
00029   pluginName = "FI";
00030   helpFile = "fi.html";
00031 
00032   colorLabel = "color";
00033   lineTypeLabel = "lineType";
00034   smoothingLabel = "smoothing";
00035   labelLabel = "label";
00036   maTypeLabel = "maType";
00037   pluginLabel = "plugin";
00038 
00039   formatList.append(FormatMAType);
00040   formatList.append(FormatInteger);
00041   
00042   setDefaults();
00043 }
00044 
00045 FI::~FI ()
00046 {
00047 }
00048 
00049 void FI::setDefaults ()
00050 {
00051   color.setNamedColor("orange");
00052   lineType = PlotLine::HistogramBar;
00053   label = pluginName;
00054   smoothing = 2;
00055   maType = 0;
00056 }
00057 
00058 Indicator * FI::calculate ()
00059 {
00060   Indicator *output = new Indicator;
00061   output->setDateFlag(dateFlag);
00062   output->setLogScale(logScale);
00063 
00064   PlotLine *fi = getFI();
00065   if (smoothing > 1)
00066   {
00067     PlotLine *ma = getMA(fi, maType, smoothing);
00068     ma->setColor(color);
00069     ma->setType(lineType);
00070     ma->setLabel(label);
00071     output->addLine(ma);
00072     delete fi;
00073   }
00074   else
00075   {
00076     fi->setColor(color);
00077     fi->setType(lineType);
00078     fi->setLabel(label);
00079     output->addLine(fi);
00080   }
00081   return output;
00082 }
00083 
00084 PlotLine * FI::getFI ()
00085 {
00086   PlotLine *fi = new PlotLine();
00087   int loop;
00088   double force = 0;
00089   for (loop = 1; loop < (int) data->count(); loop++)
00090   {
00091     double cdiff = data->getClose(loop) - data->getClose(loop - 1);
00092     force = data->getVolume(loop) * cdiff;
00093     fi->append(force);
00094   }
00095 
00096   return fi;
00097 }
00098 
00099 int FI::indicatorPrefDialog (QWidget *w)
00100 {
00101   QString pl = QObject::tr("Parms");
00102   QString cl = QObject::tr("Color");
00103   QString ll = QObject::tr("Label");
00104   QString ltl = QObject::tr("Line Type");
00105   QString sl = QObject::tr("Smoothing");
00106   QString stl = QObject::tr("Smoothing Type");
00107 
00108   PrefDialog *dialog = new PrefDialog(w);
00109   dialog->setCaption(QObject::tr("FI Indicator"));
00110   dialog->createPage (pl);
00111   dialog->setHelpFile(helpFile);
00112   dialog->addColorItem(cl, pl, color);
00113   dialog->addComboItem(ltl, pl, lineTypes, lineType);
00114   dialog->addTextItem(ll, pl, label);
00115   dialog->addIntItem(sl, pl, smoothing, 0, 99999999);
00116   QStringList l;
00117   getMATypes(l);
00118   dialog->addComboItem(stl, pl, l, maType);
00119   
00120   int rc = dialog->exec();
00121   
00122   if (rc == QDialog::Accepted)
00123   {
00124     dialog->getColor(cl, color);
00125     lineType = (PlotLine::LineType) dialog->getComboIndex(ltl);
00126     smoothing = dialog->getInt(sl);
00127     dialog->getText(ll, label);
00128     maType = dialog->getComboIndex(stl);
00129     rc = TRUE;
00130   }
00131   else
00132     rc = FALSE;
00133   
00134   delete dialog;
00135   return rc;
00136 }
00137 
00138 void FI::setIndicatorSettings (Setting &dict)
00139 {
00140   setDefaults();
00141   
00142   if (! dict.count())
00143     return;
00144   
00145   QString s;
00146   dict.getData(colorLabel, s);
00147   if (s.length())
00148     color.setNamedColor(s);
00149     
00150   dict.getData(lineTypeLabel, s);
00151   if (s.length())
00152     lineType = (PlotLine::LineType) s.toInt();
00153 
00154   dict.getData(smoothingLabel, s);
00155   if (s.length())
00156     smoothing = s.toInt();
00157 
00158   dict.getData(labelLabel, s);
00159   if (s.length())
00160     label = s;
00161       
00162   dict.getData(maTypeLabel, s);
00163   if (s.length())
00164     maType = s.toInt();
00165 }
00166 
00167 void FI::getIndicatorSettings (Setting &dict)
00168 {
00169   QString ts = color.name();
00170   dict.setData(colorLabel, ts);
00171   ts = QString::number(lineType);
00172   dict.setData(lineTypeLabel, ts);
00173   ts = QString::number(smoothing);
00174   dict.setData(smoothingLabel, ts);
00175   dict.setData(labelLabel, label);
00176   ts = QString::number(maType);
00177   dict.setData(maTypeLabel, ts);
00178   dict.setData(pluginLabel, pluginName);
00179 }
00180 
00181 PlotLine * FI::calculateCustom (QString &p, QPtrList<PlotLine> &d)
00182 {
00183   // format1: MA_TYPE, SMOOTHING
00184 
00185   if (checkFormat(p, d, 2, 2))
00186     return 0;
00187 
00188   QStringList mal;
00189   getMATypes(mal);
00190   maType = mal.findIndex(formatStringList[0]);
00191   smoothing = formatStringList[1].toInt();
00192 
00193   PlotLine *fi = getFI();
00194   PlotLine *ma = getMA(fi, maType, smoothing);
00195   delete fi;
00196 
00197   return ma;
00198 }
00199 
00200 void FI::formatDialog (QStringList &, QString &rv, QString &rs)
00201 {
00202   rs.truncate(0);
00203   rv.truncate(0);
00204   QString pl = QObject::tr("Parms");
00205   QString vl = QObject::tr("Variable Name");
00206   QString mtl = QObject::tr("MA Type");
00207   QString ppl = QObject::tr("Period");
00208   PrefDialog *dialog = new PrefDialog(0);
00209   dialog->setCaption(QObject::tr("FI Format"));
00210   dialog->createPage (pl);
00211   dialog->setHelpFile(helpFile);
00212 
00213   QString s;
00214   QStringList mal;
00215   getMATypes(mal);
00216   dialog->addTextItem(vl, pl, s);
00217   dialog->addComboItem(mtl, pl, mal, maType);
00218   dialog->addIntItem(ppl, pl, smoothing, 1, 999999);
00219 
00220   int rc = dialog->exec();
00221   
00222   if (rc == QDialog::Accepted)
00223   {
00224     dialog->getText(vl, rv);
00225     dialog->getCombo(mtl, rs);
00226     int t = dialog->getInt(ppl);
00227     rs.append("," + QString::number(t));
00228   }
00229 
00230   delete dialog;
00231 }
00232