Error 4806 CopyBuffer() With Multi Timeframes

 

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


Documentation on MQL5: Timeseries and Indicators Access / Organizing Data Access
Documentation on MQL5: Timeseries and Indicators Access / Organizing Data Access
  • www.mql5.com
Organizing Data Access - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
TestMACD.mqh  10 kb
test.mq5  16 kb
 
Have you tried using BarsCalculated to check if the indicator data has been loaded already?
 
Yashar Seyyedin #:
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

Files:
test.mq5  17 kb
TestMACD.mqh  11 kb
 
Le Minh Duc #:

I tried, it always return -1. Here is using BarsCalculated version

So probably chart isn't loading at all. All you can do is just return.
 
Yashar Seyyedin #:
So probably chart isn't loading at all. All you can do is just return.

Is there any way to force the chart to load? Or should I reinstall terminal?

 
Le Minh Duc #:

Is there any way to force the chart to load? Or should I reinstall terminal?

l guess if you try to manually load the symbol/timeframe you get around with it.
Other than that I have no idea.
 
Yashar Seyyedin #:
l guess if you try to manually load the symbol/timeframe you get around with it.
Other than that I have no idea.

Thank you for your help. I will find other ways to deal with it

 
Le Minh Duc:

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

Your code:
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.

 
Alain Verleyen #:
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