HilbertTransformSinewaveIndicator   A
last analyzed

Complexity

Total Complexity 14

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 14
lcom 0
cbo 1
dl 0
loc 48
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 35 14
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 HilbertTransformSinewaveIndicator implements Indicator
11
{
12
    /**
13
     * Invoke the indicator.
14
     *
15
     * @param Collection $ohlcv
16
     * @param bool       $trend
17
     *
18
     * @return int
19
     *
20
     * @throws Throwable
21
     */
22
    public function __invoke(Collection $ohlcv, bool $trend = false): int
23
    {
24
        $hts = trader_ht_sine(
25
            $ohlcv->get('open'),
26
            $ohlcv->get('close')
27
        );
28
29
        throw_unless($hts, NotEnoughDataException::class);
30
31
        $dcsine   = array_pop($hts[1]);
32
        $p_dcsine = array_pop($hts[1]);
33
        // leadsine is the first one it looks like.
34
        $leadsine   = array_pop($hts[0]);
35
        $p_leadsine = array_pop($hts[0]);
36
37
        if ($trend) {
38
            if ($dcsine < 0 && $p_dcsine < 0 && $leadsine < 0 && $p_leadsine < 0) {
39
                return static::BUY; // uptrend
40
            } elseif ($dcsine > 0 && $p_dcsine > 0 && $leadsine > 0 && $p_leadsine > 0) {
41
                return static::SELL; // downtrend
42
            }
43
44
            return static::HOLD;
45
        }
46
47
        /** WE ARE NOT ASKING FOR THE TREND, RETURN A SIGNAL */
48
        if ($leadsine > $dcsine && $p_leadsine <= $p_dcsine) {
49
            return static::BUY; // buy
50
        }
51
        if ($leadsine < $dcsine && $p_leadsine >= $p_dcsine) {
52
            return static::SELL; // sell
53
        }
54
55
        return static::HOLD;
56
    }
57
}
58