Need Alert Only Once, Please help for this Alert Code..

 

I need help to correct this code, it is keep alerting continuously when margin < 200 , but I want only once.!!!


void marginlevelwarning()

 {  

   double freemargin = MathRound(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL));

     if (freemargin < 200)

          {

         Alert("Margin Level Low : ", freemargin," %.");

         }

}

Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
Documentation on MQL5: Constants, Enumerations and Structures / Environment State / Account Properties
  • www.mql5.com
Account Properties - Environment State - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Seb2021:

I need help to correct this code, it is keep alerting continuously when margin < 200 , but I want only once.!!!


use the source </> button to enter code

void marginlevelwarning()
{  
   static bool StopAlert = false;
   double freemargin = MathRound(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL));

   if(freemargin >= 200) 
      StopAlert = false;
   else if(StopAlert == false)
   {
      Alert("Margin Level Low : ", freemargin," %.");
      StopAlert = true; // stop further alerts until >= 200 and drops below 200
   }
}
 
Seb2021:

I need help to correct this code, it is keep alerting continuously when margin < 200 , but I want only once.!!!


void marginlevelwarning()

 {  

   double freemargin = MathRound(AccountInfoDouble(ACCOUNT_MARGIN_LEVEL));

     if (freemargin < 200)

          {

         Alert("Margin Level Low : ", freemargin," %.");

         }

}

Declare a global boolean variable or a static boolean variable called maybe "isAlertTriggered" and set it to false and add it to "if( !isAlertTriggered && freemargin < 0)" condition. Add a line of code after the condition to assign the value true to this boolean variable. This way you won't get another new alert again even if the freemargin condition is still true.

 

You are looking at a signal. Act on a change of signal.
          MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 (2017)

 

Thanks for help. I got it.