Completed
Branch master (5a7b19)
by Evgenii
02:31
created

MarketMeannessIndexIndicator::__invoke()   C

Complexity

Conditions 8
Paths 12

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 0
cts 16
cp 0
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 16
nc 12
nop 2
crap 72
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
     * Invoke the indicator.
12
     *
13
     * @param Collection $ohlcv
14
     * @param int        $timePeriod
15
     *
16
     * @return int
17
     */
18
    public function __invoke(Collection $ohlcv, int $timePeriod = 200): int
19
    {
20
        $close  = $ohlcv->get('close');
21
        $length = count($close);
22
        $median = array_sum($close) / $length;
23
24
        $nl = $nh = 0;
25
26
        for ($a = 0; $a < $length; $a++) {
27
            if ($close[$a] > $median && $close[$a] > @$close[$a - 1]) {
28
                $nl++;
29
            } elseif ($close[$a] < $median && $close[$a] < @$close[$a - 1]) {
30
                $nh++;
31
            }
32
        }
33
34
        $mmi = 100. * ($nl + $nh) / ($length - 1);
35
36
        if ($mmi < 75) {
37
            return static::BUY;
38
        } elseif ($mmi > 75) {
39
            return static::SELL;
40
        }
41
42
        return static::HOLD;
43
    }
44
}
45