lib/TradeItem.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 "TradeItem.h"
00023 #include <qobject.h>
00024 
00025 
00026 TradeItem::TradeItem ()
00027 {
00028   tradePosition = Long;
00029   enterSignal = None;
00030   exitSignal = None;
00031   enterPrice = 0;
00032   exitPrice = 0;
00033   volume = 0;
00034   profit = 0;
00035   stockFlag = TRUE;
00036   commissionType = FALSE;
00037   entryCom = 0;
00038   exitCom = 0;
00039   balance = 0;
00040   margin = 0;
00041 }
00042 
00043 TradeItem::~TradeItem ()
00044 {
00045 }
00046 
00047 void TradeItem::setTradePosition (TradeItem::TradePosition d)
00048 {
00049   tradePosition = d;
00050 }
00051 
00052 TradeItem::TradePosition TradeItem::getTradePosition ()
00053 {
00054   return tradePosition;
00055 }
00056 
00057 void TradeItem::setEnterSignal (TradeItem::TradeSignal d)
00058 {
00059   enterSignal = d;
00060 }
00061 
00062 TradeItem::TradeSignal TradeItem::getEnterSignal ()
00063 {
00064   return enterSignal;
00065 }
00066 
00067 void TradeItem::setExitSignal (TradeItem::TradeSignal d)
00068 {
00069   exitSignal = d;
00070 }
00071 
00072 void TradeItem::setExitSignal (QString &d)
00073 {
00074   while (1)
00075   {
00076     if (! d.compare(QObject::tr("Enter Long")))
00077     {
00078       exitSignal = EnterLong;
00079       break;
00080     }
00081 
00082     if (! d.compare(QObject::tr("Exit Long")))
00083     {
00084       exitSignal = ExitLong;
00085       break;
00086     }
00087 
00088 
00089     if (! d.compare(QObject::tr("Enter Short")))
00090     {
00091       exitSignal = EnterShort;
00092       break;
00093     }
00094 
00095     if (! d.compare(QObject::tr("Exit Short")))
00096     {
00097       exitSignal = ExitShort;
00098       break;
00099     }
00100 
00101     if (! d.compare(QObject::tr("Max Loss")))
00102     {
00103       exitSignal = MaximumLoss;
00104       break;
00105     }
00106 
00107     if (! d.compare(QObject::tr("Profit")))
00108     {
00109       exitSignal = Profit;
00110       break;
00111     }
00112 
00113     if (! d.compare(QObject::tr("Trailing")))
00114     {
00115       exitSignal = Trailing;
00116       break;
00117     }
00118 
00119     if (! d.compare(QObject::tr("CUS Stop")))
00120     {
00121       exitSignal = CUSStop;
00122       break;
00123     }
00124 
00125     if (! d.compare(QObject::tr("End of test")))
00126     {
00127       exitSignal = EndTest;
00128       break;
00129     }
00130 
00131     break;
00132   }
00133 }
00134 
00135 TradeItem::TradeSignal TradeItem::getExitSignal ()
00136 {
00137   return exitSignal;
00138 }
00139 
00140 void TradeItem::setEnterDate (QDateTime &d)
00141 {
00142   enterDate = d;
00143 }
00144 
00145 void TradeItem::getEnterDate (QDateTime &d)
00146 {
00147   d = enterDate;
00148 }
00149 
00150 void TradeItem::setExitDate (QDateTime &d)
00151 {
00152   exitDate = d;
00153 }
00154 
00155 void TradeItem::getExitDate (QDateTime &d)
00156 {
00157   d = exitDate;
00158 }
00159 
00160 void TradeItem::getEnterDateString (QString &s)
00161 {
00162   s = enterDate.toString("yyyy-MM-dd hh:mm:ss");
00163 }
00164 
00165 void TradeItem::getExitDateString (QString &s)
00166 {
00167   s = exitDate.toString("yyyy-MM-dd hh:mm:ss");
00168 }
00169 
00170 void TradeItem::setEnterPrice (double d)
00171 {
00172   enterPrice = d;
00173 }
00174 
00175 double TradeItem::getEnterPrice ()
00176 {
00177   return enterPrice;
00178 }
00179 
00180 void TradeItem::setExitPrice (double d)
00181 {
00182   exitPrice = d;
00183 }
00184 
00185 double TradeItem::getExitPrice ()
00186 {
00187   return exitPrice;
00188 }
00189 
00190 void TradeItem::setVolume (int d)
00191 {
00192   volume = d;
00193 }
00194 
00195 int TradeItem::getVolume ()
00196 {
00197   return volume;
00198 }
00199 
00200 void TradeItem::setStockFlag (bool d)
00201 {
00202   stockFlag = d;
00203 }
00204 
00205 bool TradeItem::getStockFlag ()
00206 {
00207   return stockFlag;
00208 }
00209 
00210 void TradeItem::setFuturesType (QString &d)
00211 {
00212   futuresType = d;
00213   fd.setSymbol(futuresType);
00214 }
00215 
00216 void TradeItem::calculateProfit ()
00217 {
00218   profit = 0;
00219   if (tradePosition == Short)
00220     profit = enterPrice - exitPrice;
00221   else
00222     profit = exitPrice - enterPrice;
00223 
00224   if (! stockFlag)
00225   {
00226     if (! futuresType.length())
00227       return;
00228 
00229     profit = fd.getRate() * profit;
00230   }
00231 
00232   profit = profit * volume;
00233 
00234   if (commissionType)
00235   {
00236     if (stockFlag)
00237     {
00238       entryCom = (enterPrice * volume) * entryCom;
00239       exitCom = (exitPrice * volume) * exitCom;
00240     }
00241     else
00242     {
00243       if (! futuresType.length())
00244         return;
00245 
00246       entryCom = (margin * volume) * entryCom;
00247       exitCom = (margin * volume) * exitCom;
00248     }
00249   }
00250 
00251   balance = balance + profit - entryCom - exitCom;
00252 }
00253 
00254 void TradeItem::getTradePositionString (QString &s)
00255 {
00256   s = "L";
00257   if (tradePosition == Short)
00258     s = "S";
00259 }
00260 
00261 void TradeItem::getExitSignalString (QString &s)
00262 {
00263   s.truncate(0);
00264 
00265   switch (exitSignal)
00266   {
00267     case EnterLong:
00268       s = QObject::tr("Enter Long");
00269       break;
00270     case ExitLong:
00271       s = QObject::tr("Exit Long");
00272       break;
00273     case EnterShort:
00274       s = QObject::tr("Enter Short");
00275       break;
00276     case ExitShort:
00277       s = QObject::tr("Exit Short");
00278       break;
00279     case MaximumLoss:
00280       s = QObject::tr("Max Loss");
00281       break;
00282     case Profit:
00283       s = QObject::tr("Profit");
00284       break;
00285     case Trailing:
00286       s = QObject::tr("Trailing");
00287       break;
00288     case CUSStop:
00289       s = QObject::tr("CUS Stop");
00290       break;
00291     case EndTest:
00292       s = QObject::tr("End of test");
00293       break;
00294     default:
00295       break;
00296   }
00297 }
00298 
00299 double TradeItem::getProfit ()
00300 {
00301   return profit;
00302 }
00303 
00304 void TradeItem::setBalance (double d)
00305 {
00306   balance = d;
00307 }
00308 
00309 double TradeItem::getBalance ()
00310 {
00311   return balance;
00312 }
00313 
00314 void TradeItem::setEntryCom (double d)
00315 {
00316   entryCom = d;
00317 }
00318 
00319 double TradeItem::getEntryCom ()
00320 {
00321   return entryCom;
00322 }
00323 
00324 void TradeItem::setExitCom (double d)
00325 {
00326   exitCom = d;
00327 }
00328 
00329 double TradeItem::getExitCom ()
00330 {
00331   return exitCom;
00332 }
00333 
00334 void TradeItem::setCommissionType (bool d)
00335 {
00336   commissionType = d;
00337 }
00338 
00339 bool TradeItem::getCommissionType ()
00340 {
00341   return commissionType;
00342 }
00343 
00344 double TradeItem::getCurrentProfit (double xp)
00345 {
00346   double prof = 0;
00347   if (tradePosition == TradeItem::Short)
00348     prof = enterPrice - xp;
00349   else
00350     prof = xp - enterPrice;
00351 
00352   if (! stockFlag)
00353   {
00354     if (! futuresType.length())
00355       return 0;
00356 
00357     prof = fd.getRate() * prof;
00358   }
00359 
00360   prof = prof * volume;
00361 
00362   return prof;
00363 }
00364 
00365 void TradeItem::setMargin (int d)
00366 {
00367   margin = d;
00368 }
00369