PlaySound keeps repeating

 

Hey guys,

I am creating just a very basic EA that says if certain conditions are met for both and sell, then PlaySound. The conditions look like this:

if((iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)>iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1))&&(Open[1]>iSAR(NULL,0,0.02,0.2,1))) // open buy rule

{ PlaySound("alert.wav")}

...

if((iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)<iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1))&&(Open[1]<iSAR(NULL,0,0.02,0.2,1))) // open sell rule

{ PlaySound("alert.wav")}

All well and good, only issue is the same conditions keep getting met after each new tick, so it just plays the alert sound over and over again.

I am wondering what the logic would look like if I want it to simply play the sound once a new buy condition is met, then again when the signals change to a sell, then a new buy, etc. Just want to be alerted when the SAR/MACD indicators switch directions. Any ideas on what I could try? I'm drawing a blank here. Thanks a lot,

 
mattmoneywilson: want it to simply play the sound once a new buy condition is met, then again when the signals change to a sell, then a new buy, etc. Just want to be alerted when the SAR/MACD indicators switch directions. Any ideas on what I could try?
Did you search for once per bar?
bool isSellCondition =  iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)
                      < iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1)
                     && Open[1]<iSAR(NULL,0,0.02,0.2,1);
static datetime lastSellSound;
if(isSellCondition){ // open sell rule
  if(lastSellSound != Time[0]){ lastSellSound = Time[0]; PlaySound("alert.wav") }
  :
}
 
I did yes, only problem is that when a new bar is encountered, the same parameters described above are still met, so it will just end up making a sound every new bar as well. It's close to what I'm looking for but not quite. Any other suggestions? Thanks for the reply,
 
Check for a change
static bool isSellCondition; bool wasSellCondition = isSellCondition;
isSellCondition =  iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)
                 < iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1)
                && Open[1]<iSAR(NULL,0,0.02,0.2,1);
if(isSellCondition && !wasSellCondition){ // open sell rule
  PlaySound("alert.wav");
  :
}
 

Thank you for the quick response. It did make an alert at first, but it will not anymore? Below shows what I have coded.

Don't know if I explained it well before, (or if I coded it wrong) but all I want it to do is make the alert noise when a buy/sell condition is met, then only make a new alert when the direction changes. So if it alerts because a buy condition is met (ie. MACD Main > MACD Signal && Open > SAR), it should only alert again when a sell condition is made, then back to a buy, etc. Here is my code. Thanks again,

EDIT**** Seems the alert is only going off when the buy condition is met. How do I modify this for sell too?

static bool isBuyCondition; bool wasBuyCondition = isBuyCondition;

     int result=0;
     isBuyCondition =  iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)
                 > iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1)
                && Open[1]>iSAR(NULL,0,0.02,0.2,1);
            if(isBuyCondition && !wasBuyCondition){
        PlaySound ("alert.wav");
        
        }
        return(0);
     }
     static bool isSellCondition; bool wasSellCondition = isSellCondition;

     isSellCondition =  iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1)
                 < iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1)
                && Open[1]<iSAR(NULL,0,0.02,0.2,1);
         if(isSellCondition && !wasSellCondition){
        PlaySound ("alert.wav");
        
        return(0);
     
  }
Reason: