Indicator alert looping problem

 

I have this sample indicator that detects candlestick pattern.

The problem is, the alert is looping after the pattern was detected. 

Is there a way to limit or set a number of how many time it can alert?

Hope anybody can help, here is the source code and thank you in advanced.

void OnTick()
  {
   datetime time=iTime(_Symbol,PERIOD_CURRENT,1);
   double open=iOpen(_Symbol,PERIOD_CURRENT,1);
   double high=iHigh(_Symbol,PERIOD_CURRENT,1);
   double low=iLow(_Symbol,PERIOD_CURRENT,1);
   double close=iClose(_Symbol,PERIOD_CURRENT,1);
   double open2=iOpen(_Symbol,PERIOD_CURRENT,2);
   double high2=iHigh(_Symbol,PERIOD_CURRENT,2);
   double low2=iLow(_Symbol,PERIOD_CURRENT,2);
   double close2=iClose(_Symbol,PERIOD_CURRENT,2);
   double open3=iOpen(_Symbol,PERIOD_CURRENT,3);
   double high3=iHigh(_Symbol,PERIOD_CURRENT,3);
   double low3=iLow(_Symbol,PERIOD_CURRENT,3);
   double close3=iClose(_Symbol,PERIOD_CURRENT,3);
   
   if(open3<close3)
     {
      if(open2>close2)
        {
         if(open<close)
           {
            createObj(time,low,217, clrGreen,"UP ");
            Alert("UP ");
           }
         }
      }
   if(open3>close3)
     {
      if(open2<close2)
        {
         if(open>close)
           {
            createObj(time,high,218, clrRed,"DOWN ");
            Alert("DOWN ");     
           }
         }
      }
   } 

void createObj(datetime time, double price, int arrawCode, color clr, string txt)
  {
   string objName=" ";
   StringConcatenate(objName, "Signal@",time, "at",DoubleToString(price,_Digits),"(",arrawCode,")");
   if(ObjectCreate(0,objName,OBJ_ARROW,0,time,price))
     {
      ObjectSetInteger(0,objName,OBJPROP_ARROWCODE,arrawCode);
      ObjectSetInteger(0,objName,OBJPROP_COLOR,clr);
      if(clr==clrGreen)
         ObjectSetInteger(0,objName,OBJPROP_ANCHOR,ANCHOR_TOP);
      if(clr==clrRed)
         ObjectSetInteger(0,objName,OBJPROP_ANCHOR,ANCHOR_BOTTOM);
     }
   string candleName=objName+txt;
   if(ObjectCreate(0,candleName,OBJ_TEXT,0,time,price))
     {
      ObjectSetString(0,candleName,OBJPROP_TEXT," "+txt);
      ObjectSetInteger(0,candleName,OBJPROP_COLOR,clr);
     }
  }
 
bidyey33:

I have this sample indicator that detects candlestick pattern.

The problem is, the alert is looping after the pattern was detected. 

Is there a way to limit or set a number of how many time it can alert?

Hope anybody can help, here is the source code and thank you in advanced.

Use a Boolean flag and set it to false
Only allow the alert if the flag is false
Once you trigger alert set it to true

Reset it to false when your conditions are met to allow a new alert, say on a new bar or after an amount of time whatever you choose

 
Paul Anscombe #:
Use a Boolean flag and set it to false
Only allow the alert if the flag is false
Once you trigger alert set it to true

Reset it to false when your conditions are met to allow a new alert, say on a new bar or after an amount of time whatever you choose

Can you show a sample code?

 
bidyey33 #:

Can you show a sample code?

 if(open3<close3 && open2>close2 && open<close && Allow_Alert == true)
 {
	createObj(time,low,217, clrGreen,"UP ");
        Alert("UP ");
	Allow_Alert = false;
 }
 else if(open3>close3 && open2<close2 && open>close && Allow_Alert == true)
 {
 	createObj(time,high,218, clrRed,"DOWN ");
        Alert("DOWN ");     
	Allow_Alert = false
 }
 

You need to define Allow_Alert in global scope and initialise it as false.

You should not check these conditions every tick as you are clearly testing bar closes right?  so use a new bar function (do a search plenty around) and upon a new bar check your alert conditions.

If you do that and want the alert to reset upon a new bar then you can just set Allow_Alert to true upon a new bar, you could even not bother with the Allow_Alert if you only check on new bar and happy to alert every bar.  However, if you want a different reset period or are not checking on a new bar then you need to code that and set Allow_Alert to true upon your conditions.

 
resetting the boolean flag back to true upon a new bar might be difficult for an mql newbie to figure out, I think that could possibly work:

if(TimeCurrent() - time == PeriodSeconds()*2){

Allow_Alert = true;
}

or 

If(TimeCurrent() % 60 == 0){

Allow_Alert = true;
}

One thing Paul - he should initialize the global variable to true instead of false because the first alert will be looking for the flag to be true. 
 
Conor Mcnamara #: resetting the boolean flag back to true upon a new bar might be difficult for an mql newbie to figure out, I think that could possibly work:
  1. Please edit your post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. What if there is no tick that specific second?
 
William Roeder #:
  1. Please edit your post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. What if there is no tick that specific second?

I can't seem to edit it now (wrote on mobile device). Good point, he should rather use OnTimer for his use case, as it will be more reliable for time conditions.

I think this might be another unfortunate AI code. OP - We use OnCalculate in indicators, and OnTick is for Expert Advisors.