Completed
Pull Request — master (#2)
by Evgenii
01:19
created

ChangeMomentumOscillatorIndicator::__invoke()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 12
cp 0
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 12
nc 4
nop 2
crap 20
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
class ChangeMomentumOscillatorIndicator implements Indicator
10
{
11
    /**
12
     * Invoke the indicator.
13
     *
14
     * @param Collection $ohlcv
15
     * @param int        $period
16
     *
17
     * @return int
18
     */
19
    public function __invoke(Collection $ohlcv, int $period = 14): int
20
    {
21
        $cmo = trader_cmo(
22
            $ohlcv->get('close'),
23
            $period
24
        );
25
26
        if (false === $cmo) {
27
            throw new NotEnoughDataPointsException;
28
        }
29
30
        $cmoValue = array_pop($cmo);
31
32
        if ($cmoValue < -50) {
33
            return static::BUY;
34
        } elseif ($cmoValue > 50) {
35
            return static::SELL;
36
        }
37
38
        return static::HOLD;
39
    }
40
}
41