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