|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Mokka\Strategy\Indicator; |
|
4
|
|
|
|
|
5
|
|
|
use Mokka\Action\Action; |
|
6
|
|
|
use Mokka\Action\ActionInterface; |
|
7
|
|
|
use Mokka\Action\BuyAction; |
|
8
|
|
|
use Mokka\Action\IdleAction; |
|
9
|
|
|
use Mokka\Action\SellAction; |
|
10
|
|
|
use Mokka\Strategy\StrategyAbstract; |
|
11
|
|
|
use Mokka\Strategy\IndicatorInterface; |
|
12
|
|
|
|
|
13
|
|
|
class Percent extends StrategyAbstract implements IndicatorInterface |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
const FIXER = 100000000; |
|
17
|
|
|
/** |
|
18
|
|
|
* @param $symbol |
|
19
|
|
|
* @param $lastAction Action last trade action |
|
20
|
|
|
* @return ActionInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
public function calculate($symbol, Action $lastAction) : ActionInterface |
|
23
|
|
|
{ |
|
24
|
|
|
//current price of symbol |
|
25
|
|
|
$currentPrice = $this->exchange->getPrice($symbol) * self::FIXER; |
|
26
|
|
|
|
|
27
|
|
|
$lastActionPrice = $lastAction->getActionPrice() * self::FIXER; |
|
28
|
|
|
|
|
29
|
|
|
//logic is here |
|
30
|
|
|
$calculatedPercentOfPrice = ($lastActionPrice * $this->options['default_percent']) / 100; |
|
31
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
//buy signal |
|
34
|
|
|
if ( |
|
35
|
|
|
$lastAction->getType() == ActionInterface::TYPE_SELL |
|
36
|
|
|
&& |
|
37
|
|
|
$currentPrice <= $lastActionPrice - $calculatedPercentOfPrice |
|
38
|
|
|
) { |
|
39
|
|
|
$action = new BuyAction(); |
|
40
|
|
|
$action->setType(ActionInterface::TYPE_BUY); |
|
41
|
|
|
|
|
42
|
|
|
//sell signal |
|
43
|
|
|
} elseif ( |
|
44
|
|
|
$lastAction->getType() == ActionInterface::TYPE_BUY |
|
45
|
|
|
&& |
|
46
|
|
|
$currentPrice >= $calculatedPercentOfPrice + $lastActionPrice |
|
47
|
|
|
) { |
|
48
|
|
|
$action = new SellAction(); |
|
49
|
|
|
$action->setType(ActionInterface::TYPE_SELL); |
|
50
|
|
|
} else { |
|
51
|
|
|
$action = new IdleAction(); |
|
52
|
|
|
$action->setType(ActionInterface::TYPE_IDLE); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$lastActionPrice = number_format($lastActionPrice / self::FIXER, 8, '.', ''); |
|
56
|
|
|
$currentPrice = number_format($currentPrice / self::FIXER, 8, '.', ''); |
|
57
|
|
|
|
|
58
|
|
|
$action->setPreviousPrice($lastActionPrice); |
|
59
|
|
|
$action->setActionPrice($currentPrice); |
|
60
|
|
|
|
|
61
|
|
|
return $action; |
|
62
|
|
|
|
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
} |