|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Laratrade\Indicators; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Laratrade\Indicators\Contracts\Indicator; |
|
7
|
|
|
use Laratrade\Indicators\Exceptions\NotEnoughDataPointsException; |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* |
|
12
|
|
|
* Moving Average Crossover Divergence (MACD) indicator as a buy/sell signal. |
|
13
|
|
|
* When the MACD signal less than 0, the price is trending down and it's time to sell. |
|
14
|
|
|
* When the MACD signal greater than 0, the price is trending up it's time to buy. |
|
15
|
|
|
* |
|
16
|
|
|
* Used to catch trends early and can also help us spot trend reversals. |
|
17
|
|
|
* It consists of 2 moving averages (1 fast, 1 slow) and vertical lines called a histogram, |
|
18
|
|
|
* which measures the distance between the 2 moving averages. |
|
19
|
|
|
* Contrary to what many people think, the moving average lines are NOT moving averages of the price. |
|
20
|
|
|
* They are moving averages of other moving averages. |
|
21
|
|
|
* MACD’s downfall is its lag because it uses so many moving averages. |
|
22
|
|
|
* One way to use MACD is to wait for the fast line to “cross over” or “cross under” the slow line and |
|
23
|
|
|
* enter the trade accordingly because it signals a new trend. |
|
24
|
|
|
*/ |
|
25
|
|
|
class MovingAverageCrossoverDivergenceIndicator implements Indicator |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
public function __invoke(Collection $ohlcv, int $period1 = 12, int $period2 = 26, int $period3 = 9): int |
|
29
|
|
|
{ |
|
30
|
|
|
|
|
31
|
|
|
/* |
|
32
|
|
|
* Create the MACD signal and pass in the three parameters: fast period, slow period, and the signal. |
|
33
|
|
|
* we will want to tweak these periods later for now these are fine. |
|
34
|
|
|
* data, fast period, slow period, signal period (2-100000) |
|
35
|
|
|
* array $real [, integer $fastPeriod [, integer $slowPeriod [, integer $signalPeriod ]]] |
|
36
|
|
|
*/ |
|
37
|
|
|
|
|
38
|
|
|
$macd = trader_macd( |
|
39
|
|
|
$ohlcv->get('close'), |
|
40
|
|
|
$period1, |
|
41
|
|
|
$period2, |
|
42
|
|
|
$period3 |
|
43
|
|
|
); |
|
44
|
|
|
|
|
45
|
|
|
if (false === $macd) { |
|
46
|
|
|
throw new NotEnoughDataPointsException('Not enough data points'); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$macd_raw = $macd[0]; |
|
51
|
|
|
$signal = $macd[1]; |
|
52
|
|
|
$hist = $macd[2]; |
|
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
//If not enough Elements for the Function to complete |
|
55
|
|
|
if (!$macd || !$macd_raw) { |
|
56
|
|
|
throw new NotEnoughDataPointsException('Not enough data points'); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
//$macd = $macd_raw[count($macd_raw)-1] - $signal[count($signal)-1]; |
|
|
|
|
|
|
60
|
|
|
$macd = (array_pop($macd_raw) - array_pop($signal)); |
|
61
|
|
|
|
|
62
|
|
|
// Close position for the pair when the MACD signal is negative |
|
63
|
|
|
|
|
64
|
|
|
if ($macd < 0) { |
|
65
|
|
|
return static::SELL; |
|
66
|
|
|
// Enter the position for the pair when the MACD signal is positive |
|
67
|
|
|
} elseif ($macd > 0) { |
|
68
|
|
|
return static::BUY; |
|
69
|
|
|
} else { |
|
70
|
|
|
return static::HOLD; |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
} |
|
75
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.