question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I’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:closed
  • Created 4 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
iEricZHENGcommented, Feb 26, 2020

Hi, bro, do you have any other codes? I am interested in quantitative trading with c#.

0reactions
AndreasDamencommented, Oct 28, 2021

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found