Completed
Pull Request — master (#7)
by Evgenii
02:50
created

MovingAverageCrossoverDivergenceIndicator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 43
ccs 0
cts 13
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 3
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
     * The shortcut name.
14
     *
15
     * @var string
16
     */
17
    const SHORTCUT = 'macd';
18
19
    /**
20
     * Invoke the indicator.
21
     *
22
     * @param Collection $ohlcv
23
     * @param int        $timePeriod
24
     * @param int        $slowPeriod
25
     * @param int        $signalPeriod
26
     *
27
     * @return int
28
     *
29
     * @throws Throwable
30
     */
31
    public function __invoke(Collection $ohlcv, int $timePeriod = 12, int $slowPeriod = 26, int $signalPeriod = 9): int
32
    {
33
        $macd = trader_macd(
34
            $ohlcv->get('close'),
35
            $timePeriod,
36
            $slowPeriod,
37
            $signalPeriod
38
        );
39
40
        throw_unless($macd, NotEnoughDataException::class);
41
42
        $macdValue = array_pop($macd[0]) - array_pop($macd[1]);
43
44
        if ($macdValue < 0) {
45
            return static::SELL;
46
        } elseif ($macdValue > 0) {
47
            return static::BUY;
48
        }
49
50
        return static::HOLD;
51
    }
52
}
53