Completed
Push — master ( 762814...853e43 )
by
unknown
9s
created

HilbertTransformSinewaveIndicator::__invoke()   C

Complexity

Conditions 15
Paths 7

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 0
cts 19
cp 0
rs 5.0504
c 0
b 0
f 0
cc 15
eloc 19
nc 7
nop 2
crap 240

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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