Completed
Pull Request — master (#3)
by
unknown
01:16
created

HilbertTransformSinewaveIndicator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 15
lcom 0
cbo 2
dl 0
loc 43
ccs 0
cts 19
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 38 15
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
/**
11
 * Hilbert Transform - Sinewave (MESA indicator)
12
 *
13
 *
14
 * We are actually using DSP
15
 * on the prices to attempt to get a lag-free/low-lag indicator.
16
 * This indicator can be passed an extra parameter and it will tell you in
17
 * we are in a trend or not. (when used as an indicator do not use in a trending market)
18
 */
19
class HilbertTransformSinewaveIndicator implements Indicator
20
{
21
22
    public function __invoke(Collection $ohlcv, bool $trend = false): int
23
    {
24
25
        $hts        = trader_ht_sine($ohlcv->get('open'), $ohlcv->get('close'));
26
        if (false === $hts) {
27
            throw new NotEnoughDataPointsException('Not enough data points');
28
        }
29
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 the last two sets of both are negative */
39
            if ($dcsine < 0 && $p_dcsine < 0 && $leadsine < 0 && $p_leadsine < 0) {
40
                return static::BUY; // uptrend
41
            }
42
            /** if the last two sets of both are positive */
43
            if ($dcsine > 0 && $p_dcsine > 0 && $leadsine > 0 && $p_leadsine > 0) {
44
                return static::SELL; // downtrend
45
            }
46
47
            return static::HOLD;
48
        }
49
50
        /** WE ARE NOT ASKING FOR THE TREND, RETURN A SIGNAL */
51
        if ($leadsine > $dcsine && $p_leadsine <= $p_dcsine) {
52
            return static::BUY; // buy
53
        }
54
        if ($leadsine < $dcsine && $p_leadsine >= $p_dcsine) {
55
            return static::SELL; // sell
56
        }
57
58
        return static::HOLD;
59
    }
60
61
}
62