Error 4806 Requested data not found when CopyBuffer - page 2

 

That's why a Truly Event-Driven approach is imperative for this language... We cannot to hold on on bad programming practices, like "polling" the status of something unnecessarily until we get the desired result... The MQL5 is complex enough to get this out of scope... And that's why is a mess to make very good EAs, considering also the adversity of the Brokers and behavioral sh*ts that happens all the time...

 

I have developed an Expert Advisor (EA) in MQL5 that uses the MACD indicator. However, during backtesting, I consistently encounter the error:

"Failed to copy MACD buffers. Error: 4806"

Key Details of the Implementation:

  1. MACD Initialization:

    • The iMACD function is used to create the indicator handle in the OnInit function.
    • The handle is stored globally and released in OnDeinit .
  2. Buffer Copying:

    • In the OnTick function, the MACD buffers are accessed using CopyBuffer for:
      • The main MACD line (Buffer 0).
      • The Signal line (Buffer 1).
  3. Error Handling:

    • Before calling CopyBuffer , the code checks BarsCalculated(macdHandle) to ensure the indicator has enough data.
  4. Additional Observations:

    • The log messages indicate that the MACD is not ready or that the CopyBuffer call fails even when BarsCalculated shows enough bars are available.

Code Snippet: File attach + image backtest (MACD indicator have data)

Attempts to Resolve:

  • Added checks to ensure BarsCalculated(macdHandle) >= MACD_SlowPeriod + MACD_SignalPeriod  before calling CopyBuffer .
  • Verified that the handle is valid and initialized only once in OnInit .
  • Tested with different timeframes and historical data during backtesting.

Question:

  • Could this error indicate a deeper issue with how MACD buffers are handled during backtesting in MQL5?
  • Are there additional steps or conditions I should verify to ensure the buffers are ready for copying?

If anyone has encountered a similar issue or has insights into resolving this, your help would be greatly appreciated!

Thank you in advance! 😊

Files:
 

it's because you specify the MACD timeframe to a fixed timeframe

   macdHandle = iMACD(NULL, TradeTimeframe, MACD_FastPeriod, MACD_SlowPeriod, MACD_SignalPeriod, MACD_Source);


I see you fix it to PERIOD_M1, but you should rather have it like that to avoid 4806 error:

   macdHandle = iMACD(NULL, 0, MACD_FastPeriod, MACD_SlowPeriod, MACD_SignalPeriod, MACD_Source);

The software isn't fully matured yet to properly handle 4806, and BarsCalculated won't do anything to make the error go away, so you have to specify current timeframe and leave fixed timeframe stuff for indicators.

You can try ArraysSetAsSeries to set the macd buffer as series, but I think it's safer to make a custom indicator for multi-timeframe data