MovingAverageCrossoverDivergenceIndicator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
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 36
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
     * 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