PHP中的指数移动平均线 [英] Exponential Moving Average in php

查看:130
本文介绍了PHP中的指数移动平均线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP计算EMA(指数移动平均线)值.

I want to calculate the EMA (Exponential Moving Average) value in PHP.

我尝试使用以下代码,但它给了我500错误.

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计算功能 trader-ema

PHP: EMA calculation function trader-ema

尝试了长时间的Google搜索,但在PHP中对此没有任何帮助.因此,我不知道要计算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.

编辑-1:已安装的扩展程序

我已经安装了所有必需的扩展,现在我得到了输出.但这似乎没有提供适当的输出.

I've installed all the necessary extensions, Now I am getting the output. But it doesn't seems giving proper output.

我认为PHP用于计算EMA的功能无法正常工作.在这方面的任何帮助将不胜感激.

I think PHP function for calculating EMA is not working properly. Any help in this would be greatly appreciated.

推荐答案

我建议使用以下数学库:https://github.com/markrogoyski/math-php

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;
}

这篇关于PHP中的指数移动平均线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆