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

MoneyFlowIndexIndicator::__invoke()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 15

Duplication

Lines 24
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
dl 24
loc 24
ccs 0
cts 15
cp 0
rs 8.6845
c 0
b 0
f 0
cc 4
eloc 15
nc 4
nop 2
crap 20
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
 * Money Flow Index
11
 *
12
 * @see https://www.investopedia.com/terms/m/mfi.asp
13
 *
14
 * What is the 'Money Flow Index - MFI'
15
 *
16
 * The money flow index (MFI) is a momentum indicator that measures the inflow and
17
 * outflow of money into a security over a specific period of time. The MFI uses a
18
 * stock's price and volume to measure trading pressure. Because the MFI adds trading
19
 * volume to the relative strength index (RSI), it's sometimes referred to as volume-weighted RSI.
20
 *
21
 */
22 View Code Duplication
class MoneyFlowIndexIndicator implements Indicator
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
23
{
24
    public function __invoke(Collection $ohlcv, int $period = 14): int
25
    {
26
        $mfi = trader_mfi(
27
            $ohlcv->get('high'),
28
            $ohlcv->get('low'),
29
            $ohlcv->get('close'),
30
            $ohlcv->get('volume'),
31
            $period
32
        );
33
34
        if (false === $mfi) {
35
            throw new NotEnoughDataPointsException('Not enough data points');
36
        }
37
38
        $mfiValue = array_pop($mfi);
39
40
        if ($mfiValue < -10) {
41
            return static::BUY;
42
        } elseif ($mfiValue > 80) {
43
            return static::SELL;
44
        }
45
46
        return static::HOLD;
47
    }
48
}
49