lib/IndicatorPlugin.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 "IndicatorPlugin.h"
00023 #include "TALIB.h"
00024 #include <qfile.h>
00025 #include <qtextstream.h>
00026 
00027 IndicatorPlugin::IndicatorPlugin()
00028 {
00029   saveFlag = FALSE;
00030   dateFlag = FALSE;
00031   logScale = FALSE;
00032   
00033   PlotLine pl;
00034   pl.getLineTypes(lineTypes);
00035 
00036   BarData it(pluginName);
00037   it.getInputFields(inputTypeList);
00038 
00039   opList.append("EQ");
00040   opList.append("LT");
00041   opList.append("LTEQ");
00042   opList.append("GT");
00043   opList.append("GTEQ");
00044   opList.append("AND");
00045   opList.append("OR");
00046   opList.append("XOR");
00047 
00048   maList.append("SMA"); //    TA_MAType_SMA       =0,
00049   maList.append("EMA"); //    TA_MAType_EMA       =1,
00050   maList.append("WMA"); //    TA_MAType_WMA       =2,
00051   maList.append("DEMA"); //    TA_MAType_DEMA      =3,
00052   maList.append("TEMA"); //    TA_MAType_TEMA      =4,
00053   maList.append("TRIMA"); //    TA_MAType_TRIMA     =5,
00054   maList.append("KAMA"); //    TA_MAType_KAMA      =6,
00055   maList.append("MAMA"); //    TA_MAType_MAMA      =7,
00056   maList.append("T3"); //    TA_MAType_T3        =8
00057   maList.append("Wilder");
00058 }
00059 
00060 IndicatorPlugin::~IndicatorPlugin()
00061 {
00062 }
00063 
00064 void IndicatorPlugin::setIndicatorInput (BarData *d)
00065 {
00066   data = d;
00067 }
00068 
00069 void IndicatorPlugin::loadFile (QString &file, Setting &dict)
00070 {
00071   QFile f(file);
00072   if (! f.open(IO_ReadOnly))
00073   {
00074     qDebug("IndicatorPlugin:can't read file %s", file.latin1());
00075     return;
00076   }
00077   QTextStream stream(&f);
00078   
00079   while(stream.atEnd() == 0)
00080   {
00081     QString s = stream.readLine();
00082     s = s.stripWhiteSpace();
00083     if (! s.length())
00084       continue;
00085       
00086     QStringList l = QStringList::split("=", s, FALSE);
00087     
00088     if (l.count() < 2)
00089       continue;
00090       
00091     if (l.count() > 2)
00092     {
00093       QString k = l[0];
00094       s = s.remove(0, k.length() + 1);
00095       dict.setData(k, s);  
00096     }
00097     else
00098       dict.setData(l[0], l[1]);  
00099   }
00100   
00101   f.close();
00102 }
00103 
00104 void IndicatorPlugin::saveFile (QString &file, Setting &dict)
00105 {
00106   QFile f(file);
00107   if (! f.open(IO_WriteOnly))
00108   {
00109     qDebug("IndicatorPlugin:can't save file %s", file.latin1());
00110     return;
00111   }
00112   QTextStream stream(&f);
00113   
00114   QStringList key;
00115   dict.getKeyList(key);
00116   
00117   int loop;
00118   QString s;
00119   for(loop = 0; loop < (int) key.count(); loop++)
00120   {
00121     dict.getData(key[loop], s);
00122     stream << key[loop] << "=" << s << "\n";
00123   }
00124   
00125   f.close();
00126 }
00127 
00128 void IndicatorPlugin::getMATypes (QStringList &l)
00129 {
00130   l = maList;
00131 }
00132 
00133 PlotLine * IndicatorPlugin::getMA (PlotLine *in, int type, int period)
00134 {
00135   PlotLine *ma = 0;
00136   TALIB plug;
00137   if (type == 9)
00138     ma = getWilderMA(in, period);
00139   else
00140     ma = plug.getMA(in, type, period);
00141   return ma;  
00142 }
00143 
00144 PlotLine * IndicatorPlugin::getWilderMA (PlotLine *d, int period)
00145 {
00146   PlotLine *wilderma = new PlotLine;
00147 
00148   if (period >= (int) d->getSize())
00149     return wilderma;
00150 
00151   if (period < 1)
00152     return wilderma;
00153 
00154   double t = 0;
00155   int loop;
00156   for (loop = 0; loop < period; loop++)
00157     t = t + d->getData(loop);
00158 
00159   double yesterday = t / period;
00160 
00161   wilderma->append(yesterday);
00162 
00163   for (; loop < (int) d->getSize(); loop++)
00164   {
00165     double t  = (yesterday * (period - 1) + d->getData(loop))/period;
00166     yesterday = t;
00167     wilderma->append(t);
00168   }
00169 
00170   return wilderma;
00171 }
00172 
00173 void IndicatorPlugin::getPluginName (QString &d)
00174 {
00175   d = pluginName;
00176 }
00177 
00178 void IndicatorPlugin::getHelpFile (QString &d)
00179 {
00180   d = helpFile;
00181 }
00182 
00183 IndicatorPlugin::Operator IndicatorPlugin::getOperator (QString &d)
00184 {
00185   int i = opList.findIndex(d);
00186   return (Operator) i;
00187 }
00188 
00189 bool IndicatorPlugin::checkFormat (QString &p, QPtrList<PlotLine> &d, int hrange, int lrange)
00190 {
00191   formatStringList = QStringList::split(",", p, FALSE);
00192 
00193   if ((int) formatStringList.count() < lrange || (int) formatStringList.count() > hrange)
00194   {
00195     qDebug("%s::checkFormat: invalid parm count", pluginName.latin1());
00196     return TRUE;
00197   }
00198 
00199   int loop;
00200   for (loop = 0; loop < (int) formatList.count(); loop++)
00201   {
00202     if (formatList[loop] == FormatInputArray)
00203     {
00204       if (! d.count())
00205       {
00206         qDebug("%s::checkFormat: parm #%i invalid, no INPUT_ARRAY found", pluginName.latin1(), loop+1);
00207         return TRUE;
00208       }
00209       continue;
00210     }
00211 
00212     if (formatList[loop] == FormatInputArray2)
00213     {
00214       if (d.count() != 2)
00215       {
00216         qDebug("%s::checkFormat: parm #%i invalid, no INPUT_ARRAY2 found", pluginName.latin1(), loop+1);
00217         return TRUE;
00218       }
00219       continue;
00220     }
00221 
00222     if (formatList[loop] == FormatInteger)
00223     {
00224       bool ok;
00225       formatStringList[loop].toInt(&ok);
00226       if (! ok)
00227       {
00228         qDebug("%s::checkFormat: parm #%i invalid, not an INTEGER", pluginName.latin1(), loop + 1);
00229         return TRUE;
00230       }
00231       continue;
00232     }
00233 
00234     if (formatList[loop] == FormatDouble)
00235     {
00236       bool ok;
00237       formatStringList[loop].toDouble(&ok);
00238       if (! ok)
00239       {
00240         qDebug("%s::checkFormat: parm #%i invalid, not a DOUBLE", pluginName.latin1(), loop + 1);
00241         return TRUE;
00242       }
00243       continue;
00244     }
00245 
00246     if (formatList[loop] == FormatMAType)
00247     {
00248       QStringList mal;
00249       getMATypes(mal);
00250       if (mal.findIndex(formatStringList[loop]) == -1)
00251       {
00252         qDebug("%s::checkFormat: parm #%i invalid, not an MA_TYPE", pluginName.latin1(), loop + 1);
00253         return TRUE;
00254       }
00255       continue;
00256     }
00257 
00258     if (formatList[loop] == FormatBool)
00259     {
00260       if (! formatStringList[loop].compare("TRUE"))
00261         continue;
00262       else
00263       {
00264         if (! formatStringList[loop].compare("FALSE"))
00265           continue;
00266         else
00267         {
00268           qDebug("%s::checkFormat: parm #%i invalid, not a BOOL", pluginName.latin1(), loop + 1);
00269           return TRUE;
00270         }
00271       }
00272     }
00273   }
00274 
00275   return FALSE;
00276 }
00277 
00278 void IndicatorPlugin::wakeup ()
00279 {
00280   emit signalWakeup();
00281 }
00282 
00283 void IndicatorPlugin::setFormatMethod (QString &d)
00284 {
00285   formatMethod = d;
00286 }
00287 
00288 //***************************************************************
00289 //****************** VIRTUAL OVERIDES ***************************
00290 //***************************************************************
00291 
00292 Indicator * IndicatorPlugin::calculate ()
00293 {
00294   return 0;
00295 }
00296 
00297 int IndicatorPlugin::indicatorPrefDialog (QWidget *)
00298 {
00299   return 0;
00300 }
00301 
00302 PlotLine * IndicatorPlugin::calculateCustom (QString &, QPtrList<PlotLine> &)
00303 {
00304   return 0;
00305 }
00306 
00307 void IndicatorPlugin::getIndicatorSettings (Setting &)
00308 {
00309 }
00310 
00311 void IndicatorPlugin::setIndicatorSettings (Setting &)
00312 {
00313 }
00314 
00315 void IndicatorPlugin::setCustomFunction (QStringList &)
00316 {
00317 }
00318 
00319 void IndicatorPlugin::saveIndicatorSettings (QString &d)
00320 {
00321   Setting set;
00322   getIndicatorSettings(set);
00323   saveFile(d, set);
00324 }
00325 
00326 void IndicatorPlugin::loadIndicatorSettings (QString &d)
00327 {
00328   Setting set;
00329   loadFile(d, set);
00330   QString k = "dateFlag";
00331   dateFlag = set.getInt(k);
00332   k = "logScale";
00333   logScale = set.getInt(k);
00334   setIndicatorSettings(set);
00335 }
00336 
00337 void IndicatorPlugin::formatDialog (QStringList &, QString &, QString &)
00338 {
00339 }
00340 
00341