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

__invoke()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 45
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
dl 0
loc 45
ccs 0
cts 18
cp 0
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 19
nc 7
nop 2
crap 56
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 - Trend vs Cycle Mode
11
 *
12
 *
13
 * Simply tell us if the market is
14
 * either trending or cycling, with an additional parameter the method returns
15
 * the number of days we have been in a trend or a cycle.
16
 */
17
class HilbertTransformTrendVersusCycleModeIndicator implements Indicator
18
{
19
20
    public function __invoke(Collection $ohlcv, bool $numperiods = false): int
21
    {
22
23
        $a_htm = trader_ht_trendmode($ohlcv->get('close'));
24
25
        if (false === $a_htm) {
26
            throw new NotEnoughDataPointsException('Not enough data points');
27
        }
28
29
30
        if (!$a_htm) {
31
            throw new \RuntimeException('Not enough data points. Maybe clear cache and start over.');
32
        }
33
34
        $htm = array_pop($a_htm);
35
36
        /**
37
         *  We can return the number of periods we have been
38
         *  in either a trend or a cycle by calling this again with
39
         *  $numperiods == true
40
         */
41
        if ($numperiods) {
42
            $nump = 1;
43
44
            for ($b = 0; $b < count($a_htm); $b++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
45
                $test = array_pop($a_htm);
46
                if ($test == $htm) {
47
                    $nump++;
48
                } else {
49
                    break;
50
                }
51
            }
52
53
            return $nump;
54
        }
55
56
        /**
57
         *  Otherwise we just return if we are in a trend or not.
58
         */
59
        if ($htm == 1) {
60
            return 1; // we are in a trending mode
61
        }
62
63
        return 0; // we are cycling.
64
    }
65
}
66