Completed
Pull Request — master (#3)
by
unknown
01:16
created

MarketMeannessIndexIndicator   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 36
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 36
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
/**
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