| Conditions | 7 |
| Paths | 7 |
| Total Lines | 45 |
| Code Lines | 19 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 56 |
| Changes | 0 | ||
| 1 | <?php |
||
| 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++) { |
||
|
|
|||
| 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 |
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: