Completed
Branch master (5a7b19)
by Evgenii
02:31
created

__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 4
crap 12
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Illuminate\Support\Collection;
6
use Laratrade\Indicators\Contracts\Indicator;
7
use Laratrade\Indicators\Exceptions\NotEnoughDataException;
8
use Throwable;
9
10
class MovingAverageCrossoverDivergenceIndicator implements Indicator
11
{
12
    /**
13
     * Invoke the indicator.
14
     *
15
     * @param Collection $ohlcv
16
     * @param int        $timePeriod
17
     * @param int        $slowPeriod
18
     * @param int        $signalPeriod
19
     *
20
     * @return int
21
     *
22
     * @throws Throwable
23
     */
24
    public function __invoke(Collection $ohlcv, int $timePeriod = 12, int $slowPeriod = 26, int $signalPeriod = 9): int
25
    {
26
        $macd = trader_macd(
27
            $ohlcv->get('close'),
28
            $timePeriod,
29
            $slowPeriod,
30
            $signalPeriod
31
        );
32
33
        throw_unless($macd, NotEnoughDataException::class);
34
35
        $macdValue = array_pop($macd[0]) - array_pop($macd[1]);
36
37
        if ($macdValue < 0) {
38
            return static::SELL;
39
        } elseif ($macdValue > 0) {
40
            return static::BUY;
41
        }
42
43
        return static::HOLD;
44
    }
45
}
46