AwesomeOscillatorIndicator::__invoke()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 29
ccs 0
cts 18
cp 0
rs 8.439
c 0
b 0
f 0
cc 6
eloc 18
nc 6
nop 1
crap 42
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Illuminate\Support\Collection;
6
use Laratrade\Indicators\Contracts\Indicator;
7
8
class AwesomeOscillatorIndicator implements Indicator
9
{
10
    /**
11
     * Invoke the indicator.
12
     *
13
     * @param Collection $ohlcv
14
     *
15
     * @return int
16
     */
17
    public function __invoke(Collection $ohlcv): int
18
    {
19
        $high = $ohlcv->get('high');
20
        $low  = $ohlcv->get('low');
21
22
        $data = [];
23
        foreach ($high as $key => $value) {
24
            $data[$key] = ($high[$key] + $low[$key]) / 2;
25
        }
26
27
        $sma1 = trader_sma($data, 5);
28
        $sma2 = trader_sma($data, 34);
29
30
        array_pop($data); // take most recent off
31
32
        $sma3 = trader_sma($data, 5);
33
        $sma4 = trader_sma($data, 34);
34
35
        $last    = (array_pop($sma3) - array_pop($sma4));
36
        $current = (array_pop($sma1) - array_pop($sma2));
37
38
        if ($last <= 0 && $current > 0) {
39
            return static::BUY;
40
        } elseif ($last >= 0 && $current < 0) {
41
            return static::SELL;
42
        }
43
44
        return static::HOLD;
45
    }
46
}
47