src/TesterChartPage.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 <qlayout.h>
00023 #include <qvgroupbox.h>
00024 #include <qfont.h>
00025 #include <qvaluelist.h>
00026 #include "TesterChartPage.h"
00027 #include "Config.h"
00028 #include "Indicator.h"
00029 #include "../pics/scaletoscreen.xpm"
00030 
00031 
00032 TesterChartPage::TesterChartPage (QWidget *p, DBIndex *index) : QWidget (p)
00033 {
00034   Config config;
00035   QString s;
00036   config.getData(Config::ScaleToScreen, s);
00037 //  bool logFlag = config.getData(Config::LogScale).toInt();
00038   scaleToScreenFlag = s.toInt();
00039 
00040   QVBoxLayout *vbox = new QVBoxLayout(this);
00041   vbox->setMargin(5);
00042   vbox->setSpacing(5);
00043 
00044   QHBoxLayout *hbox = new QHBoxLayout(vbox);
00045 
00046   toolbar = new Toolbar(this, Toolbar::Horizontal);
00047   hbox->addWidget(toolbar);
00048 
00049   slider = new QSlider(this);
00050   slider->setOrientation(Qt::Horizontal);
00051   connect (slider, SIGNAL(valueChanged(int)), this, SLOT(slotSliderChanged(int)));
00052   hbox->addWidget(slider);
00053 
00054   s = "scalescreen";
00055   QString s2(tr("Scale To Screen"));
00056   toolbar->addButton(s, scaletoscreen, s2);
00057   QObject::connect(toolbar->getButton(s), SIGNAL(clicked()), this, SLOT(slotScaleToScreen()));
00058   
00059   split = new QSplitter(this);
00060   split->setOrientation(Vertical);
00061   vbox->addWidget(split);
00062 
00063   equityPlot = new Plot (split, index);
00064   equityPlot->setGridFlag(TRUE);
00065   equityPlot->setScaleToScreen(scaleToScreenFlag);
00066 //  equityPlot->setLogScale(logFlag);
00067   equityPlot->setPixelspace(5);
00068   equityPlot->setIndex(0);
00069   equityPlot->setDateFlag(TRUE);
00070   equityPlot->setInfoFlag(FALSE);
00071   equityPlot->setCrosshairsFlag (FALSE); // turn off crosshairs
00072   equityPlot->setMenuFlag(FALSE);
00073   QObject::connect(this, SIGNAL(signalIndex(int)), equityPlot, SLOT(setIndex(int)));
00074   config.getData(Config::PlotFont, s);
00075   QStringList l = QStringList::split(",", s, FALSE);
00076   QFont font(l[0], l[1].toInt(), l[2].toInt());
00077   equityPlot->setPlotFont(font);
00078   
00079   plot = new Plot (split, index);
00080   plot->setGridFlag(TRUE);
00081   plot->setScaleToScreen(scaleToScreenFlag);
00082 //  plot->setLogScale(logFlag);
00083   plot->setPixelspace(5);
00084   plot->setIndex(0);
00085   plot->setDateFlag(TRUE);
00086   plot->setInfoFlag(FALSE);
00087   plot->setCrosshairsFlag (FALSE); // turn off crosshairs
00088   plot->setMenuFlag(FALSE);
00089   QObject::connect(this, SIGNAL(signalIndex(int)), plot, SLOT(setIndex(int)));
00090   plot->setPlotFont(font);
00091 
00092   QValueList<int> sizeList = split->sizes();
00093   sizeList[1] = 100;
00094   split->setSizes(sizeList);
00095 }
00096 
00097 TesterChartPage::~TesterChartPage ()
00098 {
00099 }
00100 
00101 void TesterChartPage::slotSliderChanged (int v)
00102 {
00103   emit signalIndex(v);
00104   plot->draw();
00105   equityPlot->draw();
00106 }
00107 
00108 void TesterChartPage::updateChart (BarData *recordList, QPtrList<TradeItem> &trades, double eq)
00109 {
00110   plot->setData(recordList);
00111   equityPlot->setData(recordList);
00112   
00113   //set up indicator
00114   PlotLine *bars = new PlotLine;
00115   QColor green("green");
00116   QColor red("red");
00117   QColor blue("blue");
00118   QColor color;
00119   bars->setType(PlotLine::Bar);
00120 
00121   Indicator *i = new Indicator;
00122   QString s = "bars";
00123   i->setName(s);
00124   i->addLine(bars);
00125   i->getName(s);
00126   plot->addIndicator(i);
00127 
00128   int loop;
00129   for (loop = 0; loop < (int) recordList->count(); loop++)
00130   {
00131     bars->append(blue, recordList->getOpen(loop), recordList->getHigh(loop),
00132                  recordList->getLow(loop), recordList->getClose(loop), FALSE);
00133   }
00134 
00135   for (loop = 0; loop < (int) trades.count(); loop++)
00136   {
00137     TradeItem *trade = trades.at(loop);
00138     QDateTime edate, xdate;
00139     trade->getEnterDate(edate);
00140     trade->getExitDate(xdate);
00141 
00142     if (trade->getTradePosition() == TradeItem::Long)
00143       color = green;
00144     else
00145       color = red;
00146 
00147     int i = recordList->getX(edate);
00148     int j = recordList->getX(xdate);
00149 
00150     int loop2;
00151     for (loop2 = i; loop2 <= j; loop2++)
00152       bars->setColorBar(loop2, color);
00153   }
00154 
00155   slider->setMaxValue(recordList->count() - 1);
00156         
00157   plot->draw();
00158 
00159   //setup the equity line
00160   PlotLine *el = new PlotLine;
00161   el->setColor(green);
00162   
00163   int tloop = 0;
00164   double equity = eq;
00165   for (loop = 0; loop < (int) recordList->count(); loop++)
00166   {
00167     QDateTime rdate, edate, xdate;
00168     recordList->getDate(loop, rdate);
00169     TradeItem *trade = trades.at(tloop);
00170     if (! trade)
00171       continue;
00172     trade->getEnterDate(edate);
00173     trade->getExitDate(xdate);
00174 
00175     // check if we are in a trade
00176     if (rdate >= edate && rdate < xdate)
00177     {
00178       double t = equity + trade->getCurrentProfit(recordList->getClose(loop));
00179       el->append(t);
00180     }
00181     else
00182     {
00183       // check if we are ready to close trade out 
00184       if (rdate == xdate)
00185       {
00186         equity = trade->getBalance();
00187         tloop++;
00188         if (tloop >= (int) trades.count())
00189           tloop = trades.count() - 1;
00190         el->append(equity);
00191       }
00192       else
00193         el->append(equity);
00194     }
00195   }
00196 
00197   Indicator *i2 = new Indicator;
00198   QString str = "equity";
00199   i2->setName(str);
00200   i2->addLine(el);
00201   i2->getName(str);
00202   equityPlot->addIndicator(i2);
00203   equityPlot->draw();
00204 }
00205 
00206 void TesterChartPage::slotScaleToScreen ()
00207 {
00208   if (scaleToScreenFlag)
00209     scaleToScreenFlag = FALSE;
00210   else
00211     scaleToScreenFlag = TRUE;
00212 
00213   plot->setScaleToScreen(scaleToScreenFlag);
00214   equityPlot->setScaleToScreen(scaleToScreenFlag);
00215 
00216   equityPlot->draw();
00217   plot->draw();
00218 }
00219 
00220 void TesterChartPage::slotLogScaling (bool)
00221 {
00222 /*
00223   plot->setLogScale(d);
00224   equityPlot->setLogScale(d);
00225   equityPlot->draw();
00226   plot->draw();
00227 */
00228 }
00229 
00230 void TesterChartPage::clear ()
00231 {
00232   plot->clear();
00233   equityPlot->clear();
00234 }
00235