how to alert once at a time

 

my code didnt perform well. ex :

if(normal logic... ){

if( !hasSentAlertOnce ){

Alert(" Alert " + Symbol() +" "+ Period());

hasSentAlertOnce= true;

}

}else {

hasSentAlertOnce = false;

}

anything wrong in this code (bt didnt wrk :( alert over n over ) . still couldnt figure out

 

..

static bool hasSentAlertOnce = false ;

..

if(normal logic... ){

if( !hasSentAlertOnce ){

Alert(" Alert " + Symbol() +" "+ Period());

hasSentAlertOnce= true;

}

}else {

hasSentAlertOnce = false;

}

 
Ais:

..

static bool hasSentAlertOnce = false ;

..

if(normal logic... ){

if( !hasSentAlertOnce ){

Alert(" Alert " + Symbol() +" "+ Period());

hasSentAlertOnce= true;

}

}else {

hasSentAlertOnce = false;

}

hasSentAlertOnce is global variable on my code.so is it necessary to set static ?
 
  1. Global == static

  2. Depending on the how often your 'normal logic' triggers, you might want to suppress the alert for a period of time:

    static datetime allowAlert;
    if (TimeCurrent() > allowAlert){ allowAlert=TimeCurrent()+ 5*60; Alert(...
 

..

//< Initialization >

bool hasSentAlertOnce = false ;

//</Initialization >

..

if(normal logic... ){

if( !hasSentAlertOnce ){

Alert(" Alert " + Symbol() +" "+ Period());

hasSentAlertOnce= true;

}

}else {

hasSentAlertOnce = false;

}

 
still no luk :(
 

You set your flag to TRUE

if( !hasSentAlertOnce ){
Alert(" Alert " + Symbol() +" "+ Period());
hasSentAlertOnce= true;
}

than a signal repeats and it is FALSE again,

}else {
hasSentAlertOnce = false;
}

thus no sense in changing the flag so often.

Use another trick:

if ( ALERT_CONDITION ) { Alert(); wasAlert = true; }

if ( ANTI_ALERT_CONDITION ) { wasAlert = false; }

ANTI_ALERT_CONDITION must be different from ALERT_CONDITION but not a simple "else".

If you have sell and buy signals alert it is better to use a switch:

int Last_Signal = 0; //--no signal so far

if ( buySignalAppeared && Last_Signal <= 0 ) { Alert(); Last_Signal = 1; } //--was sell, become buy

if ( sellSignalAppeared && Last_Signal >= 0 ) { Alert(); Last_Signal = -1; } //--was buy, become sell

 

..

//< Initialization >

bool hasSentAlertOnce = false ;

//</Initialization >

..

if(normal logic... ){

if( !hasSentAlertOnce ){

Alert(" Alert " + Symbol() +" "+ Period());

hasSentAlertOnce= true;

}

}//else {

//hasSentAlertOnce = false;

//}

 
double alertTag;
if (rule for alert && alertTag!=Time[0]){
   Alert (blablabla);
   alertTag=Time[0];
}
This will give you alert only once per bar, but if on next bar open rule for alert is still true it will gives you alert again on so on.
 
jurcekmpt:
This will give you alert only once per bar, but if on next bar open rule for alert is still true it will gives you alert again on so on.

This solution works well for me, butI had to declare "alertTag" as a STATIC DATETIME variable not a Double.

Thanks

 
  1. Of course you do.
  2. Do you really expect the original poster to be waiting for your post after seven (7) years? Don't resurrect old threads unless you have a good reason.
Reason: