iCustom is making me sad

 

Hi guys,

I'm having issues with the iCustom command. Basically it's working sometimes and not others, which I find a little bizarre. Here is my code.

bool Scan_Super_Trend()
   {
   ArrayResize(Value1,7,7);
   ArrayResize(Value2,7,7);
   ArrayResize(SuperTrend,7,7);
   ArrayResize(STDirection,7,7);

   i = 1;

   while (i <= 6)
      {
      if (i != 0)
         {
         Value1[i] = NormalizeDouble(iCustom(NULL,0,"Super Trend","",44,3,3000,"",false,0,i),Digits);
         Value2[i] = NormalizeDouble(iCustom(NULL,0,"Super Trend","",44,3,3000,"",false,1,i),Digits);
                    
         if ((Value1[i] < 200) && (Value2[i] < 200))
            {
            SuperTrend[i] = Value1[i];
            STDirection[i] = "Switching";
            }
         else if (Value1[i] < 200)
            {
            SuperTrend[i] = Value1[i];
            STDirection[i] = "Bullish";
            }
         else if (Value2[i] < 200)
            {
            SuperTrend[i] = Value2[i];
            STDirection[i] = "Bearish";
            }
         else
            {
            SuperTrend[i] = 666;
            STDirection[i] = "Failed";
            Print("Value1[",i,"] = ",Value1[i]);
            Print("Value2[",i,"] = ",Value2[i]);
            }
         }
      i++;
      }

   Print("STDirection = ",STDirection[2]," (",STDirection[1],")");
   Print("SuperTrend = ",SuperTrend[2]);
   Print("Value1 = ",Value1[2]);
   Print("Value2 = ",Value2[2]);
   Print("******************************");
   
   /*if (STDirection[2] == "Switching")
      {
      Check_Last_Trend();
      }*/
               
   return(true);
   }
This function is currently called every tick, and seems to work great for a while after initial compilation, and calls up the correct values. Then all of a sudden it just stops working properly and fails every time it's run. Can anyone give me any reason as to why this might be happening. And crucially, why it only happens sometime?
 
What value does it print when it fails ?
 
Marco vd Heijden:
What value does it print when it fails ?
it prints 2147483647 (AKA EMPTY_VALUE). I have since added a bit to reset all of the array values to 0 before it executes. So far it's working, but I've been dissapointed before.
 
Yeah, that made no difference at all. It always starts working again when I recompile it. Then it will stop again. It's infuriating.
 

If its not important , or the indicator does not repaint , do you need to scan the latest 6 bars constantly?

From looking at the code i will assume you have a "new bar" filtering system in "OnTick"

 
DrBeardface:

Hi guys,

I'm having issues with the iCustom command. Basically it's working sometimes and not others, which I find a little bizarre. Here is my code.

This function is currently called every tick, and seems to work great for a while after initial compilation, and calls up the correct values. Then all of a sudden it just stops working properly and fails every time it's run. Can anyone give me any reason as to why this might be happening. And crucially, why it only happens sometime?

I'm not expert in MT4, but, just my observations...

  1. Your first IF statemente is useless, since you are starting at i=1.
  2. You declared a 7-element array and are not using the first element (0).
  3. You are calling "Super Trend" with the very same values of "i" - which is 1 through 6. Always. Do you know if this indicator is using internally ArraySetAsSeries()? Otherwise you are getting the very same values from the indicator buffers...

;)

 

The problem is with the iCustom call, and I'm pretty sure I now know WHAT the problem is, I just don't know how to fix it. When I run the indicator on a chart is draws a line from it's buffers. But occasionally it will fail and stop keeping up with the chart, meaning that I have to reinitialise the indicator to see the line. I'm wondering if there's some problem with the actual indicator (I don't have the source code for it) that makes it stop working after a certain amount of time and this is why the code I've written works for a few hours then suddenly just stops and only reads as an empty buffer value, and also that it starts working again when I recompile it.

The assumptions I am making are that my use of the iCustom command is essentially correct, and that when iCustom is used in a script the indicator it is reading from is initialised ONLY when the script is initialised, and not every time the iCustom function is called within the script.

If the second of those assumptions is correct, is there a way to make it reinitialise the indicator every time iCustom is called?

 
DrBeardface:

The problem is with the iCustom call, and I'm pretty sure I now know WHAT the problem is, I just don't know how to fix it. When I run the indicator on a chart is draws a line from it's buffers. But occasionally it will fail and stop keeping up with the chart, meaning that I have to reinitialise the indicator to see the line. I'm wondering if there's some problem with the actual indicator (I don't have the source code for it) that makes it stop working after a certain amount of time and this is why the code I've written works for a few hours then suddenly just stops and only reads as an empty buffer value, and also that it starts working again when I recompile it.

The assumptions I am making are that my use of the iCustom command is essentially correct, and that when iCustom is used in a script the indicator it is reading from is initialised ONLY when the script is initialised, and not every time the iCustom function is called within the script.

If the second of those assumptions is correct, is there a way to make it reinitialise the indicator every time iCustom is called?

Don't mix your terminology. Are you using a script? How can you recompile if you don't have the source code?

 

It may be that your Super Trend indicator has problems.

https://www.mql5.com/en/code/22750

This may suit you and will not need constantly re-initializing.

Check if the output is the same.

 
Keith Watford:

Don't mix your terminology. Are you using a script? How can you recompile if you don't have the source code?

I reinitialise the indicator, and recompile my EA. Correct terminology used (I believe) in each case, sorry if it was confusing.

Reason: