Newbie Programming Question, Is it possible to make a condition only occur once until it appears again?

 

Hi! So I am using what's below for buying or selling. It's a little messy because I am still experimenting, but I was wondering if anyone knew a way to program it so every time ma - ma2 is greater then 0 this condition occurs, then resets and doesn't check again until ma - ma2 is less then 0? If I can solve this issue the then i can finish the rest of my program!


Any help would be extremely appreciated!!!


void CheckForOpen()
  {
   double ma;
   double ma2;
   double ma3;
   int    res;
   bool pulse1;
   bool pulse2;
   
//---- go trading only for first tiks of new bar
   if(Volume[0]>1) return;
//---- get Moving Average 
   ma=iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,0);
  ma2=iMA(NULL,0,25,0,MODE_SMA,PRICE_CLOSE,0);
  ma3=iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,3);
//---- Nested if statements designed to prevent the EA from making more then 2 bids per rotation.

   if(ma - ma2 <= 0)
      {
      pulse1 = true;
      }
   if(ma - ma2 >= 0)
      {
      pulse2 = true;
      }
    
   if(ma - ma2 < -0.0020 && ma - ma2 <= 0)
     {
     pulse1 = false;
     }
   if(ma - ma2 > 0.0020 && ma - ma2 >= 0)
     {
     pulse2 = false;
     }

   if(ma - ma2 <= 0 && ma - ma2 > -0.0020)  
     {
      res=OrderSend(Symbol(),OP_SELL,InceptionLots,Bid,3,0,0,"",MAGICMA,0,Red);
      return;
     }
//---- inception buy conditions
   if(ma - ma2 >= 0 && ma - ma2 < 0.0020)  
     {
      res=OrderSend(Symbol(),OP_BUY,InceptionLots,Ask,3,0,0,"",MAGICMA,0,Blue);
      return;
     }

  }
 

You have answered your own question...


static bool CheckMa = false;

if(ma - ma2 < 0){ // ma2 is greater than ma
   CheckMa = true;
}

if(ma - ma2 > 0 && CheckMa ){ // ma is greater than ma2 and check is confirmed
  // Do your stuff here
  CheckMa = false;  // don't forget to disable after your done
}
Code isn't checked or compiled
 

Sometimes you just stare at a problem so long you lose sight of the simple answers...LOL

Good point, I could just use a toggle that way!


Thank you!

 
  1.   if(Volume[0]>1) return;
    

    Volume is unreliable (skipped ticks.) Bars is unreliable (stops changing at max bars in chart) Always use Time

    static datetime Time0;  bool    newBar  = Time0 < Time[0];
    if (!newBar) return(0);                   Time0 = Time[0];
    

  2. every time ma - ma2 is greater then 0 this condition occurs, then resets and doesn't check again until ma - ma2 is less then 0?
    Alternative would be only trade when ma crosses ma2.
       ma     =iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,1);
       maPrev =iMA(NULL,0,7,0,MODE_SMA,PRICE_CLOSE,2);
       ma2    =iMA(NULL,0,25,0,MODE_SMA,PRICE_CLOSE,1);
       ma2Prev=iMA(NULL,0,25,0,MODE_SMA,PRICE_CLOSE,2);
       if (ma > ma2 && maPrev <= ma2Prev) { // Do your stuff here