CopyBuffer does not work in OnInit() function ? - page 2

 
Alain Verleyen:
Why ? What are you trying to solve ?

When you run OnInit(), you initialize handles.

When the ticks start coming, You run CopyBuffer to prepare data analysis.

I want to test CopyBuffer during OnInit() to avoid discovering that we have a buffer problem (lack of bars for example) pro actively.

 
Marcelo Coutinho:

When you run OnInit(), you initialize handles.

When the ticks start coming, You run CopyBuffer to prepare data analysis.

I want to test CopyBuffer during OnInit() to avoid discovering that we have a buffer problem (lack of bars for example) pro actively.

The data are not supposed to be ready during the initialization process, they can or can not.

You always have to manage possible errors of CopyBuffer(). I don't see why it's a problem, you just have to act accordingly to the possible errors.

 
Alain Verleyen:

The data are not supposed to be ready during the initialization process, they can or can not.

You always have to manage possible errors of CopyBuffer(). I don't see why it's a problem, you just have to act accordingly to the possible errors.

Let me give you an example:

Lets suppose you add an EA to the graph 2 hours before the market opening.

If you could test the CopyBuffer with the last "x" bars available you would know in advance there is a problem.

The way it works today, I have to wait the market opening and the 1st tick to realise there is a problem.

 
Marcelo Coutinho:

Let me give you an example:

Lets suppose you add an EA to the graph 2 hours before the market opening.

If you could test the CopyBuffer with the last "x" bars available you would know in advance there is a problem.

The way it works today, I have to wait the market opening and the 1st tick to realise there is a problem.

Just use a timer, you don't have to wait for a tick. But I still don't see what you will win by doing that.
 
Alain Verleyen:

The data are not supposed to be ready during the initialization process, they can or can not.

You always have to manage possible errors of CopyBuffer(). I don't see why it's a problem, you just have to act accordingly to the possible errors.

facing the same issue for heikenashi:

https://www.mql5.com/en/forum/248411


I think it's an issue with MT5 program itself.

I do have data on chart. Checked also IndicartorParameters()

Error 4806 0n Heikenashi indicator (MQL5) : "indicator not ready" bug
Error 4806 0n Heikenashi indicator (MQL5) : "indicator not ready" bug
  • 2018.05.30
  • www.mql5.com
Don't know what am i doing wrong : Handle & everything else is all okay, still its saying : "indicator data absent"/"indicator not ready" etc...
 
Tusher Ahmed:

facing the same issue for heikenashi:

https://www.mql5.com/en/forum/248411


I think it's an issue with MT5 program itself.

I do have data on chart. Checked also IndicartorParameters()

No it's an issue with your code.
 
Alain Verleyen:
Just use a timer, you don't have to wait for a tick. But I still don't see what you will win by doing that.

Do you mean like this, Mr. Alain?

int OnInit()
{
   EventSetMillisecondTimer(1);
   return(INIT_SUCCEEDED);
}
void OnTimer()
{
   if(CopyBuffer(i_handle,0,0,6,i_buffer)!=6)
   {
      Print("Failed to copy data from the indicator buffer or price chart buffer: ",GetLastError());
      ResetLastError();
      return;
   }
}
 
Marcelo Coutinho:

Let me give you an example:

Lets suppose you add an EA to the graph 2 hours before the market opening.

If you could test the CopyBuffer with the last "x" bars available you would know in advance there is a problem.

The way it works today, I have to wait the market opening and the 1st tick to realise there is a problem.

i also had quite similiar problem in my strategy. Need to to learn the market behavior and map it to my strategy, i ended up performing manual calculcation in my code using the existing formula depending on the indicator i want to use.

The formula is there, just look in the MT5 indicators source code, unless you have your own indicator with custom formula (but you wouldn't have asked the question).

As long as the copyrates can return whatever count of bars you target, indicator's formula should return the expected value

 
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
Reason: