Have you tried using BarsCalculated to check if the indicator data has been loaded already?
I tried, it always return -1. Here is using BarsCalculated version
Hello, I want to create an indicator that retrieves multi timeframe data of a custom indicator. Due to the original program is bulky so I have recreated it in a shorter version to reproduce the error, My intention is copy MACD data from different timeframes, the size of those buffers based on M1 Period. But when I try to use CopyBuffer() function it always gives error 4806 and not once did it copied successfully (Alain Verleyen said at some point it would get that error but in my case I always).
I don't have much experience working with multi timeframes so I dont know why this error occurs. I followed the process: Initialize handle in OnInit(), copy data in OnCalculate() function.
Previously I encountered error 4401 when using the iBarShift() function. After searching around the forum, I synchronized historical data as topic: https://www.mql5.com/en/docs/series/timeseries_access#synchronized. The error 4401 is fine now but the error 4806 still is not fixed at all.
Edit 1: I Debug this indicator on Period M15, only Period M15 got the data. The rest of are error
double CopyDataHandle(int &handle, datetime startTime, int bufferIndex=0) { double data[1]; int attempt = 3; int copy = -1; while(attempt-- > 0) { copy = CopyBuffer(handle, bufferIndex, startTime, 1, data); if(copy > 0) return data[0]; Sleep(10); } if(copy <= 0) Print(GetLastError()); return 0; }
That's not good.
1. Sleep() doesn't work in indicator.
2. Never do a loop like that, even with a working Sleep(). If data are not ready, return and wait next tick. (There are other solutions more sophisticated, but start with that).
3. It's very inefficient, your are collected data 1 by 1 ?!!! Using imbricated loop, fortunately your code doesn't work ;-)
MT5/mql5 data processing is asynchronous, we need to deal with it correctly to benefit from the speed advantage of the platform. A loop is not a good way to deal with it because you NEVER know how much time it will take to get the data. (and anyway it doesn't work in an indicator).
In general your code is overly complicated without any benefit.
Your code:
That's not good.
1. Sleep() doesn't work in indicator.
2. Never do a loop like that, even with a working Sleep(). If data are not ready, return and wait next tick. (There are other solutions more sophisticated, but start with that).
3. It's very inefficient, your are collected data 1 by 1 ?!!! Using imbricated loop, fortunately your code doesn't work ;-)
MT5/mql5 data processing is asynchronous, we need to deal with it correctly to benefit from the speed advantage of the platform. A loop is not a good way to deal with it because you NEVER know how much time it will take to get the data. (and anyway it doesn't work in an indicator).
In general your code is overly complicated without any benefit.
1. Thank you for that information, I kept seeing some examples in the codebase that used it so I thought it would work.
2. So when the indicator calculates for the first time and encounters an error, it will have to recalculate from the beginning is that right?
3. Do you have a better way to get data from another timeframe? I just came up with this right now

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello, I want to create an indicator that retrieves multi timeframe data of a custom indicator. Due to the original program is bulky so I have recreated it in a shorter version to reproduce the error, My intention is copy MACD data from different timeframes, the size of those buffers based on M1 Period. But when I try to use CopyBuffer() function it always gives error 4806 and not once did it copied successfully (Alain Verleyen said at some point it would get that error but in my case I always).
I don't have much experience working with multi timeframes so I dont know why this error occurs. I followed the process: Initialize handle in OnInit(), copy data in OnCalculate() function.
Previously I encountered error 4401 when using the iBarShift() function. After searching around the forum, I synchronized historical data as topic: https://www.mql5.com/en/docs/series/timeseries_access#synchronized. The error 4401 is fine now but the error 4806 still is not fixed at all.
Edit 1: I Debug this indicator on Period M15, only Period M15 got the data. The rest of are error