説明

trader_ema(array \$real, int \$timePeriod = ?): array

パラメータ

real

timePeriod

Number of period. Valid range from 2 to 100000.

戻り値

Returns an array with calculated data or false on failure.

Ash Christos
3 years ago
This method works fine. If you're finding that the EMA and SMA results are the same, the precision setting might still be the default or not tuned to your use case. (For example floats with more than 3 levels, sathoshi's, etc)

# this is needed for 0.00XXXXZ levels
andy at siliconrockstar dot com
2 years ago
The trader_ema() function works correctly if you understand what the second argument is. The second argument is used to group the values into overlapping periods. Within the periods, the numbers undergo a simple average calculation.

So if you call trader_ema(\$array, 6), and your array only has six values, you're going to get back a simple average, because there is no previous data to weight the value.

If you call trader_ema(\$array, 3), then your six array values will be grouped into four overlapping groups of three, and you'll get back four values, each representing the EMA for that period.

Below is the output of trader_ema(array(1,2,2,1,3,4), 3) and trader_sma(array(1,2,2,1,3,4), 3). You can see the first value is the same for both the EMA and SMA calculations.

array(4) {
=>
float(1.6666666667)
=>
float(1.3333333333)
=>
float(2.1666666667)
=>
float(3.0833333333)
}

array(4) {
=>
float(1.6666666667)
=>
float(1.6666666667)
=>
float(2)
=>
float(2.6666666667)
}
kazemi dot milad at gmail dot com
3 years ago
this return just simple moving avrage
for get ema correct use this code
\$number is data array and \$n is number of period
example:
\$number    => last value
\$number[n]    =>first value

function exponentialMovingAverage(array \$numbers, int \$n): array
{
\$numbers=array_reverse(\$numbers);
\$m   = count(\$numbers);
\$α   = 2 / (\$n + 1);
\$EMA = [];

// Start off by seeding with the first data point
\$EMA[] = \$numbers;

// Each day after: EMAtoday = α⋅xtoday + (1-α)EMAyesterday
for (\$i = 1; \$i < \$m; \$i++) {
\$EMA[] = (\$α * \$numbers[\$i]) + ((1 - \$α) * \$EMA[\$i - 1]);
}
\$EMA=array_reverse(\$EMA);
return \$EMA;
}
-1
Anonymous
2 years ago
As noted,  this function simply doesnt work.  It returns MA values.
-11
manoj dot monu23 at gmail dot com
7 years ago
the trader_ema() function does'nt work correctly , it calculate just average of last period entries. Follow the following code for trader_ema:
function EMACalculator(\$limit,\$array)
{
\$EMA_previous_day = \$array;
//print_r(\$array);
\$multiplier1 = (2/\$limit+1);
\$EMA[]=array();
\$EMA = \$array;
\$Close= \$array;

while(\$limit)
{
//echo"EMA is \$EMA\n";
\$EMA = (\$Close - \$EMA_previous_day) * \$multiplier1 + \$EMA_previous_day;
\$EMA_previous_day= \$EMA;

\$limit--;
}
return \$EMA;
}
where \$limit accept the period of ema  and \$array : accept array of data for ema calculation. 