Skip to content

Commit

Permalink
Added functions from TA-lib
Browse files Browse the repository at this point in the history
* accbands
* avgdev
* imi
  • Loading branch information
jb-lopez committed Nov 26, 2023
1 parent 29cccb4 commit decca01
Show file tree
Hide file tree
Showing 15 changed files with 3,171 additions and 2,829 deletions.
36 changes: 18 additions & 18 deletions source/LupeTrader.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public static function acos(array $real): array
*/
public static function chaikinAccumulationDistributionLine(array $high, array $low, array $close, array $volume): array
{
$count = self::verifyArrayCounts([$high, $low, $close, $volume]);
$result = new \SplFixedArray($count + 1);
$count = self::verifyArrayCounts([$high, $low, $close, $volume]);
$result = new \SplFixedArray($count + 1);
$moneyFlowVolume = 0.0;
for ($i = 0; $i <= $count; $i++) {
$denominator = $high[$i] - $low[$i];
if ($denominator > 0) {
$moneyFlowMultiplier = (($close[$i] - $low[$i]) - ($high[$i] - $close[$i])) / $denominator;
$moneyFlowVolume += ($moneyFlowMultiplier * $volume[$i]);
$moneyFlowVolume += ($moneyFlowMultiplier * $volume[$i]);
}
$result[$i] = $moneyFlowVolume;
}
Expand Down Expand Up @@ -101,19 +101,19 @@ public static function add(array $real0, array $real1): array
*/
public static function chaikinOscillator(array $high, array $low, array $close, array $volume, int $fastPeriod = 3, int $slowPeriod = 10): array
{
$count = self::verifyArrayCounts([$high, $low, $close, $volume]);
$fastK = 2 / ($fastPeriod + 1);
$slowK = 2 / ($slowPeriod + 1);
$count = self::verifyArrayCounts([$high, $low, $close, $volume]);
$fastK = 2 / ($fastPeriod + 1);
$slowK = 2 / ($slowPeriod + 1);
$oneMinusFastK = 1 - $fastK;
$oneMinusSlowK = 1 - $slowK;

$ad = self::ad($high, $low, $close, $volume);
$ad = self::ad($high, $low, $close, $volume);
$fastEma = $slowEma = $ad[0];
$output = [];
$output = [];

for ($i = 1; $i <= $count; $i++) {
$fastEma = ($fastK * $ad[$i]) + ($oneMinusFastK * $fastEma);
$slowEma = ($slowK * $ad[$i]) + ($oneMinusSlowK * $slowEma);
$fastEma = ($fastK * $ad[$i]) + ($oneMinusFastK * $fastEma);
$slowEma = ($slowK * $ad[$i]) + ($oneMinusSlowK * $slowEma);
$output[$i] = $fastEma - $slowEma;
}

Expand Down Expand Up @@ -149,10 +149,10 @@ public static function adosc(array $high, array $low, array $close, array $volum
*/
public static function averageDirectionalMovementIndex(array $high, array $low, array $close, int $timePeriod = 14): array
{
$count = self::verifyArrayCounts([$high, $low, $close]);
$plus = self::plusDI($high, $low, $close, $timePeriod);
$minus = self::minusDI($high, $low, $close, $timePeriod);
$sum = self::add($plus, $minus);
$count = self::verifyArrayCounts([$high, $low, $close]);
$plus = self::plusDI($high, $low, $close, $timePeriod);
$minus = self::minusDI($high, $low, $close, $timePeriod);
$sum = self::add($plus, $minus);
$result = new \SplFixedArray($count + 1);
for ($i = 0; $i <= $count; $i++) {
$result[$i] = $sum[$i] > 0 ? 100 * abs($plus[$i] - $minus[$i]) / $sum[$i] : 0;
Expand Down Expand Up @@ -216,12 +216,12 @@ public static function slowstochrsi(
int $slowD_Period = 3,
int $slowD_MAType = MovingAverageType::SMA
): array {
$real = \array_values($real);
$real = \array_values($real);
$endIdx = count($real) - 1;
$rsi = [];
$rsi = [];
self::checkForError(self::getMomentumIndicators()::rsi(0, $endIdx, $real, $rsi_period, self::$outBegIdx, self::$outNBElement, $rsi));
$rsi = array_values($rsi);
$endIdx = self::verifyArrayCounts([&$rsi]);
$rsi = array_values($rsi);
$endIdx = self::verifyArrayCounts([&$rsi]);
$outSlowK = [];
$outSlowD = [];
self::checkForError(
Expand Down
18 changes: 9 additions & 9 deletions source/TALib/Classes/CandleSetting.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,25 @@ class CandleSetting
public function __construct(int $settingType, int $rangeType = null, int $avgPeriod = null, float $factor = null)
{
$this->settingType = $settingType;
$this->rangeType = $rangeType;
$this->avgPeriod = $avgPeriod;
$this->factor = $factor;
$this->rangeType = $rangeType;
$this->avgPeriod = $avgPeriod;
$this->factor = $factor;
}

public function CopyFrom(CandleSetting $source)
{
$this->settingType = $source->settingType;
$this->rangeType = $source->rangeType;
$this->avgPeriod = $source->avgPeriod;
$this->factor = $source->factor;
$this->rangeType = $source->rangeType;
$this->avgPeriod = $source->avgPeriod;
$this->factor = $source->factor;
}

public function CandleSetting(CandleSetting $that)
{
$this->settingType = $that->settingType;
$this->rangeType = $that->rangeType;
$this->avgPeriod = $that->avgPeriod;
$this->factor = $that->factor;
$this->rangeType = $that->rangeType;
$this->avgPeriod = $that->avgPeriod;
$this->factor = $that->factor;
}

}
Loading

0 comments on commit decca01

Please sign in to comment.