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

HilbertTransformTrendVersusCycleModeIndicator   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 1
dl 0
loc 55
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 35 5
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 HilbertTransformTrendVersusCycleModeIndicator implements Indicator
11
{
12
    /**
13
     * The shortcut name.
14
     *
15
     * @var string
16
     */
17
    const SHORTCUT = 'httvcm';
18
19
    /**
20
     * Invoke the indicator.
21
     *
22
     * @param Collection $ohlcv
23
     * @param bool       $numPeriods
24
     *
25
     * @return int
26
     *
27
     * @throws Throwable
28
     */
29
    public function __invoke(Collection $ohlcv, bool $numPeriods = false): int
30
    {
31
        $htm = trader_ht_trendmode(
32
            $ohlcv->get('open'),
33
            $ohlcv->get('close')
34
        );
35
36
        throw_unless($htm, NotEnoughDataException::class);
37
38
        $htmValue = array_pop($htm);
39
40
        /**
41
         *  We can return the number of periods we have been
42
         *  in either a trend or a cycle by calling this again with
43
         *  $numperiods == true
44
         */
45
        if ($numPeriods) {
46
            $nump = 1;
47
48
            for ($b = 0; $b < count($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...
49
                $test = array_pop($htm);
50
                if ($test == $htmValue) {
51
                    $nump++;
52
                } else {
53
                    break;
54
                }
55
            }
56
57
            return $nump;
58
        } elseif ($htmValue == 1) {
59
            return static::BUY;
60
        }
61
62
        return static::HOLD;
63
    }
64
}
65