alarm going off repeatedly

 

I have coded this so that once the alarm fires, it sets the Bars count and shouldnèt trigger again for 5mins. However, when the lines cross, the a;larm keeps triggering every few ticks as the values cross and uncross. How can I stop this"


   if (iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,0) < TSI_Buffer[0]
   && iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,1) > TSI_Buffer[1]
   ) 
   {
      Comment ("Crossed up");
      if (Bars>BarsTotal || BarsTotal==0) {
         Alert ("Crossing up");
         BarsTotal=Bars;
      }
      Alert ("Crossing up");
   }
   else if (iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,0) > TSI_Buffer[0]
   && iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,1) < TSI_Buffer[1]
   ) 
   {
      Comment ("Crossed down");
      if (Bars>BarsTotal || BarsTotal==0) {
         Alert ("Crossing down");
         BarsTotal=Bars;
      }
   }
 
again and again :-)
 
SanMiguel:

I have coded this so that once the alarm fires, it sets the Bars count and shouldnèt trigger again for 5mins. However, when the lines cross, the a;larm keeps triggering every few ticks as the values cross and uncross. How can I stop this"


Make your test only on new bar by comparing closed candles.

If (isNewBar())
   if (iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,1) < TSI_Buffer[1]
   && iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,2) > TSI_Buffer[2]
   ) 
   {
      Comment ("Crossed up");
      if (Bars>BarsTotal || BarsTotal==0) {
         Alert ("Crossing up");
         BarsTotal=Bars;
      }
      Alert ("Crossing up");
   }
   else if (iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,1) > TSI_Buffer[1]
   && iMAOnArray(TSI_Buffer,0,7,0,MODE_EMA,2) < TSI_Buffer[2]
   ) 
   {
      Comment ("Crossed down");
      if (Bars>BarsTotal || BarsTotal==0) {
         Alert ("Crossing down");
         BarsTotal=Bars;
      }
   }
}

Search the site if you need an implementation of isNewBar.

 
angevoyageur:

Make your test only on new bar by comparing closed candles.

Search the site if you need an implementation of isNewBar.


if I do that then it will only sound at the closing of a candle. If the cross happens halfway through a 5 min candle, the alert won't happen until 2.5 mins later. But I need the alert to happen as soon as it crosses
 
SanMiguel:
if I do that then it will only sound at the closing of a candle. If the cross happens halfway through a 5 min candle, the alert won't happen until 2.5 mins later. But I need the alert to happen as soon as it crosses
So if it crosses one way then the other and does this 20 times on one candle you want 20 Alerts ?
 
SanMiguel:
if I do that then it will only sound at the closing of a candle. If the cross happens halfway through a 5 min candle, the alert won't happen until 2.5 mins later. But I need the alert to happen as soon as it crosses

You have to choose what you want. If you need a "good" signal then wait for closed candle. If you can deal with a "false" alert then trigger it on open candle, but you signal can have disappeared when the candle will be closed.

To manage the latter, you can for example set a bool that alert has been triggered and reset this bool on new bar.

 
angevoyageur:

You have to choose what you want. If you need a "good" signal then wait for closed candle. If you can deal with a "false" alert then trigger it on open candle, but you signal can have disappeared when the candle will be closed.

To manage the latter, you can for example set a bool that alert has been triggered and reset this bool on new bar.

in the code above I set the barstotal flag but it doesn't stop the alarm
 
SanMiguel:
in the code above I set the barstotal flag but it doesn't stop the alarm
https://www.mql5.com/en/forum/146525/page2#834677
 
People are trying to help you but you keep saying the same thing. Just think about it. What do you want? If you want the alert once per bar then you did get advice and code for that. If you want it to be able to change the alert to the opposite if the condition changes then you have to live with the problem of the alert going on and of. What is the use of such alert on a 5min bar? Of course you can code it in such a way that if the UP alert comes inside the bar, then you cannot have a DOWN alert untill the end of this bar. But why would you do that? Haven't you seen so many bars that look great for the upside and turn down before the bar closes? or vice versa. Wait for the end of the bar to get your signal! That is the best advise, and you already got it from others. Forget the 2.5 minutes. They will not save you if you are wrong and will not make you rich if you are right.
Reason: