|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mokka\Strategy; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
use DoctrineTest\InstantiatorTestAsset\AbstractClassAsset; |
|
7
|
|
|
use Mokka\Action\Action; |
|
8
|
|
|
use Mokka\Action\ActionInterface; |
|
9
|
|
|
use Mokka\Action\BuyAction; |
|
10
|
|
|
use Mokka\Action\IdleAction; |
|
11
|
|
|
use Mokka\Action\SellAction; |
|
12
|
|
|
use Mokka\Config\Logger; |
|
13
|
|
|
use Mokka\Exchange\ExchangeInterface; |
|
14
|
|
|
|
|
15
|
|
|
class StrategyCalculator |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var ExchangeInterface |
|
20
|
|
|
*/ |
|
21
|
|
|
private static $exchange; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var IndicatorInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private static $indicator; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var |
|
30
|
|
|
*/ |
|
31
|
|
|
private $symbol; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var |
|
35
|
|
|
*/ |
|
36
|
|
|
private $interval; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var |
|
40
|
|
|
*/ |
|
41
|
|
|
private $market; |
|
42
|
|
|
/** |
|
43
|
|
|
* StrategyCalculator constructor. |
|
44
|
|
|
* @param ExchangeInterface $exchange |
|
45
|
|
|
* @param IndicatorInterface $indicator |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
ExchangeInterface $exchange, |
|
49
|
|
|
IndicatorInterface $indicator |
|
50
|
|
|
) |
|
51
|
|
|
{ |
|
52
|
|
|
self::$exchange = $exchange; |
|
53
|
|
|
self::$indicator = $indicator; |
|
54
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function run(Logger $logger): Action |
|
58
|
|
|
{ |
|
59
|
|
|
//get last action and price from logs |
|
60
|
|
|
$lastAction = $logger |
|
|
|
|
|
|
61
|
|
|
->read() |
|
62
|
|
|
->where('market', '=', $this->getMarket()) |
|
63
|
|
|
->where('symbol', '=', $this->getSymbol()) |
|
64
|
|
|
->sortDesc('lastUpdate') |
|
65
|
|
|
->limit(1) |
|
66
|
|
|
->first(); |
|
67
|
|
|
|
|
68
|
|
|
$lastAction = new Action($lastAction); |
|
69
|
|
|
|
|
70
|
|
|
//calculate in loop by interval |
|
71
|
|
|
/** @var ActionInterface $actionType */ |
|
72
|
|
|
$actionType = self::$indicator->calculate($this->getSymbol(), $lastAction); |
|
73
|
|
|
|
|
74
|
|
|
if ($actionType->getType() == ActionInterface::TYPE_BUY) { |
|
75
|
|
|
$action = new BuyAction(); |
|
76
|
|
|
}elseif ($actionType->getType() == ActionInterface::TYPE_SELL) { |
|
77
|
|
|
$action = new SellAction(); |
|
78
|
|
|
}elseif ($actionType->getType() == ActionInterface::TYPE_IDLE) { |
|
79
|
|
|
$action = new IdleAction(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** @var ActionInterface $action */ |
|
83
|
|
|
$action->setType($actionType->getType()); |
|
|
|
|
|
|
84
|
|
|
$action->setSymbol($this->getSymbol()); |
|
|
|
|
|
|
85
|
|
|
$action->setMarket($this->getMarket()); |
|
|
|
|
|
|
86
|
|
|
$action->setPreviousPrice($lastAction->getActionPrice()); |
|
87
|
|
|
$action->setActionPrice($actionType->getActionPrice()); |
|
88
|
|
|
$action->setLastUpdate(time()); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
return $action; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return mixed |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getSymbol() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->symbol; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param mixed $symbol |
|
103
|
|
|
*/ |
|
104
|
|
|
public function setSymbol($symbol) |
|
105
|
|
|
{ |
|
106
|
|
|
$this->symbol = $symbol; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
|
public function getInterval() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->interval; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param mixed $interval |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setInterval($interval) |
|
121
|
|
|
{ |
|
122
|
|
|
$this->interval = $interval; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return mixed |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getMarket() |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->market; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param mixed $market |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setMarket($market) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->market = $market; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
} |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.