MarketMeannessIndexIndicator   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 37
ccs 0
cts 16
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C __invoke() 0 26 8
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Illuminate\Support\Collection;
6
use Laratrade\Indicators\Contracts\Indicator;
7
8
class MarketMeannessIndexIndicator implements Indicator
9
{
10
    /**
11
     * Invoke the indicator.
12
     *
13
     * @param Collection $ohlcv
14
     * @param int        $timePeriod
15
     *
16
     * @return int
17
     */
18
    public function __invoke(Collection $ohlcv, int $timePeriod = 200): int
19
    {
20
        $close  = $ohlcv->get('close');
21
        $length = count($close);
22
        $median = array_sum($close) / $length;
23
24
        $nl = $nh = 0;
25
26
        for ($a = 0; $a < $length; $a++) {
27
            if ($close[$a] > $median && $close[$a] > @$close[$a - 1]) {
28
                $nl++;
29
            } elseif ($close[$a] < $median && $close[$a] < @$close[$a - 1]) {
30
                $nh++;
31
            }
32
        }
33
34
        $mmi = 100. * ($nl + $nh) / ($length - 1);
35
36
        if ($mmi < 75) {
37
            return static::BUY;
38
        } elseif ($mmi > 75) {
39
            return static::SELL;
40
        }
41
42
        return static::HOLD;
43
    }
44
}
45