Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

log and series counter

  • Loading branch information...
commit b80e03824bb7fa39b283529c9fd2febe14d1dc34 1 parent c89f97f
Evgeniy Sokolov authored
View
21 classes/bussiness/Log.class.php
@@ -0,0 +1,21 @@
+<?php
+ namespace tradeSystem;
+
+ /**
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @author Evgeniy Sokolov <ewgraf@gmail.com>
+ */
+ final class Log extends \ewgraFramework\Singleton
+ {
+ private $items = array();
+
+ /**
+ * @return Log
+ */
+ public function add($text)
+ {
+ $this->items[] = $text;
+ return $this;
+ }
+ }
+?>
View
10 classes/bussiness/Portfolio.class.php
@@ -27,6 +27,11 @@ public function setBalance($balance)
public function addBalance($balance)
{
$this->balance = Math::add($this->balance, $balance);
+
+ Log::me()->add(
+ __CLASS__.': add '.$balance.' to balance, now '.$this->balance
+ );
+
return $this;
}
@@ -36,6 +41,11 @@ public function addBalance($balance)
public function subBalance($balance)
{
$this->balance = Math::sub($this->balance, $balance);
+
+ Log::me()->add(
+ __CLASS__.': sub '.$balance.' from balance, now '.$this->balance
+ );
+
return $this;
}
View
51 classes/bussiness/SeriesCounter.class.php
@@ -0,0 +1,51 @@
+<?php
+ namespace tradeSystem;
+
+ /**
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @author Evgeniy Sokolov <ewgraf@gmail.com>
+ */
+ final class SeriesCounter
+ {
+ private $winSeries = 0;
+ private $loseSeries = 0;
+
+ private $win = 0;
+ private $lose = 0;
+
+ public static function create()
+ {
+ return new self;
+ }
+
+ public function win()
+ {
+ if ($this->lose) {
+ $this->loseSeries = max($this->loseSeries, $this->lose);
+ $this->lose = 0;
+ }
+
+ $this->win++;
+ }
+
+ public function lose()
+ {
+ if ($this->win) {
+ $this->winSeries = max($this->winSeries, $this->win);
+ $this->win = 0;
+ }
+
+ $this->lose++;
+ }
+
+ public function getWinSeries()
+ {
+ return max($this->winSeries, $this->win);
+ }
+
+ public function getLoseSeries()
+ {
+ return max($this->loseSeries, $this->lose);
+ }
+ }
+?>
View
36 classes/bussiness/strategy/MACDHistogrammReverseStrategy.class.php
@@ -12,6 +12,11 @@
const TAKE_PROFIT_INDENT = 0.4;
/**
+ * @var SeriesCounter
+ */
+ private $seriesCounter = null;
+
+ /**
* @var Security
*/
private $security = null;
@@ -26,6 +31,20 @@
/**
* @return MACDHistogrammReverseStrategy
*/
+ public function setSeriesCounter(SeriesCounter $seriesCounter)
+ {
+ $this->seriesCounter = $seriesCounter;
+ return $this;
+ }
+
+ public function getSeriesCounter()
+ {
+ return $this->seriesCounter;
+ }
+
+ /**
+ * @return MACDHistogrammReverseStrategy
+ */
public function setIndicator(Indicator $indicator)
{
$this->indicator = $indicator;
@@ -96,12 +115,18 @@ private function openPosition($price, OrderType $orderType)
setSecurity($this->security)->
setType($orderType->getPositionType());
+ Log::me()->add(
+ __CLASS__.': open '.$position->getType()->getName().' position with price '
+ .$position->getPrice().' and count '.$position->getCount().' ('.$position->getSecurity()->getId().')'
+ );
+
PositionManager::me()->manage($position);
$invertor = $orderType->getInvertor();
$stopLoss =
StopLoss::create()->
+ setSeriesCounter($this->getSeriesCounter())->
setCount($position->getCount())->
setPrice(
Math::sub(
@@ -115,8 +140,14 @@ private function openPosition($price, OrderType $orderType)
setType($orderType->getInverted())->
setSecurity($this->security);
+ Log::me()->add(
+ __CLASS__.': add '.$stopLoss->getType()->getName().' stop-loss with price '
+ .$stopLoss->getPrice().' and count '.$stopLoss->getCount().' ('.$stopLoss->getSecurity()->getId().')'
+ );
+
$takeProfit =
TakeProfit::create()->
+ setSeriesCounter($this->getSeriesCounter())->
setCount($position->getCount())->
setIndent(self::TAKE_PROFIT_INDENT)->
setIndentUnitsType(UnitsType::value())->
@@ -132,6 +163,11 @@ private function openPosition($price, OrderType $orderType)
setType($orderType->getInverted())->
setSecurity($this->security);
+ Log::me()->add(
+ __CLASS__.': add '.$takeProfit->getType()->getName().' take-profit with price '
+ .$takeProfit->getPrice().' and count '.$takeProfit->getCount().', indent is '.$takeProfit->getIndent().' '.$takeProfit->getIndentUnitsType()->getName().' ('.$takeProfit->getSecurity()->getId().')'
+ );
+
$orGroup =
OrderOrGroup::create()->
addStrategy($stopLoss)->
View
27 classes/bussiness/strategy/orders/StopLoss.class.php
@@ -7,6 +7,11 @@
*/
final class StopLoss extends BaseOrder
{
+ /**
+ * @var SeriesCounter
+ */
+ private $seriesCounter = null;
+
private $price = null;
/**
@@ -20,6 +25,20 @@ public static function create()
/**
* @return StopLoss
*/
+ public function setSeriesCounter(SeriesCounter $seriesCounter)
+ {
+ $this->seriesCounter = $seriesCounter;
+ return $this;
+ }
+
+ public function getSeriesCounter()
+ {
+ return $this->seriesCounter;
+ }
+
+ /**
+ * @return StopLoss
+ */
public function setPrice($price)
{
$this->price = $price;
@@ -42,6 +61,14 @@ public function simpleHandle($value, $realizePrice = null)
? $realizePrice
: $value
);
+
+ Log::me()->add(
+ __CLASS__.': realized with price '
+ .$this->getRealizationPrice().' and count '.$this->getCount().' ('.$this->getSecurity()->getId().')'
+ );
+
+ if ($this->getSeriesCounter())
+ $this->getSeriesCounter()->lose();
}
return $this;
View
27 classes/bussiness/strategy/orders/TakeProfit.class.php
@@ -7,6 +7,11 @@
*/
final class TakeProfit extends BaseOrder
{
+ /**
+ * @var SeriesCounter
+ */
+ private $seriesCounter = null;
+
private $price = null;
private $extremumPrice = null;
@@ -29,6 +34,20 @@ public static function create()
/**
* @return TakeProfit
*/
+ public function setSeriesCounter(SeriesCounter $seriesCounter)
+ {
+ $this->seriesCounter = $seriesCounter;
+ return $this;
+ }
+
+ public function getSeriesCounter()
+ {
+ return $this->seriesCounter;
+ }
+
+ /**
+ * @return TakeProfit
+ */
public function setPrice($price)
{
$this->price = $price;
@@ -83,6 +102,14 @@ public function simpleHandle($value, $realizePrice = null)
? $realizePrice
: $value
);
+
+ Log::me()->add(
+ __CLASS__.': realized with price '
+ .$this->getRealizationPrice().' and count '.$this->getCount().' ('.$this->getSecurity()->getId().')'
+ );
+
+ if ($this->getSeriesCounter())
+ $this->getSeriesCounter()->win();
} else
$this->updateExtremum($value);
}
View
29 tests/cases/business/SeriesCounterTestCase.class.php
@@ -0,0 +1,29 @@
+<?php
+ namespace tradeSystem\tests;
+
+ /**
+ * @license http://www.opensource.org/licenses/bsd-license.php BSD
+ * @author Evgeniy Sokolov <ewgraf@gmail.com>
+ */
+ final class SeriesCounterTestCase extends TradeSystemTestCase
+ {
+ public function testCommon()
+ {
+ $counter = \tradeSystem\SeriesCounter::create();
+
+ $counter->win();
+ $counter->win();
+ $counter->win();
+ $counter->lose();
+ $counter->lose();
+ $counter->win();
+ $counter->lose();
+ $counter->lose();
+ $counter->lose();
+ $counter->lose();
+
+ $this->assertSame(3, $counter->getWinSeries());
+ $this->assertSame(4, $counter->getLoseSeries());
+ }
+ }
+?>
View
8 tests/cases/business/strategy/MACDHistogrammReverseStrategyTestCase.class.php
@@ -7,18 +7,17 @@
*/
final class MACDHistogrammReverseStrategyTestCase extends TradeSystemTestCase
{
- private $savedPortfolio = null;
- private $savedPositionStorage = null;
-
public function setUp()
{
$this->saveSingleton(\tradeSystem\Portfolio::me());
+ $this->saveSingleton(\tradeSystem\Log::me());
$this->saveSingleton(\tradeSystem\PositionStorage::me());
}
public function tearDown()
{
$this->restoreSingleton(\tradeSystem\Portfolio::me());
+ $this->restoreSingleton(\tradeSystem\Log::me());
$this->restoreSingleton(\tradeSystem\PositionStorage::me());
}
@@ -42,12 +41,15 @@ public function testCommon()
addIndicator($MACDSignal)->
addIndicator($MACDHistogramm);
+ $seriesCounter = \tradeSystem\SeriesCounter::create();
+
$strategy =
new \tradeSystem\MACDHistogrammReverseStrategy(
new \tradeSystem\EndStrategy()
);
$strategy->
+ setSeriesCounter($seriesCounter)->
setIndicator($MACDHistogramm)->
setSecurity(\tradeSystem\Security::create()->setId('SBER3'));
View
2  tests/cases/business/strategy/orders/StopLossTestCase.class.php
@@ -10,12 +10,14 @@
public function setUp()
{
$this->saveSingleton(\tradeSystem\Portfolio::me());
+ $this->saveSingleton(\tradeSystem\Log::me());
$this->saveSingleton(\tradeSystem\PositionStorage::me());
}
public function tearDown()
{
$this->restoreSingleton(\tradeSystem\Portfolio::me());
+ $this->restoreSingleton(\tradeSystem\Log::me());
$this->restoreSingleton(\tradeSystem\PositionStorage::me());
}
View
2  tests/cases/business/strategy/orders/TakeProfitTestCase.class.php
@@ -10,12 +10,14 @@
public function setUp()
{
$this->saveSingleton(\tradeSystem\Portfolio::me());
+ $this->saveSingleton(\tradeSystem\Log::me());
$this->saveSingleton(\tradeSystem\PositionStorage::me());
}
public function tearDown()
{
$this->restoreSingleton(\tradeSystem\Portfolio::me());
+ $this->restoreSingleton(\tradeSystem\Log::me());
$this->restoreSingleton(\tradeSystem\PositionStorage::me());
}
Please sign in to comment.
Something went wrong with that request. Please try again.