BollingerBandsIndicator::__invoke()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 31
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 17
cp 0
rs 8.8571
c 0
b 0
f 0
cc 3
eloc 23
nc 3
nop 5
crap 12
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 BollingerBandsIndicator implements Indicator
11
{
12
    /**
13
     * Invoke the indicator.
14
     *
15
     * @param Collection $ohlcv
16
     * @param int        $timePeriod
17
     * @param float      $nbDevUp
18
     * @param float      $nbDevDn
19
     * @param int        $mAType
20
     *
21
     * @return int
22
     *
23
     * @throws Throwable
24
     */
25
    public function __invoke(
26
        Collection $ohlcv,
27
        int $timePeriod = 10,
28
        float $nbDevUp = 2,
29
        float $nbDevDn = 2,
30
        int $mAType = 0
31
    ): int {
32
        $close      = $ohlcv->get('close');
33
        $closeValue = array_pop($close);
34
35
        $bbands = trader_bbands(
36
            $ohlcv->get('close'),
37
            $timePeriod,
38
            $nbDevUp,
39
            $nbDevDn,
40
            $mAType
41
        );
42
43
        throw_unless($bbands, NotEnoughDataException::class);
44
45
        $upperValue = array_pop($bbands[0]);
46
        $lowerValue = array_pop($bbands[2]);
47
48
        if ($closeValue <= $lowerValue) {
49
            return static::BUY;
50
        } elseif ($closeValue >= $upperValue) {
51
            return static::SELL;
52
        }
53
54
        return static::HOLD;
55
    }
56
}
57