Download MetaTrader 5

Getting very small number from indicator buffer

To add comments, please log in or register
Add our Twitter channel to friends and follow the news!
pvo2
64
pvo2 2013.09.06 11:03 

Hi,

I'm trying to build a mechanism within my EA whereby the value of the iATR buffer is assigned to a variable of the same type for use further within the code (i.e. rolling ATR). However, the results I have been getting is a very small number and to further test this I had carved out the relevant bits of code (below) into a separate EA (away from all the other code) whereby I was getting the same results. I had inserted a couple of print functions to see the values and irrespective of whether it was directly from the array or through a variable of the same type I was yielding the same outcome. Attached below is a screen print of the strategy tester journal with the values from the array.

This way of extracting indicator values from a buffer is common from what I have seen including the MT5 expert programming book and in the mql5 reference book. I'm struggling to see where the problem lies in what should be quite simple. Is anyone able to provide an answer?

Thanks in advance for your help.

Peter 

 

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
      int _1barATRHandle;
      double _1barATRArray[];
      _1barATRHandle = iATR(Symbol(),0,1);
      ArraySetAsSeries(_1barATRArray,true);
      CopyBuffer(_1barATRHandle,0,0,1,_1barATRArray);
      Print("_1barATRArray[0]: "+(string)_1barATRArray[0]);
      double _1barATR = _1barATRArray[0];                                                                                      
      Print("_1barATR:" +(string)_1barATR);

  }
//+------------------------------------------------------------------+

 

Files:
Alain Verleyen
Moderator
30736
Alain Verleyen 2013.09.06 13:39  
pvo2:

Hi,

I'm trying to build a mechanism within my EA whereby the value of the iATR buffer is assigned to a variable of the same type for use further within the code (i.e. rolling ATR). However, the results I have been getting is a very small number and to further test this I had carved out the relevant bits of code (below) into a separate EA (away from all the other code) whereby I was getting the same results. I had inserted a couple of print functions to see the values and irrespective of whether it was directly from the array or through a variable of the same type I was yielding the same outcome. Attached below is a screen print of the strategy tester journal with the values from the array.

This way of extracting indicator values from a buffer is common from what I have seen including the MT5 expert programming book and in the mql5 reference book. I'm struggling to see where the problem lies in what should be quite simple. Is anyone able to provide an answer?

Thanks in advance for your help.

Peter 

Your code is correct. Which symbol and timeframe are you using ?
pvo2
64
pvo2 2013.09.06 13:41  
angevoyageur:
Your code is correct. Which symbol and timeframe are you using ?

Hi Alain,

I tested it against the daily EURUSD pair.  The MT5 software I'm using was downloaded from mql5.com.

Have you ever experienced anything like this before?

Thanks

Peter 

Alain Verleyen
Moderator
30736
Alain Verleyen 2013.09.06 16:33  
pvo2:

Hi Alain,

I tested it against the daily EURUSD pair.  The MT5 software I'm using was downloaded from mql5.com.

Have you ever experienced anything like this before?

Thanks

Peter 

It seems your printed values are data for each new daily bar. As you get data for current (open) candle which has only 1 (or very few) tick, the result is values around 0.00001. Seems logical to me. Try to get data for closed candle :

 CopyBuffer(_1barATRHandle,0,1,1,_1barATRArray);
pvo2
64
pvo2 2013.09.08 10:26  
angevoyageur:

It seems your printed values are data for each new daily bar. As you get data for current (open) candle which has only 1 (or very few) tick, the result is values around 0.00001. Seems logical to me. Try to get data for closed candle :

Hi Alain,

Thanks. I made the change you suggested which solves the issue. However, when I embed the code back into my EA - more specifically within the method from the class that is called from I now get zero value. I've spent the last 2 days pulling apart the code trying to understand why but have not yet come to a conclusion yet. I know the code doesn't lie so I'll let you know once I've found the answer. Debugging is tough! 

Thanks again.

Peter 

Alain Verleyen
Moderator
30736
Alain Verleyen 2013.09.08 19:23  
pvo2:

Hi Alain,

Thanks. I made the change you suggested which solves the issue. However, when I embed the code back into my EA - more specifically within the method from the class that is called from I now get zero value. I've spent the last 2 days pulling apart the code trying to understand why but have not yet come to a conclusion yet. I know the code doesn't lie so I'll let you know once I've found the answer. Debugging is tough! 

Thanks again.

Peter 

Ok, let me know if you need help.
pvo2
64
pvo2 2013.09.10 13:41  
angevoyageur:
Ok, let me know if you need help.

After hours of debugging, the problem seemed to have worked itself out. More specifically, I'm not getting very small numbers anymore without fundamentally changing my code. I had been tweaking with the position start index on the copybuffer together with the ArraySetAsSeries and somehow it worked out itself.

I do have another question in relation to arrays - the code above works fine under an OnTick event handler, as new ticks are populated a calculated value will populate the buffer and into the array. However, when inserting the same code into the initialize event handler no values are generate i.e. 0. Given that the strategy testing does not start at the very first bag of the chart (i.e. 1-Jan-1971) I would've thought that the iATR handle would still be able to calculate a value. 

What do you think?

Peter 

Alain Verleyen
Moderator
30736
Alain Verleyen 2013.09.10 16:12  
pvo2:

After hours of debugging, the problem seemed to have worked itself out. More specifically, I'm not getting very small numbers anymore without fundamentally changing my code. I had been tweaking with the position start index on the copybuffer together with the ArraySetAsSeries and somehow it worked out itself.

I do have another question in relation to arrays - the code above works fine under an OnTick event handler, as new ticks are populated a calculated value will populate the buffer and into the array. However, when inserting the same code into the initialize event handler no values are generate i.e. 0. Given that the strategy testing does not start at the very first bag of the chart (i.e. 1-Jan-1971) I would've thought that the iATR handle would still be able to calculate a value. 

What do you think?

Peter 

I think it's not a good idea to place this code in OnInit(). Only the line which initialize ATR handle should be in OnInit().
To add comments, please log in or register