iCustom returns wrong value in some case!?

 

Hey programmers,

I have a super weird problem and do not find a solution. Maybe you can help. I am working with a custom indicator and calculate it´s value in the following function: 

string checkTrend(){
   trend0 = iCustom(_Symbol,Timeframe,NameTrend,PeriodsTrend,CountBarsTrend,0,1);
   trend1 = iCustom(_Symbol,Timeframe,NameTrend,PeriodsTrend,CountBarsTrend,1,1);

   if(trend0 != 0){
      PrintFormat("Trend Values (0,1): %f, %f! Buy signal...",trend0,trend1);
      return "buy";
   }else if(trend1 != 0){
      PrintFormat("Trend Valus (0,1): %f, %f! Sell signal...",trend0,trend1);
      return "sell";
   }else{
      PrintFormat("Trend Valus (0,1): %f, %f! No signal...",trend0,trend1);
      return "";
   }
}

In my OnTick() I use the function above in an if statement to check weather the trend is long or short. The whole code block is only executed once per bar whenever a new bar appears.

.
.
.
if(checkHA() == "buy" 
   && !isTrade
   && (!IsTrend || checkTrend() == "buy")){

        //execute long trade...
}

When I run a test I can visually see (in the chart) that the signal is "buy" on 2018.01.02 06:52:00. Unfortunately the function returns "sell" as you can see here:

wrong behavior

If i add another function call above the if statement, the code looks like this:

.
.
.
checkTrend();

if(checkHA() == "buy" 
   && !isTrade
   && (!IsTrend || checkTrend() == "buy")){
        
        //execute long trade
}

Then i run the exact same backtest and the result is as follows. Now both function calls return the right string value which is the buy signal...

correct behavior

This gives me a big headache and I hope that someone can help me here. 

Greetings,

René 

 
Verify that you are reading the buffers (index) correctly
 
Hector Pacheco:
Verify that you are reading the buffers (index) correctly

Hey, thanks for the hint. I do not really understand what you mean by that. I definitely named the right buffers and i do get the right result. But only if I put the function call before the if statement... That is the only thing I do not get

 

Okay I think I got it now.. For anybody who is interested or facing the same problem. This post kinda helped me to understand the iCustom: https://stackoverflow.com/questions/26491213/mql4-icustom-returns-always-the-same-wrong-value-0x7fffffff

Looks like the problem in my case was that I did not call the iCustom often enough. So when I really needed the current value it was not available in the buffer and I received an old one while the indicator started to update. In my case the difference between the first code (checkTrend() above the if statement) and the second one was that the code did not reach the function call in the if statement every time. Makes sense to me and I hope someone find this useful. 

MQL4 iCustom returns always the same (wrong) value (0x7FFFFFFF)
MQL4 iCustom returns always the same (wrong) value (0x7FFFFFFF)
  • stackoverflow.com
I wrote a custom indicator as follows: The indicator works fine in the application and the chart is plotted correctly. When I try retrieving the last value in the ExpertAdvisor I always receive the same value: speed is: 2147483647...