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

AwesomeOscillatorIndicator   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 1
dl 0
loc 39
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 29 6
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
        $highs = $ohlcv->get('high');
20
        $lows  = $ohlcv->get('low');
21
22
        $data = [];
23
        foreach ($highs as $key => $value) {
24
            $data[$key] = ($highs[$key] + $lows[$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
        $prior = (array_pop($sma3) - array_pop($sma4)); // last 'tick'
36
        $now   = (array_pop($sma1) - array_pop($sma2)); // current 'tick'
37
38
        if ($prior <= 0 && $now > 0) {
39
            return static::BUY;
40
        } elseif ($prior >= 0 && $now < 0) {
41
            return static::SELL;
42
        }
43
44
        return static::HOLD;
45
    }
46
}
47