lib/CUS.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 "CUS.h"
00023 #include "CUSDialog.h"
00024 #include "Config.h"
00025 #include "IndicatorPlugin.h"
00026 #include <qmessagebox.h>
00027 #include <qobject.h>
00028 #include <qfile.h>
00029 
00030 CUS::CUS ()
00031 {
00032   pluginName = "CUS";
00033   helpFile = "cus.html";
00034   version = 0.33;
00035 }
00036 
00037 CUS::~CUS ()
00038 {
00039 }
00040 
00041 Indicator * CUS::calculate ()
00042 {
00043   Config config;
00044   QStringList varList;
00045   QDict<PlotLine> lines;
00046   lines.setAutoDelete(TRUE);
00047 
00048   checkIncludes();
00049 
00050   Indicator *output = new Indicator;
00051   output->setDateFlag(dateFlag);
00052   output->setLogScale(logScale);
00053   
00054   int loop;
00055   for (loop = 0; loop < (int) formulaList.count(); loop++)
00056   {
00057     // check if whole line is a comment
00058     QString s = formulaList[loop].left(2);
00059     if ( ! s.compare("//"))
00060       continue;
00061 
00062     if (formulaList[loop].contains(":="))
00063     {
00064       QStringList l = QStringList::split(":=", formulaList[loop], FALSE);
00065       if (l.count() != 2)
00066       {
00067         qDebug("CUS::calculate: line %i parm missing", loop);
00068         return output;
00069       }
00070       
00071       QString var = l[0].stripWhiteSpace();
00072       if (varList.findIndex(var) != -1)
00073       {
00074         qDebug("CUS::calculate: line %i duplicate variable: %s", loop, var.latin1());
00075         return output;
00076       }
00077       varList.append(var);
00078 
00079       QStringList l2 = QStringList::split("(", l[1], FALSE);
00080       if (l2.count() != 2)
00081       {
00082         qDebug("CUS::calculate: line %i bad indicator format", loop);
00083         return output;
00084       }
00085       
00086       QString plugin = l2[0].stripWhiteSpace();
00087       QString parms = l2[1];
00088       parms.truncate(parms.find(")", 0, TRUE));
00089       parms = parms.stripWhiteSpace();
00090       while(parms.contains(" "))
00091         parms = parms.remove(parms.find(" ", 0, TRUE), 1);
00092 
00093       IndicatorPlugin *plug = config.getIndicatorPlugin(plugin);
00094       if (! plug)
00095       {
00096         qDebug("CUS::calculate: %s plugin not loaded", plugin.latin1());
00097         config.closePlugin(plugin);
00098         return output;
00099       }
00100       plug->setIndicatorInput(data);
00101 
00102       l = QStringList::split(",", parms, FALSE);
00103       int loop2;
00104       QPtrList<PlotLine> inList;
00105       inList.setAutoDelete(FALSE);
00106       for (loop2 = 0; loop2 < (int) l.count(); loop2++)
00107       {
00108         if (varList.findIndex(l[loop2]) != -1)
00109           inList.append(lines.find(l[loop2]));
00110         else
00111         {
00112           int itype = inputTypeList.findIndex(l[loop2]);
00113           if (itype != -1)
00114           {
00115             PlotLine *in = data->getInput((BarData::InputType) itype);
00116             if (! in)
00117             {
00118               qDebug("CUS::calculate:line%i parm%i: input not found", loop, loop2);
00119               return output;
00120             }
00121 
00122             lines.replace(l[loop2], in);
00123             inList.append(in);
00124           }
00125         }
00126       }
00127 
00128       PlotLine *out = plug->calculateCustom(parms, inList);
00129       if (! out)
00130       {
00131         qDebug("CUS::calculate: line %i: no PlotLine returned: %s", loop, parms.latin1());
00132         config.closePlugin(plugin);
00133         return output;
00134       }
00135 
00136       lines.replace(var, out);
00137     }
00138 
00139     createPlot(formulaList[loop], lines, output);
00140   }
00141 
00142   return output;
00143 }
00144 
00145 void CUS::createPlot (QString &d, QDict<PlotLine> &lines, Indicator *output)
00146 {
00147   if (! d.contains("plot"))
00148     return;
00149 
00150   QStringList l = QStringList::split("(", d, FALSE);
00151   if (l.count() != 2)
00152   {
00153     qDebug("CUS::createPlot: bad plot format: %s", d.ascii());
00154     return;
00155   }
00156 
00157   QString parms = l[1];
00158   parms.truncate(parms.find(")", -1, TRUE));
00159   l = QStringList::split(",", parms, FALSE);
00160   if (l.count() != 4)
00161   {
00162     qDebug("CUS::createPlot: missing plot parms: %s",d.ascii());
00163     return;
00164   }
00165 
00166   // 1.var name
00167   l[0] = l[0].stripWhiteSpace();
00168   PlotLine *pl = lines.find(l[0]);
00169   if (! pl)
00170   {
00171     qDebug("CUS::createPlot: bad plot parm 1: %s",d.ascii());
00172     return;
00173   }
00174 
00175   // 2.color
00176   l[1] = l[1].stripWhiteSpace();
00177   pl->setColor(l[1]);
00178 
00179   // 3.label
00180   l[2] = l[2].stripWhiteSpace();
00181   pl->setLabel(l[2]);
00182 
00183   // 4.linetype
00184   l[3] = l[3].stripWhiteSpace();
00185   pl->setType(l[3]);
00186 
00187   PlotLine *tline = new PlotLine;
00188   tline->copy(pl);
00189   output->addLine(tline);
00190 }
00191 
00192 int CUS::indicatorPrefDialog (QWidget *)
00193 {
00194   CUSDialog *dialog = new CUSDialog(helpFile);
00195   
00196   int loop;
00197   for (loop = 0; loop < (int) formulaList.count(); loop++)
00198     dialog->setLine(formulaList[loop]);
00199     
00200   int rc = dialog->exec();
00201   
00202   if (rc == QDialog::Accepted)
00203   {
00204     dialog->getList(formulaList);
00205 
00206     rc = TRUE;
00207   }
00208   else
00209     rc = FALSE;
00210   
00211   delete dialog;
00212   return rc;
00213 }
00214 
00215 void CUS::setCustomFunction (QStringList &d)
00216 {
00217   formulaList.clear();
00218   formulaList = d;
00219 }
00220 
00221 void CUS::getIndicatorSettings (Setting &dict)
00222 {
00223   QString ts = "script";
00224   QString ts2 = formulaList.join("|");
00225   dict.setData(ts, ts2);
00226   ts = "plugin";
00227   dict.setData(ts, pluginName);
00228   ts = "version";
00229   ts2 = QString::number(version);
00230   dict.setData(ts, ts2);
00231 }
00232 
00233 void CUS::setIndicatorSettings (Setting &dict)
00234 {
00235   formulaList.clear();
00236 
00237   if (! dict.count())
00238     return;
00239 
00240   QString ts = "script";
00241   QString s;
00242   dict.getData(ts, s);
00243   if (s.length())
00244     formulaList = QStringList::split("|", s, FALSE);
00245 
00246   ts = "version";
00247   dict.getData(ts, s);
00248   if (s.length())
00249     version = s.toDouble();
00250 }
00251 
00252 void CUS::includeCUS (QString &d, QStringList &rl)
00253 {
00254   QStringList l = QStringList::split(")", d, FALSE);
00255   QStringList l2 = QStringList::split("(", l[0], FALSE);
00256   QString i = l2[1];
00257   i = i.stripWhiteSpace();
00258 
00259   Config config;
00260   QString s, s2;
00261   config.getData(Config::IndicatorPath, s);
00262   config.getData(Config::IndicatorGroup, s2);
00263   s.append("/" + s2 + "/" + i);
00264 
00265   Setting dict;
00266   config.getIndicator(s, dict);
00267 
00268   if (! dict.count())
00269     return;
00270 
00271   QString ts = "script";
00272   dict.getData(ts, s);
00273   if (s.length())
00274     rl = QStringList::split("|", s, FALSE);
00275 }
00276 
00277 void CUS::checkIncludes ()
00278 {
00279   int loop;
00280   QStringList nfl;
00281   for (loop = 0; loop < (int) formulaList.count(); loop++)
00282   {
00283     if (! formulaList[loop].contains("INCLUDECUS("))
00284     {
00285       nfl.append(formulaList[loop]);
00286       continue;
00287     }
00288 
00289     QStringList l;
00290     includeCUS(formulaList[loop], l);
00291 
00292     int loop2;
00293     for (loop2 = 0; loop2 < (int) l.count(); loop2++)
00294       nfl.append(l[loop2]);
00295   }
00296 
00297   formulaList = nfl;
00298 }
00299