Exponential Moving Average in php

喜你入骨 提交于 2021-01-27 18:54:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!