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

MarketMeannessIndexIndicator::__invoke()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
dl 0
loc 31
ccs 0
cts 18
cp 0
rs 4.909
c 0
b 0
f 0
cc 9
eloc 18
nc 24
nop 2
crap 90
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