Issue while using iCustom Function in my expert advisor

 

Hello everyone,

I'm trying it to add the custom "ary-aind-indicator" to filter the positions that my expert will open.

This indicator is very simple : it display green bars for bullish momentum, and red bars for bearish momentum. And obviously, the returned value is the bar value which is positive for a green bar, and negative for a red one. And also, this bar value is the value that is displayed in the Data window of MT4, so i'm pretty sure that this indicator returns the bar value.

So I used the iCustom function to get the returned bar value (the indicator receive as input a period which is "27" in this case, and 3 buffers indexed as 0,1 and 2):

double  Val=iCustom(Symbol(),0,"ary-aind-indicator",27,0,1);

Here the issue, no matter what buffer I use, the iCustom function doesn't seem to return the bar value. Even when I isolate this indicator and use it alone to know what is the problem as the following (T1 and T2 are used to open one single trade they are both initially set at zero and receive incrementation++ when the order is opened) :

   if (T1==0 &&( Val > 0 ))
   {
      PP1=Bid;
      ticket11=OrderSend(Symbol(),OP_BUY,lots1,Ask,0,PP1-(100*10*Point),PP1+(100*10*Point),"Buy",magic,0,Red);
      Alert("Niveau PP atteint");
      SendMail("Buy N1", Symbol()+TimeToStr(TimeLocal(),TIME_SECONDS));
      SendNotification("Buy N1"+ ThePair);
   }
   
   if (T2==0 && ( Val < 0))
   {
      PP2=Bid;
      ticket12=OrderSend(Symbol(),OP_SELL,lots1,Bid,0,(PP2+S)+(100*10*Point),(PP2+S)-(100*10*Point),"SELL",magic+1,0,Red);
      Alert("Niveau PP atteint");
      SendMail("Sell N1", Symbol()+TimeToStr(TimeLocal(),TIME_SECONDS));
      SendNotification("Sell N1"+ ThePair);
   }

The issue still persist, cause my approach is very simple : if red bar (Val < 0) then open short position

       if green bar (Val > 0) then open long position

But when I back test, sometimes even when there is a red bar (Val < 0) the expert doesn't open a short position, and this no matter what buffer I use, so i'm really confused about it.

Here the code of the indicator if someone need to check it, and below you will find attached the indicator file, and a screen shot of the indicator to see what it looks like, and also how in the data window it's the bar value that is returned :

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Blue
#property indicator_color2 Lime
#property indicator_color3 Red

extern int period = 27;
double Mas1[];
double Mas2[];
double Mas3[];

int init() {
   SetIndexStyle(0, DRAW_NONE);
   SetIndexStyle(1, DRAW_HISTOGRAM, STYLE_SOLID, 1.5, Lime);
   SetIndexStyle(2, DRAW_HISTOGRAM, STYLE_SOLID, 1.5, Red);
   IndicatorDigits(Digits + 1);
   SetIndexBuffer(0, Mas1);
   SetIndexBuffer(1, Mas2);
   SetIndexBuffer(2, Mas3);
   IndicatorShortName("ARYAIND");
   SetIndexLabel(1, NULL);
   SetIndexLabel(2, NULL);
   return (0);
}

int start() {
   double arya1;
   double arya2;
   double arya10;
   int aryax = IndicatorCounted();
   double aryay = 0;
   double aryaz = 0;
   double aryam = 0;
   double aryalow = 0;
   double aryahigh = 0;
   if (aryax > 0) aryax--;
   int k = Bars - aryax;
   for (int aryak = 0; aryak < k; aryak++) {
      aryahigh = High[iHighest(NULL, 0, MODE_HIGH, period, aryak)];
      aryalow = Low[iLowest(NULL, 0, MODE_LOW, period, aryak)];
      arya10 = (High[aryak] + Low[aryak]) / 2.0;
      aryay = 0.66 * ((arya10 - aryalow) / (aryahigh - aryalow) - 0.5) + 0.67 * aryaz;
      aryay = MathMin(MathMax(aryay, -0.999), 0.999);
      Mas1[aryak] = MathLog((aryay + 1.0) / (1 - aryay)) / 2.0 + aryam / 2.0;
      aryaz = aryay;
      aryam = Mas1[aryak];
   }
   bool aryacheck = TRUE;
   for (aryak = k - 2; aryak >= 0; aryak--) {
      arya2 = Mas1[aryak];
      arya1 = Mas1[aryak + 1];
      if ((arya2 < 0.0 && arya1 > 0.0) || arya2 < 0.0) aryacheck = FALSE;
      if ((arya2 > 0.0 && arya1 < 0.0) || arya2 > 0.0) aryacheck = TRUE;
      if (!aryacheck) {
         Mas3[aryak] = arya2;
         Mas2[aryak] = 0.0;
      } else {
         Mas2[aryak] = arya2;
         Mas3[aryak] = 0.0;
      }
   }
   return (0);
}

Sorry for being to long. And thanks for any help.

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

  2. You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

Reason: