Completed
Pull Request — master (#7)
by Evgenii
02:50
created

__invoke()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 0
cts 17
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 17
nc 15
nop 2
crap 72
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 HilbertTransformInstantaneousTrendlineIndicator implements Indicator
11
{
12
    /**
13
     * The shortcut name.
14
     *
15
     * @var string
16
     */
17
    const SHORTCUT = 'htit';
18
19
    /**
20
     * Invoke the indicator.
21
     *
22
     * @param Collection $ohlcv
23
     * @param int        $timePeriod
24
     *
25
     * @return int
26
     *
27
     * @throws Throwable
28
     */
29
    public function __invoke(Collection $ohlcv, int $timePeriod = 4): int
30
    {
31
        $htl = trader_ht_trendline($ohlcv->get('close'));
32
33
        throw_unless($htl, NotEnoughDataException::class);
34
35
        $wma = trader_wma($ohlcv->get('close'), $timePeriod);
36
37
        throw_unless($htl, NotEnoughDataException::class);
38
39
        $uptrend = $downtrend = $declared = 0;
40
41
        for ($i = 0; $i < 5; $i++) {
42
            $htlValue = array_pop($htl);
43
            $wmaValue = array_pop($wma);
44
45
            $uptrend   += ($wmaValue > $htlValue ? 1 : 0);
46
            $downtrend += ($wmaValue < $htlValue ? 1 : 0);
47
48
            $declared = (($wmaValue - $htlValue) / $htlValue);
49
        }
50
51
        if ($uptrend || $declared >= 0.15) {
52
            return static::BUY;
53
        } elseif ($downtrend || $declared <= 0.15) {
54
            return static::SELL;
55
        }
56
57
        return static::HOLD;
58
    }
59
}
60