binance – How to calculate MACD using PHP?-ThrowExceptions

Exception or error:

I get similar MACD and Signal values, using the following class in Binance:

I got this code from:

https://github.com/hurdad/doo-forex/blob/master/protected/class/Technical%20Indicators/MACD.php

How can I modify the class to arrive at the exact value?

How to solve:

You can use the php-trader lib, note that it works as CLI only.

But this is fairly simple math:

MACD = EMA26 – EMA12

enter image description here

enter image description here

Answer:

/*
* Exponential moving average (EMA)
*
* The start of the EPA is seeded with the first data point.
* Then each day after that:
* EMAtoday = α⋅xtoday + (1-α)EMAyesterday
*
* where
* α: coefficient that represents the degree of weighting decrease, a constant smoothing factor between 0 and 1.
*
* @param array $numbers
* @param int $n Length of the EPA
* @return array of exponential moving averages
*/

function exponentialMovingAverage( $numbers, $n)
{
$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;}

Leave a Reply

Your email address will not be published. Required fields are marked *