iCustom not always reliable

 

From my use of iCustom() in an Expert Advisor, and from several tests, I've made the conclusion that, in general, one cannot send or close orders on the same bar on which the signal is triggered.

The reason for this issue is because:
1) the Indicator makes all its calculation
2) then it fills the buffers
3) then it draws on the chart
3) finally, the Expert Advisor, through iCustom(), retrieves the values from the buffers

It also depends on if one, in the Indicator, uses open[] or close[] values and the type of calculation involved.

Therefore, if the Indicator is limited to the last closed bar, iCustom() returns always the correct value (because it will never change) but then the Expert Advisor will receive the value in the next bar (a total of two bars late):

// Indicator
int limit = tot_bars - prev_tot_bars > 1 ? tot_bars - 1 : tot_bars - prev_tot_bars;
for(int i = limit; i >= 1 ; i--) {
...
}

// Expert Advisor
iCustom(Symbol(), Period(), indicator, parameters, index, 1);

On the other hand, if the Indicator tries to calculate also the current bar and iCustom() tries to return the previous value, what one gets is a value not reliable (because different from the closing value):

// Indicator
int limit = tot_bars - prev_tot_bars > 1 ? tot_bars - 1 : tot_bars - prev_tot_bars;
for(int i = limit; i >= 0 ; i--) {
...
}

// Expert Advisor
iCustom(Symbol(), Period(), indicator, parameters, index, 1);

If what I've said is right, in order to avoid such delay, it seems to me that the only way is to implement all the calculations directly in the Expert Advisor. Correct?

 
  1. Why did you post your MT4 question in the MT5 EA section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2. You are triggering on the bar where the signal occurs (bar one). Bar zero may set and unset as the close price (Bid) changes. If you want to trigger on a potentially false signal, change to bar zero.

  3. dcstoyanov: Therefore, if the Indicator is limited to the last closed bar,

    Wrong. Your posted code shows that it is processing bar zero.

  4. dcstoyanov: only way is to implement all the calculations directly in the Expert Advisor. Correct?
    Wrong. There would be no difference.