问题
I want to calculate the EMA (Exponential Moving Average) value in PHP.
I've tried with following code but it's giving me 500 error.
$real = array(12,15,17,19,21,25,28,12,15,16);
$timePeriod = 3;
$data = trader_ema($real,$timePeriod);
var_dump($data);
PHP: EMA calculation function trader-ema
Tried with long time Googling but not getting any help on this in PHP. So, I've no clue what needs to be done to calculate the EMA value.
Edit-1: Installed extensions
I've installed all the necessary extensions, Now I am getting the output. But it doesn't seems giving proper output.
I think PHP function for calculating EMA is not working properly. Any help in this would be greatly appreciated.
回答1:
I recommend to use the math library from: https://github.com/markrogoyski/math-php
public static function exponentialMovingAverage(array $numbers, int $n): array
{
$m = count($numbers);
$α = 2 / ($n + 1);
$EMA = [];
// Start off by seeding with the first data point
$EMA[] = $numbers[0];
// Each day after: EMAtoday = α⋅xtoday + (1-α)EMAyesterday
for ($i = 1; $i < $m; $i++) {
$EMA[] = ($α * $numbers[$i]) + ((1 - $α) * $EMA[$i - 1]);
}
return $EMA;
}
来源:https://stackoverflow.com/questions/39444699/exponential-moving-average-in-php