Alert only on change

To add comments, please log in or register
GrumpyDuckMan
882
GrumpyDuckMan  

Hello everyone,

I have this statement in  OnTick(). I'm trying to work out a way to only alert if result changes. atm it triggers an alert everytime even if there isn't a change to result.

How is it possible to make it trigger the alert only if condition changes state?


     if(Condition1 && Condition2 && Condition3 && Condition4 && Condition5 && Condition6)
   
        result= true; 
   else result=false; 
Keith Watford
Moderator
20336
Keith Watford  
   static bool lastCondition=false;

   bool result=(Condition1 && Condition2 && Condition3 && Condition4 && Condition5 && Condition6)
   if(lastCondition && result)
      result=false;
   else
      lastCondition=result;

This assumes that you only want to action when the result changes from false to true.

GrumpyDuckMan
882
GrumpyDuckMan  

Hello again,

Thanks,

that looks a lot better than this.

   for(result=false;result!=true;ON_NEW--)
     {
      OFF_NEW=0;
      if(ON_NEW==1)Alert("new input");
      break;
     }
   for(result=true;result!=false;OFF_NEW--)
     {
      ON_NEW=0;
      if(OFF_NEW==1) Alert("new output");
      break;
nicholi shen
2392
nicholi shen  
GrumpyDuckMan:

Hello again,

Thanks,

that looks a lot better than this.

I'm not sure what those loops are trying to do... Assuming you want to "toggle" on fresh conditions you'll want to use a static int so you can initialize it with a sentinel value. 


void OnStart()
{
   while(!IsStopped()){
      alert_on_change(TimeLocal() % 2 == 0);
      Sleep(1);
   }
   
}

void alert_on_change(bool condition)
{
   static int state = WRONG_VALUE;
   if(state == WRONG_VALUE)
      state = condition;
   if(state != condition) {
      Print("Condition changed to ", condition);
      state = condition;
   }
}
GrumpyDuckMan
882
GrumpyDuckMan  

Hello again,

Thank you,

both on you helped a lot. I have never used static variables before. I am still reading over the use of them in the MQL4 reference manual atm.

GrumpyDuckMan
882
GrumpyDuckMan  

Hello again,

How about this,

    static bool lastCondition=result;
    bool result=(Condition1 && Condition2 && Condition3 && Condition4 && Condition5 && Condition6)
    if(lastCondition && result) result=result;
    else
    lastCondition=result; 
William Roeder
20207
William Roeder  
   static bool signalCur=false;   bool signalPre = signalCur;
   signalCur = Condition1 && Condition2 && Condition3 && Condition4 && Condition5 && Condition6;
   if(signalCur && !signalPre) DoIt();
To add comments, please log in or register