Completed
Branch master (5a7b19)
by Evgenii
02:31
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
     * Invoke the indicator.
14
     *
15
     * @param Collection $ohlcv
16
     * @param int        $timePeriod
17
     *
18
     * @return int
19
     *
20
     * @throws Throwable
21
     */
22
    public function __invoke(Collection $ohlcv, int $timePeriod = 4): int
23
    {
24
        $htl = trader_ht_trendline($ohlcv->get('close'));
25
26
        throw_unless($htl, NotEnoughDataException::class);
27
28
        $wma = trader_wma($ohlcv->get('close'), $timePeriod);
29
30
        throw_unless($htl, NotEnoughDataException::class);
31
32
        $uptrend = $downtrend = $declared = 0;
33
34
        for ($i = 0; $i < 5; $i++) {
35
            $htlValue = array_pop($htl);
36
            $wmaValue = array_pop($wma);
37
38
            $uptrend   += ($wmaValue > $htlValue ? 1 : 0);
39
            $downtrend += ($wmaValue < $htlValue ? 1 : 0);
40
41
            $declared = (($wmaValue - $htlValue) / $htlValue);
42
        }
43
44
        if ($uptrend || $declared >= 0.15) {
45
            return static::BUY;
46
        } elseif ($downtrend || $declared <= 0.15) {
47
            return static::SELL;
48
        }
49
50
        return static::HOLD;
51
    }
52
}
53