OnBalanceVolumeIndicator::__invoke()   B
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 0
cts 13
cp 0
rs 8.7624
c 0
b 0
f 0
cc 5
eloc 13
nc 3
nop 1
crap 30
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 OnBalanceVolumeIndicator implements Indicator
11
{
12
    /**
13
     * Invoke the indicator.
14
     *
15
     * @param Collection $ohlcv
16
     *
17
     * @return int
18
     *
19
     * @throws Throwable
20
     */
21
    public function __invoke(Collection $ohlcv): int
22
    {
23
        $obv = trader_obv(
24
            $ohlcv->get('close'),
25
            $ohlcv->get('volume')
26
        );
27
28
        throw_unless($obv, NotEnoughDataException::class);
29
30
        $currentValue = array_pop($obv);
31
        $priorValue   = array_pop($obv);
32
        $earlierValue = array_pop($obv);
33
34
        if (($currentValue > $priorValue) && ($priorValue > $earlierValue)) {
35
            return static::BUY;
36
        } elseif (($currentValue < $priorValue) && ($priorValue < $earlierValue)) {
37
            return static::SELL;
38
        }
39
40
        return static::HOLD;
41
    }
42
}
43