EMA/SMA giving wrong values in live trading

Asked by Sejpalsinh Jadeja · SOLVED
1 reply 834 views Jump to solution Reply
Sejpalsinh Jadeja Original Post
AlgoDelta Team

EMA/SMA values not matching chart in my algo (using 1-min candles)

I’m calculating EMA and SMA in my algo using 1-minute candles built from ticks.

But when I compare it with broker/chart values, the numbers don’t match exactly.

I’ve checked my candle data (OHLC looks correct), still indicator values are slightly off.

Is there something I’m missing in EMA/SMA calculation?

Posted Apr 24
Reply

    1 Reply

    Support AlgoDelta
    Support AlgoDelta AlgoDelta Support ·

    Yes, this may be the issue of data consistency.

    See may be the problem because of these factors:

    Calculation on incomplete candles
    Missing initial historical data.
    Candle mismatch because of timing

    See we need the both the live and historical data to get the accurate values in EMA and SMA.

    Below is the code that you can refer:

    def calculate_ema(prices, period=5):
        ema = []
        k = 2 / (period + 1)
    
        for i, price in enumerate(prices):
            if i == 0:
                ema.append(price)
            else:
                ema.append(price * k + ema[-1] * (1 - k))
    
        return ema
    
    # This prices should be have the both live and historical data
    prices = [100, 102, 101, 105, 107, 110]
    
    ema_values = calculate_ema(prices, period=3)
    print(ema_values)
    Verified Solution
    • Perfect, this is exactly what I was looking for. I followed the instructions and code, and I get the accurate values. Thanks for the help. – Sejpalsinh Jadeja Team Jun 16
    Markdown supported · ```json for code blocks · paste or drop screenshots

    Be specific and kind. Never share API keys, passwords or personal account details.

    Welcome to the community

    Log in or create your account in under a minute.

    1. 1Mobile
    2. 2E-mail
    3. 3Password
    +91

    Have a password?

    By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.