| @@ 10-43 (lines=34) @@ | ||
| 7 | use Laratrade\Indicators\Exceptions\NotEnoughDataException; |
|
| 8 | use Throwable; |
|
| 9 | ||
| 10 | class AverageDirectionalMovementIndexIndicator implements Indicator |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * Invoke the indicator. |
|
| 14 | * |
|
| 15 | * @param Collection $ohlcv |
|
| 16 | * @param int $timePeriod |
|
| 17 | * |
|
| 18 | * @return int |
|
| 19 | * |
|
| 20 | * @throws Throwable |
|
| 21 | */ |
|
| 22 | public function __invoke(Collection $ohlcv, int $timePeriod = 14): int |
|
| 23 | { |
|
| 24 | $adx = trader_adx( |
|
| 25 | $ohlcv->get('high'), |
|
| 26 | $ohlcv->get('low'), |
|
| 27 | $ohlcv->get('close'), |
|
| 28 | $timePeriod |
|
| 29 | ); |
|
| 30 | ||
| 31 | throw_unless($adx, NotEnoughDataException::class); |
|
| 32 | ||
| 33 | $adxValue = array_pop($adx); |
|
| 34 | ||
| 35 | if ($adxValue > 50) { |
|
| 36 | return static::BUY; |
|
| 37 | } elseif ($adxValue < 20) { |
|
| 38 | return static::SELL; |
|
| 39 | } |
|
| 40 | ||
| 41 | return static::HOLD; |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||
| @@ 10-43 (lines=34) @@ | ||
| 7 | use Laratrade\Indicators\Exceptions\NotEnoughDataException; |
|
| 8 | use Throwable; |
|
| 9 | ||
| 10 | class CommodityChannelIndexIndicator implements Indicator |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * Invoke the indicator. |
|
| 14 | * |
|
| 15 | * @param Collection $ohlcv |
|
| 16 | * @param int $timePeriod |
|
| 17 | * |
|
| 18 | * @return int |
|
| 19 | * |
|
| 20 | * @throws Throwable |
|
| 21 | */ |
|
| 22 | public function __invoke(Collection $ohlcv, int $timePeriod = 14): int |
|
| 23 | { |
|
| 24 | $cci = trader_cci( |
|
| 25 | $ohlcv->get('high'), |
|
| 26 | $ohlcv->get('low'), |
|
| 27 | $ohlcv->get('close'), |
|
| 28 | $timePeriod |
|
| 29 | ); |
|
| 30 | ||
| 31 | throw_unless($cci, NotEnoughDataException::class); |
|
| 32 | ||
| 33 | $cciValue = array_pop($cci); |
|
| 34 | ||
| 35 | if ($cciValue < -100) { |
|
| 36 | return static::BUY; |
|
| 37 | } elseif ($cciValue > 100) { |
|
| 38 | return static::SELL; |
|
| 39 | } |
|
| 40 | ||
| 41 | return static::HOLD; |
|
| 42 | } |
|
| 43 | } |
|
| 44 | ||
| @@ 10-44 (lines=35) @@ | ||
| 7 | use Laratrade\Indicators\Exceptions\NotEnoughDataException; |
|
| 8 | use Throwable; |
|
| 9 | ||
| 10 | class MoneyFlowIndexIndicator implements Indicator |
|
| 11 | { |
|
| 12 | /** |
|
| 13 | * Invoke the indicator. |
|
| 14 | * |
|
| 15 | * @param Collection $ohlcv |
|
| 16 | * @param int $timePeriod |
|
| 17 | * |
|
| 18 | * @return int |
|
| 19 | * |
|
| 20 | * @throws Throwable |
|
| 21 | */ |
|
| 22 | public function __invoke(Collection $ohlcv, int $timePeriod = 14): int |
|
| 23 | { |
|
| 24 | $mfi = trader_mfi( |
|
| 25 | $ohlcv->get('high'), |
|
| 26 | $ohlcv->get('low'), |
|
| 27 | $ohlcv->get('close'), |
|
| 28 | $ohlcv->get('volume'), |
|
| 29 | $timePeriod |
|
| 30 | ); |
|
| 31 | ||
| 32 | throw_unless($mfi, NotEnoughDataException::class); |
|
| 33 | ||
| 34 | $mfiValue = array_pop($mfi); |
|
| 35 | ||
| 36 | if ($mfiValue < -10) { |
|
| 37 | return static::BUY; |
|
| 38 | } elseif ($mfiValue > 80) { |
|
| 39 | return static::SELL; |
|
| 40 | } |
|
| 41 | ||
| 42 | return static::HOLD; |
|
| 43 | } |
|
| 44 | } |
|
| 45 | ||