Completed
Push — master ( 762814...853e43 )
by
unknown
9s
created

OnBalanceVolumeIndicator::__invoke()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 0
cts 12
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 13
nc 4
nop 2
crap 42
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
/**
10
 * On Balance Volume
11
 *
12
 * @see http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:on_balance_volume_obv
13
 *
14
 * signal assumption that volume precedes price on confirmation, divergence and breakouts
15
 * use with mfi to confirm
16
 */
17
class OnBalanceVolumeIndicator implements Indicator
18
{
19
20
    public function __invoke(Collection $ohlcv, int $period = 14): int
21
    {
22
23
        $obv = trader_obv($ohlcv->get('close'), $ohlcv->get('volume'));
24
25
        if (false === $obv) {
26
            throw new NotEnoughDataPointsException('Not enough data points');
27
        }
28
29
        $current_obv = array_pop($obv);
30
        $prior_obv = array_pop($obv);
31
        $earlier_obv = array_pop($obv);
32
33
        /**
34
         *   This forecasts a trend in the last three periods
35
         *   TODO: this needs to be tested more, we might need to look closer for crypto currencies
36
         */
37
        if (($current_obv > $prior_obv) && ($prior_obv > $earlier_obv)) {
38
            return static::BUY; // upwards momentum
39
        } elseif (($current_obv < $prior_obv) && ($prior_obv < $earlier_obv)) {
40
            return static::SELL; // downwards momentum
41
        } else {
42
            return static::HOLD;
43
        }
44
    }
45
}
46