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

__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 21
loc 21
ccs 0
cts 13
cp 0
rs 9.3142
c 0
b 0
f 0
cc 3
eloc 13
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Laratrade\Indicators;
4
5
use Illuminate\Support\Collection;
6
use Laratrade\Indicators\Contracts\Indicator;
7
use Laratrade\Indicators\Exceptions\NotEnoughDataException;
8
use Throwable;
9
10 View Code Duplication
class AverageDirectionalMovementIndexIndicator 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...
11
{
12
    /**
13
     * The shortcut name.
14
     *
15
     * @var string
16
     */
17
    const SHORTCUT = 'admi';
18
19
    /**
20
     * Invoke the indicator.
21
     *
22
     * @param Collection $ohlcv
23
     * @param int        $timePeriod
24
     *
25
     * @return int
26
     *
27
     * @throws Throwable
28
     */
29
    public function __invoke(Collection $ohlcv, int $timePeriod = 14): int
30
    {
31
        $adx = trader_adx(
32
            $ohlcv->get('high'),
33
            $ohlcv->get('low'),
34
            $ohlcv->get('close'),
35
            $timePeriod
36
        );
37
38
        throw_unless($adx, NotEnoughDataException::class);
39
40
        $adxValue = array_pop($adx);
41
42
        if ($adxValue > 50) {
43
            return static::BUY;
44
        } elseif ($adxValue < 20) {
45
            return static::SELL;
46
        }
47
48
        return static::HOLD;
49
    }
50
}
51