Calculating EMA
See original GitHub issueI’m trying to calculate EMA(20) for the pre-last candle but the values don’t match those from Binance. More specifically, when I change previousDayEMA = candles[i].Close; // SMA(period, candles);
to hard-coded number from Binance, it works fine. Can someone explain me what I am missing and it would be nice if someone knows how to round it the same way Binance do and the same way I did, but by not hard-coding the value (6). Because this rounding works for TRXUSDT but for example not for BTCUSDT.
Rounding:
Math.Round(EMA, 6, MidpointRounding.AwayFromZero);
Code:
// Usage
var klines = _client.GetKlines("TRXUSDT", KlineInterval.ThirtyMinutes, limit: 21).Data.ToList();
var EMA20 = ExponentialMovingAverage.Calculate(20, klines);
// The actual code
using Binance.Net.Objects;
using System;
using System.Collections.Generic;
namespace Bot.Models.Indicators
{
public class ExponentialMovingAverage
{
public static decimal SMA(int period, List<BinanceKline> candles)
{
if (candles.Count >= period)
{
decimal sum = 0;
for (int i = 0; i < period; i++)
{
sum += candles[i].Close;
}
return sum / period;
}
return 0;
}
public static decimal EMA(decimal close, decimal previousDayEMA, decimal period)
{
decimal multiplier = 2 / (period + 1);
return (close - previousDayEMA) * multiplier + previousDayEMA;
}
public static decimal Calculate(int period, List<BinanceKline> candles)
{
decimal previousDayEMA = 0;
if (candles.Count >= period)
{
decimal sum = 0;
for (int i = 0; i < period; i++)
{
if (i == 0)
{
previousDayEMA = candles[i].Close; // SMA(period, candles);
}
decimal EMA = ExponentialMovingAverage.EMA(candles[i].Close, previousDayEMA, period);
Console.WriteLine(Math.Round(EMA, 6, MidpointRounding.AwayFromZero));
sum += EMA;
previousDayEMA = EMA;
}
return sum / period;
}
return 0;
}
}
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
What is EMA? How to Use Exponential Moving Average ...
The calculation for the SMA is straightforward. It is simply the sum of the stock's closing prices during a time period, divided by...
Read more >Exponential Moving Average (EMA)
To calculate a 10-day simple moving average, simply add the closing prices of the last 10 days and divide by 10. The 20-day...
Read more >Moving Averages - Simple and Exponential - ChartSchool
There are three steps to calculating an exponential moving average (EMA). First, calculate the simple moving average for the initial EMA value. An...
Read more >Exponential Moving Average (EMA)
As exemplified in the chart above, EMAs calculated over a fewer number of periods (i.e., based on more recent prices) show a higher...
Read more >Exponential Moving Average Formula | Example and Excel ...
Step 3: Now, finally, to calculate the EMA, we will use the formula above – (Closing Price – EMA of the previous day)...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hi, bro, do you have any other codes? I am interested in quantitative trading with c#.
Hey man, how did you fix it if I may ask? Im still trying and at this point I have no idea what I can try anymore.