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

MarketMeannessIndexIndicator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 35
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D __invoke() 0 31 9
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
 * Market Meanness Index (link) 
11
 *
12
 * This indicator is not a measure of how
13
 * grumpy the market is, it shows if we are currently in or out of a trend
14
 * based on price reverting to the mean.
15
 *
16
 * NO TALib specific function
17
 * Market Meanness Index - tendency to revert to the mean
18
 * currently moving in our out of a trend?
19
 * prevent loss by false trend signals
20
 *
21
 * if mmi > 75 then not trending
22
 * if mmi < 75 then trending
23
 *
24
 */
25
class MarketMeannessIndexIndicator implements Indicator
26
{
27
28
    public function __invoke(Collection $ohlcv, int $period = 200): int
29
    {
30
31
        $data_close = [];
32
        foreach ($ohlcv->get('close') as $point) {
33
            $data_close[] = $point;
34
        }
35
36
        $nl = $nh = 0;
37
        $len = count($data_close);
38
        $median = (array_sum($data_close) / $len);
39
40
        for ($a = 0; $a < $len; $a++) {
41
            if ($data_close[$a] > $median && $data_close[$a] > @$data_close[$a - 1]) {
42
                $nl++;
43
            } elseif ($data_close[$a] < $median && $data_close[$a] < @$data_close[$a - 1]) {
44
                $nh++;
45
            }
46
        }
47
48
        $mmi = 100. * ($nl + $nh) / ($len - 1);
49
        if ($mmi < 75) {
50
            return static::BUY;
51
        }
52
53
        if ($mmi > 75) {
54
            return static::SELL;
55
        }
56
57
        return static::HOLD;
58
    }
59
}
60