Download MetaTrader 5

How is iCustom executed?

To add comments, please log in or register
Keith Watford
Moderator
10065
Keith Watford  

I have never considered this before.

I just wondered if anybody actually knows the order of an iCustom() call in an EA.

Is the iCustom executed before the the code in OnTick that uses the value?

I ask this because I am currently working on a project where the iCustom call is only executed on a new bar, getting the value for bar[1].

The indicator also only executes on new bars, but it is a lot more complicated than the EA, therefore would take longer to execute.

So is it possible that the EA would try to access the value for bar[1] before the indicator has finished its calculations?

I would hope that this is taken care of in the way EAs are executed and I guess that I will find out in my testing, but if somebody knows for sure, I would like to know.


Thanks

Blahtech Limited
4300
James Cater  
Keith Watford:

I have never considered this before.

I just wondered if anybody actually knows the order of an iCustom() call in an EA.

Is the iCustom executed before the the code in OnTick that uses the value?

I ask this because I am currently working on a project where the iCustom call is only executed on a new bar, getting the value for bar[1].

The indicator also only executes on new bars, but it is a lot more complicated than the EA, therefore would take longer to execute.

So is it possible that the EA would try to access the value for bar[1] before the indicator has finished its calculations?

I would hope that this is taken care of in the way EAs are executed and I guess that I will find out in my testing, but if somebody knows for sure, I would like to know.


Thanks

The call from OnTick to OnCalculate via iCustom is as you would expect

The OnTick method in your EA will execute up to the point where it calls iCustom. The iCustom then calls the OnCalculate method in your indicator then return back to execute the rest of the OnTick method

The OnInit method on the indicator gets called once on the first call to iCustom. However beware of putting any calls to iCustom in the OnInit method of the EA because the first call to iCuston will force a call to OnCalculate even though it has not being called from OnTick. As a result I would only recommend making iCustom calls from OnTick not from OnInit.

This anomaly has been changed in MQL5 where the creation of the iCustom link and indicator OnInit call have been separated from the subsequent OnTick to OnCalculate calls.

I deduced all these behaviours from using simple print logging statements.

Keith Watford
Moderator
10065
Keith Watford  

Thank you James.

A nice clear answer and it makes sense.

To add comments, please log in or register