βœ–

Home Forums Trading view MACD values not matching broker/chart in my algo β€” what am I doing wrong?

MACD values not matching broker/chart in my algo β€” what am I doing wrong?

  • Author
    Posts
    • πŸ“ Question Details

      I’m calculating MACD in my algo using my 1-minute candles.

      I implemented EMA (12, 26) and signal line (9), but when I compare with chart values, they don’t match exactly. Candle data looks correct, still MACD is slightly off.

      Is there any specific way to calculate MACD properly so it matches chart values?

    • Yes, so MACD is depending on many factors to be correct.

      So, to make it super accurate you need the right live candle data along with the sufficient historical data of that script.

      And may be sometimes the calculation mismatch is also there. Things to remember use the closed candles only, preload enough data (e.g. at least 200 candles) and maintain EMA continuity.

      Below is the code you can refer:

      def calculate_ema(prices, period):
          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
      
      def calculate_macd(prices):
          ema12 = calculate_ema(prices, 12)
          ema26 = calculate_ema(prices, 26)
      
          macd_line = [e12 - e26 for e12, e26 in zip(ema12, ema26)]
          signal_line = calculate_ema(macd_line, 9)
          histogram = [m - s for m, s in zip(macd_line, signal_line)]
      
          return macd_line, signal_line, histogram
      
      # Sample
      prices = [100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119,120]
      
      macd, signal, hist = calculate_macd(prices)
      
      print("MACD:", macd[-5:])
      print("Signal:", signal[-5:])
      print("Histogram:", hist[-5:])
Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⏳

Pending Approval

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top