Completed
Pull Request — master (#7)
by Evgenii
02:50
created

MarketMeannessIndexIndicator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 44
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 44
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
     * The shortcut name.
12
     *
13
     * @var string
14
     */
15
    const SHORTCUT = 'mmi';
16
17
    /**
18
     * Invoke the indicator.
19
     *
20
     * @param Collection $ohlcv
21
     * @param int        $timePeriod
22
     *
23
     * @return int
24
     */
25
    public function __invoke(Collection $ohlcv, int $timePeriod = 200): int
26
    {
27
        $close  = $ohlcv->get('close');
28
        $length = count($close);
29
        $median = array_sum($close) / $length;
30
31
        $nl = $nh = 0;
32
33
        for ($a = 0; $a < $length; $a++) {
34
            if ($close[$a] > $median && $close[$a] > @$close[$a - 1]) {
35
                $nl++;
36
            } elseif ($close[$a] < $median && $close[$a] < @$close[$a - 1]) {
37
                $nh++;
38
            }
39
        }
40
41
        $mmi = 100. * ($nl + $nh) / ($length - 1);
42
43
        if ($mmi < 75) {
44
            return static::BUY;
45
        } elseif ($mmi > 75) {
46
            return static::SELL;
47
        }
48
49
        return static::HOLD;
50
    }
51
}
52