lib/BARS.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 "BARS.h"
00023 #include "PrefDialog.h"
00024 #include "DBBase.h"
00025 #include "Config.h"
00026 #include <qdict.h>
00027 #include <qobject.h>
00028 #include <qinputdialog.h>
00029 #include <math.h>
00030 #include <qfileinfo.h>
00031 
00032 
00033 BARS::BARS ()
00034 {
00035   pluginName = "BARS";
00036   helpFile = "bars.html";
00037 
00038   methodList.append("Bar");
00039   methodList.append("Candle");
00040   methodList.append("PF");
00041 
00042   Config config;
00043   config.getData(Config::Home, dbPath);
00044   dbPath.append("/index/pf.db");
00045 
00046   barUpColorLabel = "barUpColor";
00047   barDownColorLabel = "barDownColor";
00048   barNeutralColorLabel = "barNeutralColor";
00049   candleColorLabel = "candleColor";
00050   labelLabel = "label";
00051   methodLabel = "method";
00052   lineTypeLabel = "lineType";
00053   pluginLabel = "plugin";
00054 
00055   pfXColorLabel = "pfXColor";
00056   pfOColorLabel = "pfOColor";
00057   pfReversalLabel = "pfReversal";
00058   pfMethodLabel = "pfMethod";
00059 
00060   maColorLabel = "maColor";
00061   maLineTypeLabel = "maLineType";
00062   maPeriodLabel = "maPeriod";
00063   maLabelLabel = "maLabel";
00064   maTypeLabel = "maType";
00065   maInputLabel = "maInput";
00066 
00067   maColor2Label = "maColor2";
00068   maLineType2Label = "maLineType2";
00069   maPeriod2Label = "maPeriod2";
00070   maLabel2Label = "maLabel2";
00071   maType2Label = "maType2";
00072   maInput2Label = "maInput2";
00073 
00074   maColor3Label = "maColor3";
00075   maLineType3Label = "maLineType3";
00076   maPeriod3Label = "maPeriod3";
00077   maLabel3Label = "maLabel3";
00078   maType3Label = "maType3";
00079   maInput3Label = "maInput3";
00080 
00081   formatList.append(FormatString);
00082 
00083   setDefaults();
00084 }
00085 
00086 BARS::~BARS ()
00087 {
00088 }
00089 
00090 void BARS::setDefaults ()
00091 {
00092   barUpColor.setNamedColor("green");
00093   barDownColor.setNamedColor("red");
00094   barNeutralColor.setNamedColor("blue");
00095   candleColor.setNamedColor("green");
00096   label = pluginName;
00097 
00098   pfXColor.setNamedColor("green");
00099   pfOColor.setNamedColor("red");
00100   pfReversal = 3;
00101   pfBoxSize = 1;
00102   pfMethod = QObject::tr("Default");
00103 
00104   maColor.setNamedColor("red");
00105   maColor2.setNamedColor("red");
00106   maColor3.setNamedColor("yellow");
00107   maLineType = PlotLine::Line;
00108   maLineType2 = PlotLine::Line;
00109   maLineType3 = PlotLine::Line;
00110   maLabel = "10MA";
00111   maLabel2 = "50MA";
00112   maLabel3 = "200MA";
00113   maPeriod = 10;
00114   maPeriod2 = 50;
00115   maPeriod3 = 200;
00116   maType = 0;
00117   maType2 = 0;
00118   maType3 = 0;
00119   maInput = BarData::Close;
00120   maInput2 = BarData::Close;
00121   maInput3 = BarData::Close;
00122 }
00123 
00124 Indicator * BARS::calculate ()
00125 {
00126   Indicator *output = new Indicator;
00127   output->setDateFlag(dateFlag);
00128   output->setLogScale(logScale);
00129 
00130   if (! method.compare("Bar"))
00131   {
00132     output->addLine(calculateBar());
00133     calculateMA(output);
00134   }
00135 
00136   if (! method.compare("Candle"))
00137   {
00138     output->addLine(calculateCandle());
00139     calculateMA(output);
00140   }
00141 
00142   if (! method.compare("PF"))
00143     output->addLine(calculatePF());
00144 
00145   return output;
00146 }
00147 
00148 PlotLine * BARS::calculateBar ()
00149 {
00150   PlotLine *line = new PlotLine;
00151 
00152   int loop;
00153   QColor color;
00154 
00155   for (loop = 0; loop < (int) data->count(); loop++)
00156   {
00157     if (loop > 0)
00158     {  
00159       if (data->getClose(loop) > data->getClose(loop - 1))
00160         color = barUpColor;
00161       else
00162       {
00163         if (data->getClose(loop) < data->getClose(loop - 1))
00164           color = barDownColor;
00165         else
00166           color = barNeutralColor;
00167       }
00168     }
00169     else
00170       color = barNeutralColor;
00171 
00172     line->append(color, data->getOpen(loop), data->getHigh(loop), data->getLow(loop),
00173                  data->getClose(loop), FALSE);
00174 
00175     QDateTime dt;
00176     data->getDate(loop, dt);
00177     line->append(dt);
00178   }
00179 
00180   line->setType(PlotLine::Bar);
00181   line->setLabel(label);
00182   return line;
00183 }
00184 
00185 PlotLine * BARS::calculateCandle ()
00186 {
00187   PlotLine *line = new PlotLine;
00188   int loop;
00189   for (loop = 0; loop < (int) data->count(); loop++)
00190   {
00191     double c = data->getClose(loop);
00192     double o = data->getOpen(loop);
00193     bool fillFlag = FALSE;
00194 
00195     if (o != 0)
00196     {
00197       if (c < o)
00198         fillFlag = TRUE;
00199     }
00200 
00201     line->append(candleColor, o, data->getHigh(loop), data->getLow(loop), c, fillFlag);
00202 
00203     QDateTime dt;
00204     data->getDate(loop, dt);
00205     line->append(dt);
00206   }
00207 
00208   line->setType(PlotLine::Candle);
00209   line->setLabel(label);
00210   return line;
00211 }
00212 
00213 PlotLine * BARS::calculatePF ()
00214 {
00215   PlotLine *line = new PlotLine;
00216 
00217   // determine start either x or o
00218   if (data->count() < 2)
00219     return line;
00220 
00221   getPFSettings();
00222 
00223   bool XOFlag = FALSE;
00224   if (data->getHigh(1) > data->getHigh(0))
00225     XOFlag = TRUE; // prices rising, we start with x's
00226 
00227   double high = 0;
00228   double d = data->getHigh(0) /  pfBoxSize;
00229   int t = (int) d;
00230   if (t * pfBoxSize <= data->getHigh(0))
00231     high = (t + 1) * pfBoxSize;
00232   else
00233     high = t * pfBoxSize;
00234 
00235   double low = 0;
00236   t = (int) (data->getLow(0) / pfBoxSize);
00237   low = t * pfBoxSize;
00238 
00239   int loop;
00240   for (loop = 1; loop < (int) data->count(); loop++)
00241   {
00242     if (XOFlag)
00243     {
00244       if (data->getHigh(loop) > high)
00245       {
00246         // new high
00247         d = data->getHigh(loop) /  pfBoxSize;
00248         t = (int) d;
00249         if (t * pfBoxSize <= data->getHigh(loop))
00250           high = (t + 1) * pfBoxSize;
00251         else
00252           high = t * pfBoxSize;
00253       }
00254 
00255       double reversal = high - (pfBoxSize * pfReversal);
00256       if (data->getLow(loop) < reversal)
00257       {
00258         // reversal to O's
00259         line->append(pfXColor, pfBoxSize, high, low, low, XOFlag);
00260 
00261         high = high - pfBoxSize; // lower high 1 box
00262 
00263         t = (int) (data->getLow(loop) / pfBoxSize);
00264         low = t * pfBoxSize;
00265 
00266         XOFlag = FALSE;
00267       }
00268     }
00269     else
00270     {
00271       if (data->getLow(loop) < low)
00272       {
00273         // new low
00274         t = (int) (data->getLow(loop) / pfBoxSize);
00275         low = t * pfBoxSize;
00276       }
00277 
00278       double reversal = low + (pfBoxSize * pfReversal);
00279       if (data->getHigh(loop) > reversal)
00280       {
00281         // reversal to X's
00282         line->append(pfOColor, pfBoxSize, high, low, low, XOFlag);
00283 
00284         low = low + pfBoxSize; // raise low 1 box
00285 
00286         d = data->getHigh(loop) /  pfBoxSize;
00287         t = (int) d;
00288         if (t * pfBoxSize <= data->getHigh(loop))
00289           high = (t + 1) * pfBoxSize;
00290         else
00291           high = t * pfBoxSize;
00292 
00293         XOFlag = TRUE;
00294       }
00295     }
00296   }
00297 
00298   if (XOFlag)
00299     line->append(pfXColor, pfBoxSize, high, low, low, XOFlag);
00300   else
00301     line->append(pfOColor, pfBoxSize, high, low, low, XOFlag);
00302 
00303   line->setType(PlotLine::PF);
00304   line->setLabel(label);
00305   return line;
00306 }
00307 
00308 void BARS::getPFSettings ()
00309 {
00310   // set default traditional
00311   pfBoxSize = (((data->getMax() - data->getMin()) / 2) + data->getMin()) * 0.02;
00312 
00313   if (! pfMethod.compare(QObject::tr("Default")))
00314     return;
00315 
00316   QString s;
00317   Config config;
00318   config.getData(Config::CurrentChart, s);
00319   QFileInfo fi(s);
00320   s = fi.fileName();
00321 
00322   DBBase db;
00323   if (db.open(dbPath))
00324   {
00325     qDebug("BARS::getPFBoxSize: error opening %s", dbPath.latin1());
00326     return;
00327   }
00328 
00329   QString s2;
00330   db.getData(s, s2);
00331   if (! s2.length())
00332     return;
00333 
00334   QStringList l = QStringList::split(",", s2, FALSE);
00335   pfBoxSize = l[0].toDouble();
00336   pfReversal = l[1].toInt();
00337   db.close();
00338 }
00339 
00340 void BARS::calculateMA (Indicator *output)
00341 {
00342   if (maPeriod > 1)
00343   {
00344     PlotLine *in = data->getInput(maInput);
00345     if (in)
00346     {
00347       PlotLine *ma = getMA(in, maType, maPeriod);
00348       ma->setColor(maColor);
00349       ma->setType(maLineType);
00350       ma->setLabel(maLabel);
00351       output->addLine(ma);
00352       delete in;
00353     }
00354   }
00355 
00356   if (maPeriod2 > 1)
00357   {
00358     PlotLine *in = data->getInput(maInput2);
00359     if (in)
00360     {
00361       PlotLine *ma = getMA(in, maType2, maPeriod2);
00362       ma->setColor(maColor2);
00363       ma->setType(maLineType2);
00364       ma->setLabel(maLabel2);
00365       output->addLine(ma);
00366       delete in;
00367     }
00368   }
00369 
00370   if (maPeriod3 > 1)
00371   {
00372     PlotLine *in = data->getInput(maInput3);
00373     if (in)
00374     {
00375       PlotLine *ma = getMA(in, maType3, maPeriod3);
00376       ma->setColor(maColor3);
00377       ma->setType(maLineType3);
00378       ma->setLabel(maLabel3);
00379       output->addLine(ma);
00380       delete in;
00381     }
00382   }
00383 }
00384 
00385 int BARS::indicatorPrefDialog (QWidget *w)
00386 {
00387   bool newFlag = FALSE;
00388   if (! method.length())
00389     newFlag = TRUE;
00390 
00391   if (newFlag)
00392   {
00393     bool ok;
00394     QString s = QInputDialog::getItem(QObject::tr("BARS type Selection"),
00395                                       QObject::tr("Select a bar type:"),
00396                                       methodList,
00397                                       0,
00398                                       TRUE,
00399                                       &ok,
00400                                       w);
00401     if (ok)
00402     {
00403       method = s;
00404       label = method;
00405     }
00406     else
00407       return FALSE;
00408   }
00409 
00410   QString pl = QObject::tr("Parms");
00411   QString ucl = QObject::tr("Up Color");
00412   QString dcl = QObject::tr("Down Color");
00413   QString ncl = QObject::tr("Neutral Color");
00414   QString ccl = QObject::tr("Candle Color");
00415   QString ll = QObject::tr("Label");
00416 
00417   QString pfxcl = QObject::tr("X Color");
00418   QString pfocl = QObject::tr("O Color");
00419   QString pfrl = QObject::tr("Reversal");
00420   QString pfbsl = QObject::tr("Box Size");
00421   QString pfml = QObject::tr("Method");
00422 
00423   QString pl2 = QObject::tr("MA");
00424   QString macl = QObject::tr("MA Color");
00425   QString mall = QObject::tr("MA Label");
00426   QString maltl = QObject::tr("MA Line Type");
00427   QString mapl = QObject::tr("MA Period");
00428   QString matl = QObject::tr("MA Type");
00429   QString mail = QObject::tr("MA Input");
00430 
00431   QString pl3 = QObject::tr("MA2");
00432   QString ma2cl = QObject::tr("MA2 Color");
00433   QString ma2ll = QObject::tr("MA2 Label");
00434   QString ma2ltl = QObject::tr("MA2 Line Type");
00435   QString ma2pl = QObject::tr("MA2 Period");
00436   QString ma2tl = QObject::tr("MA2 Type");
00437   QString ma2il = QObject::tr("MA2 Input");
00438 
00439   QString pl4 = QObject::tr("MA3");
00440   QString ma3cl = QObject::tr("MA3 Color");
00441   QString ma3ll = QObject::tr("MA3 Label");
00442   QString ma3ltl = QObject::tr("MA3 Line Type");
00443   QString ma3pl = QObject::tr("MA3 Period");
00444   QString ma3tl = QObject::tr("MA3 Type");
00445   QString ma3il = QObject::tr("MA3 Input");
00446 
00447   PrefDialog *dialog = new PrefDialog(w);
00448   dialog->setCaption(QObject::tr("BARS Indicator"));
00449   dialog->createPage (pl);
00450   dialog->setHelpFile(helpFile);
00451 
00452   if (! method.compare("Bar"))
00453   {
00454     dialog->setCaption(QObject::tr("Bar Indicator"));
00455     dialog->addColorItem(ucl, pl, barUpColor);
00456     dialog->addColorItem(dcl, pl, barDownColor);
00457     dialog->addColorItem(ncl, pl, barNeutralColor);
00458   }
00459 
00460   if (! method.compare("Candle"))
00461   {
00462     dialog->setCaption(QObject::tr("Candle Indicator"));
00463     dialog->addColorItem(ccl, pl, candleColor);
00464   }
00465 
00466   if (! method.compare("PF"))
00467   {
00468     dialog->setCaption(QObject::tr("P&F Indicator"));
00469 
00470     QStringList l;
00471     l.append(QObject::tr("Default"));
00472     l.append(QObject::tr("Custom"));
00473     dialog->addComboItem(pfml, pl, l, pfMethod);
00474 
00475     dialog->addColorItem(pfxcl, pl, pfXColor);
00476     dialog->addColorItem(pfocl, pl, pfOColor);
00477 
00478     double bs = pfBoxSize;
00479     int rv = pfReversal;
00480 
00481     if (! pfMethod.compare(QObject::tr("Custom")))
00482     {
00483       QString s;
00484       Config config;
00485       config.getData(Config::CurrentChart, s);
00486       QFileInfo fi(s);
00487       s = fi.fileName();
00488 
00489       DBBase db;
00490       if (! db.open(dbPath))
00491       {
00492         QString s2;
00493         db.getData(s, s2);
00494         if (s2.length())
00495         {
00496           QStringList l = QStringList::split(",", s2, FALSE);
00497           bs = l[0].toDouble();
00498           rv = l[1].toInt();
00499         }
00500         db.close();
00501       }
00502     }
00503 
00504     dialog->addDoubleItem(pfbsl, pl, bs, 0.0001, 999999.0);
00505     dialog->addIntItem(pfrl, pl, rv, 1, 99);
00506   }
00507 
00508   dialog->addTextItem(ll, pl, label);
00509 
00510   QStringList mal;
00511   getMATypes(mal);
00512 
00513   if (! method.compare("Bar") || ! method.compare("Candle"))
00514   { 
00515     dialog->createPage (pl2);
00516     dialog->addColorItem(macl, pl2, maColor);
00517     dialog->addTextItem(mall, pl2, maLabel);
00518     dialog->addComboItem(maltl, pl2, lineTypes, maLineType);
00519     dialog->addComboItem(matl, pl2, mal, maType);
00520     dialog->addIntItem(mapl, pl2, maPeriod, 1, 999999);
00521     dialog->addComboItem(mail, pl2, inputTypeList, maInput);
00522 
00523     dialog->createPage (pl3);
00524     dialog->addColorItem(ma2cl, pl3, maColor2);
00525     dialog->addTextItem(ma2ll, pl3, maLabel2);
00526     dialog->addComboItem(ma2ltl, pl3, lineTypes, maLineType2);
00527     dialog->addComboItem(ma2tl, pl3, mal, maType2);
00528     dialog->addIntItem(ma2pl, pl3, maPeriod2, 1, 999999);
00529     dialog->addComboItem(ma2il, pl3, inputTypeList, maInput2);
00530 
00531     dialog->createPage (pl4);
00532     dialog->addColorItem(ma3cl, pl4, maColor3);
00533     dialog->addTextItem(ma3ll, pl4, maLabel3);
00534     dialog->addComboItem(ma3ltl, pl4, lineTypes, maLineType3);
00535     dialog->addComboItem(ma3tl, pl4, mal, maType3);
00536     dialog->addIntItem(ma3pl, pl4, maPeriod3, 1, 999999);
00537     dialog->addComboItem(ma3il, pl4, inputTypeList, maInput3);
00538   }
00539  
00540   int rc = dialog->exec();
00541   
00542   if (rc == QDialog::Accepted)
00543   {
00544     if (! method.compare("Bar"))
00545     {
00546       dialog->getColor(ucl, barUpColor);
00547       dialog->getColor(dcl, barDownColor);
00548       dialog->getColor(ncl, barNeutralColor);
00549       lineType = PlotLine::Bar;
00550     }
00551 
00552     if (! method.compare("Candle"))
00553     {
00554       dialog->getColor(ccl, candleColor);
00555       lineType = PlotLine::Candle;
00556     }
00557 
00558     if (! method.compare("PF"))
00559     {
00560       dialog->getCombo(pfml, pfMethod);
00561       dialog->getColor(pfxcl, pfXColor);
00562       dialog->getColor(pfocl, pfOColor);
00563       pfReversal = dialog->getInt(pfrl);
00564 
00565       if (! pfMethod.compare(QObject::tr("Custom")) && data)
00566       {
00567         double d = dialog->getDouble(pfbsl);
00568         QString s;
00569         Config config;
00570         config.getData(Config::CurrentChart, s);
00571         QFileInfo fi(s);
00572         s = fi.fileName();
00573         DBBase db;
00574         if (! db.open(dbPath))
00575         {
00576           QString s2 = QString::number(d) + "," + QString::number(pfReversal);
00577           db.setData(s, s2);
00578           db.close();
00579         }
00580       }
00581 
00582       lineType = PlotLine::PF;
00583     }
00584 
00585     dialog->getText(ll, label);
00586 
00587     if (! method.compare("Bar") || ! method.compare("Candle"))
00588     { 
00589       dialog->getColor(macl, maColor);
00590       maLineType = (PlotLine::LineType) dialog->getComboIndex(maltl);
00591       maPeriod = dialog->getInt(mapl);
00592       dialog->getText(mall, maLabel);
00593       maType = dialog->getComboIndex(matl);
00594       maInput = (BarData::InputType) dialog->getComboIndex(mail);
00595 
00596       dialog->getColor(ma2cl, maColor2);
00597       maLineType2 = (PlotLine::LineType) dialog->getComboIndex(ma2ltl);
00598       maPeriod2 = dialog->getInt(ma2pl);
00599       dialog->getText(ma2ll, maLabel2);
00600       maType2 = dialog->getComboIndex(ma2tl);
00601       maInput2 = (BarData::InputType) dialog->getComboIndex(ma2il);
00602 
00603       dialog->getColor(ma3cl, maColor3);
00604       maLineType3 = (PlotLine::LineType) dialog->getComboIndex(ma3ltl);
00605       maPeriod3 = dialog->getInt(ma3pl);
00606       dialog->getText(ma3ll, maLabel3);
00607       maType3 = dialog->getComboIndex(ma3tl);
00608       maInput3 = (BarData::InputType) dialog->getComboIndex(ma3il);
00609     }
00610 
00611     rc = TRUE;
00612   }
00613   else
00614     rc = FALSE;
00615   
00616   delete dialog;
00617   return rc;
00618 }
00619 
00620 void BARS::setIndicatorSettings (Setting &dict)
00621 {
00622   setDefaults();
00623   
00624   if (! dict.count())
00625     return;
00626 
00627   QString s;
00628   dict.getData(labelLabel, s);
00629   if (s.length())
00630     label = s;
00631   
00632   dict.getData(lineTypeLabel, s);
00633   if (s.length())
00634     lineType = (PlotLine::LineType) s.toInt();
00635 
00636   dict.getData(methodLabel, s);
00637   if (s.length())
00638     method = s;
00639 
00640   dict.getData(barUpColorLabel, s);
00641   if (s.length())
00642     barUpColor.setNamedColor(s);
00643     
00644   dict.getData(barDownColorLabel, s);
00645   if (s.length())
00646     barDownColor.setNamedColor(s);
00647     
00648   dict.getData(barNeutralColorLabel, s);
00649   if (s.length())
00650     barNeutralColor.setNamedColor(s);
00651 
00652   dict.getData(candleColorLabel, s);
00653   if (s.length())
00654     candleColor.setNamedColor(s);
00655 
00656   dict.getData(maColorLabel, s);
00657   if (s.length())
00658     maColor.setNamedColor(s);
00659     
00660   dict.getData(maLineTypeLabel, s);
00661   if (s.length())
00662     maLineType = (PlotLine::LineType) s.toInt();
00663 
00664   dict.getData(maPeriodLabel, s);
00665   if (s.length())
00666     maPeriod = s.toInt();
00667 
00668   dict.getData(maLabelLabel, s);
00669   if (s.length())
00670     maLabel = s;
00671       
00672   dict.getData(maTypeLabel, s);
00673   if (s.length())
00674     maType = s.toInt();
00675     
00676   dict.getData(maInputLabel, s);
00677   if (s.length())
00678     maInput = (BarData::InputType) s.toInt();
00679 
00680   dict.getData(maColor2Label, s);
00681   if (s.length())
00682     maColor2.setNamedColor(s);
00683     
00684   dict.getData(maLineType2Label, s);
00685   if (s.length())
00686     maLineType2 = (PlotLine::LineType) s.toInt();
00687 
00688   dict.getData(maPeriod2Label, s);
00689   if (s.length())
00690     maPeriod2 = s.toInt();
00691 
00692   dict.getData(maLabel2Label, s);
00693   if (s.length())
00694     maLabel2 = s;
00695       
00696   dict.getData(maType2Label, s);
00697   if (s.length())
00698     maType2 = s.toInt();
00699     
00700   dict.getData(maInput2Label, s);
00701   if (s.length())
00702     maInput2 = (BarData::InputType) s.toInt();
00703 
00704   dict.getData(maColor3Label, s);
00705   if (s.length())
00706     maColor3.setNamedColor(s);
00707     
00708   dict.getData(maLineType3Label, s);
00709   if (s.length())
00710     maLineType3 = (PlotLine::LineType) s.toInt();
00711 
00712   dict.getData(maPeriod3Label, s);
00713   if (s.length())
00714     maPeriod3 = s.toInt();
00715 
00716   dict.getData(maLabel3Label, s);
00717   if (s.length())
00718     maLabel3 = s;
00719       
00720   dict.getData(maType3Label, s);
00721   if (s.length())
00722     maType3 = s.toInt();
00723     
00724   dict.getData(maInput3Label, s);
00725   if (s.length())
00726     maInput3 = (BarData::InputType) s.toInt();
00727 
00728   dict.getData(pfXColorLabel, s);
00729   if (s.length())
00730     pfXColor.setNamedColor(s);
00731     
00732   dict.getData(pfOColorLabel, s);
00733   if (s.length())
00734     pfOColor.setNamedColor(s);
00735     
00736   dict.getData(pfReversalLabel, s);
00737   if (s.length())
00738     pfReversal = s.toInt();
00739 
00740   dict.getData(pfMethodLabel, s);
00741   if (s.length())
00742     pfMethod = s;
00743 }
00744 
00745 void BARS::getIndicatorSettings (Setting &dict)
00746 {
00747   QString ts = barUpColor.name();
00748   dict.setData(barUpColorLabel, ts);
00749   ts = barDownColor.name();
00750   dict.setData(barDownColorLabel, ts);
00751   ts = barNeutralColor.name();
00752   dict.setData(barNeutralColorLabel, ts);
00753   ts = candleColor.name();
00754   dict.setData(candleColorLabel, ts);
00755   dict.setData(labelLabel, label);
00756   dict.setData(methodLabel, method);
00757   ts = QString::number(lineType);
00758   dict.setData(lineTypeLabel, ts);
00759   dict.setData(pluginLabel, pluginName);
00760 
00761   ts = pfXColor.name();
00762   dict.setData(pfXColorLabel, ts);
00763   ts = pfOColor.name();
00764   dict.setData(pfOColorLabel, ts);
00765   ts = QString::number(pfReversal);
00766   dict.setData(pfReversalLabel, ts);
00767   dict.setData(pfMethodLabel, pfMethod);
00768 
00769   ts = maColor.name();
00770   dict.setData(maColorLabel, ts);
00771   ts = QString::number(maLineType);
00772   dict.setData(maLineTypeLabel, ts);
00773   ts = QString::number(maPeriod);
00774   dict.setData(maPeriodLabel, ts);
00775   dict.setData(maLabelLabel, maLabel);
00776   ts = QString::number(maType);
00777   dict.setData(maTypeLabel, ts);
00778   ts = QString::number(maInput);
00779   dict.setData(maInputLabel, ts);
00780 
00781   ts = maColor2.name();
00782   dict.setData(maColor2Label, ts);
00783   ts = QString::number(maLineType2);
00784   dict.setData(maLineType2Label, ts);
00785   ts = QString::number(maPeriod2);
00786   dict.setData(maPeriod2Label, ts);
00787   dict.setData(maLabel2Label, maLabel2);
00788   ts = QString::number(maType2);
00789   dict.setData(maType2Label, ts);
00790   ts = QString::number(maInput2);
00791   dict.setData(maInput2Label, ts);
00792 
00793   ts = maColor3.name();
00794   dict.setData(maColor3Label, ts);
00795   ts = QString::number(maLineType3);
00796   dict.setData(maLineType3Label, ts);
00797   ts = QString::number(maPeriod3);
00798   dict.setData(maPeriod3Label, ts);
00799   dict.setData(maLabel3Label, maLabel3);
00800   ts = QString::number(maType3);
00801   dict.setData(maType3Label, ts);
00802   ts = QString::number(maInput3);
00803   dict.setData(maInput3Label, ts);
00804 }
00805 
00806 PlotLine * BARS::calculateCustom (QString &p, QPtrList<PlotLine> &d)
00807 {
00808   // format1 (BARS): TYPE
00809   // format2 (BARS): TYPE, COLOR
00810   // format3 (BARS): TYPE, REVERSAL
00811   // TODO: format4 (BARS): TYPE, REVERSAL, BOXSIZE
00812 
00813   int type = 0; // 0 == bars, 1 == candle, 2 == pf
00814   formatList.clear();
00815   QStringList l = QStringList::split(",", p, FALSE);
00816   if (l.count() == 1)
00817     formatList.append(FormatString); // OHLC bars
00818   else
00819   {
00820     if (l.count() == 2)
00821     {
00822       bool ok;
00823       //formatStringList[1].toInt(&ok);
00824       l[1].toInt(&ok);
00825       if (! ok)
00826       {
00827         // candle bars
00828         formatList.append(FormatString);
00829         formatList.append(FormatString);
00830         type = 1;
00831       }
00832       else
00833       {
00834         // PF bars
00835         formatList.append(FormatString);
00836         formatList.append(FormatInteger);
00837         type = 2;
00838       }
00839     }
00840     else
00841     {
00842       qDebug("BARS::calculateCustom: invalid parm count");
00843       return 0;
00844     }
00845   }
00846 
00847   if (checkFormat(p, d, 2, 1))
00848     return 0;
00849 
00850   method = formatStringList[0];
00851 
00852   if (type == 1)
00853     candleColor.setNamedColor(formatStringList[1]);
00854 
00855   if (type == 2)
00856     pfReversal = formatStringList[1].toInt();
00857 
00858   PlotLine *line = 0;
00859   if (! method.compare("Bar"))
00860     line = calculateBar();
00861 
00862   if (! method.compare("Candle"))
00863     line = calculateCandle();
00864 
00865   if (! method.compare("PF"))
00866     line = calculatePF();
00867 
00868   return line;
00869 }
00870 
00871 void BARS::formatDialog (QStringList &, QString &rv, QString &rs)
00872 {
00873   rs.truncate(0);
00874   rv.truncate(0);
00875 
00876   bool ok;
00877   method = QInputDialog::getItem(QObject::tr("BARS Method Selection"),
00878                                  QObject::tr("Select a method:"),
00879                                  methodList,
00880                                  0,
00881                                  TRUE,
00882                                  &ok,
00883                                  0);
00884   if (! ok)
00885     return;
00886 
00887   QString pl = QObject::tr("Parms");
00888   QString vnl = QObject::tr("Variable Name");
00889   QString cl = QObject::tr("Color");
00890   QString rl = QObject::tr("Reversal");
00891   PrefDialog *dialog = new PrefDialog(0);
00892   dialog->setCaption(QObject::tr("BARS Format"));
00893   dialog->createPage (pl);
00894   dialog->setHelpFile(helpFile);
00895 
00896   QString s;
00897   dialog->addTextItem(vnl, pl, s);
00898 
00899   if (! method.compare("Candle"))
00900     dialog->addColorItem(cl, pl, candleColor);
00901 
00902   if (! method.compare("PF"))
00903     dialog->addIntItem(rl, pl, pfReversal);
00904 
00905   int rc = dialog->exec();
00906 
00907   if (rc == QDialog::Accepted)
00908   {
00909     dialog->getText(vnl, rv);
00910     rs = method;
00911 
00912     if (! method.compare("Candle"))
00913     {
00914       dialog->getColor(cl, candleColor);
00915       rs.append("," + candleColor.name());
00916     }
00917 
00918     if (! method.compare("PF"))
00919     {
00920       pfReversal = dialog->getInt(rl);
00921       rs.append("," + QString::number(pfReversal));
00922     }
00923   }
00924 
00925   delete dialog;
00926 }
00927