dynel14 wrote:
Hi George,
I have not seen the indicator was repainting!? I need an alert for color change.
regards
dynel14
Instead of doing a one-off to modify this indicator I decided to take a shot at writing a reusable alerting procedure. This is what I came up with.
First of all, to be a good alerting procedure you have to:
- Send alerts to screen or email or both
Keep track of when the last alert was sent so that it doesn't send them with every tick
Allow for sending them every tick if you really want to
Keep track of individual alerts so that you can have one alert every tick, one only every 5 minutes, another only once a day, etc.
I think this addresses the above.
Code: Select all //+-------------------------------------------------------------------------------------+
//uAlert(int AlertIdx, int Frequency, bool AlertDestination,
// string Msg, bool Condition1, bool Condition2=true,bool Condition3=true)
//+-------------------------------------------------------------------------------------+
//int AlertIdx -- use a unique index for each alert you want.
// I recommend you use #define statements
//int Frequency -- how often do you want to display/evaluate the alert. this way it
// doesn't ping you on every tick. Use standard PERIOD defines in
// Empty4 or uAlertEveryTick for every tick
//bool AlertDestination -- one of uALERT_SCREEN, uALERT_EMAIL or the combination
// uALERT_SCREEN|uALERT_EMAIL for both
//string Msg -- the message you would like to display
//bool Condition1 -- the condition you want evaluated. should result in a true or false
//bool Condition2=true -- optional additional conditions. not really necessary but
//bool Condition3=true could help make code readable. all conditions must be true
//+-------------------------------------------------------------------------------------+
//following defines are required
#define uAlertEveryTick -1 //evaluate alerts each time the routine is called
#define uALERT_SCREEN 2 //display alerts to screen
#define uALERT_EMAIL 4 //send alerts as emails
#define uALERT_EXTERNAL 8 //future use - run an external program
void uAlert(int AlertIdx, int Frequency, int AlertDestination, string Msg, bool Condition1, bool Condition2=true,bool Condition3=true) {
static int AlertCheck[10]; //assume there will be multiple alerts -- mostly so we save time on resizing
if (IsTesting()) return; //no alerts in testing
if (AlertDestination == 0) return; //hmmm, don't want to alert right now.
if (ArraySize(AlertCheck) < AlertIdx + 1)
ArrayResize(AlertCheck, AlertIdx+10); //try to make sure there is enough to keep adding
//now, do nothing unless it is a new bar or frequency = everytick
int CurrentBarTime = iTime(Symbol(),Frequency,0);
if (CurrentBarTime != AlertCheck[AlertIdx] || Frequency == uAlertEveryTick) {
//update the array to make sure we don't alert again until new bar
AlertCheck[AlertIdx] = CurrentBarTime;
//send a copy to the log
Print(WindowExpertName()," ",Symbol()," ",Msg);
//send to screen if requested
if (AlertDestination & uALERT_SCREEN == uALERT_SCREEN) {
Alert(WindowExpertName()," ",Symbol()," ",Msg);
}//(AlertDestination & uALERT_SCREEN == uALERT_SCREEN)
//send to EMAIL if requested
if (AlertDestination & uALERT_SCREEN == uALERT_SCREEN) {
SendMail("Alert from " + WindowExpertName(),Symbol()+" "+Msg);
}//(AlertDestination & uALERT_SCREEN == uALERT_SCREEN)
}//(CurrentBarTime != AlertCheck[AlertIdx] || Frequency == uAlertEveryTick)
}//uAlert(int AlertIdx, int Frequency, int AlertDestination, string Msg, bool Condition1, bool Condition2=true,bool Condition3=true)
Usage examples:
Code: Select all
//use defines to assign meaningful names to the different alerts in your program. make sure the numbers are different!
#define CrossAlert 0
#define NewHourAlert 1
#define OrderPlacedAlert 3
//Alert the user if price is above daily open, but only once an hour
uAlert(CrossAlert,PERIOD_H1,uALERT_SCREEN,"Price is above daily open",Bid > iOpen(Symbol(),PERIOD_D1,0));
//Alert at the start of a new hour, checking every 5 minutes
uAlert(NewHourAlert ,PERIOD_M5,uALERT_SCREEN,"New Hour",TimeHour(Time[0]) !=TimeHour(Time[1]));
//Alert as soon as an order is placed
uAlert(OrderPlacedAlert,uAlertEveryTick,uALERT_SCREEN,"New order placed",OrdersTotal() != SavedOrdersTotal);
Hopefully you get the idea. For this indicator, I think you want to do something like this... At the end of the start() procedure call (right before the return statement):
uAlert(CrossAlert,0,uALERT_SCREEN,Symbol()+" color change",(ForceUp[0]==0 && ForceUp[1]!=0) || (ForceUp[0] != 1 && ForceUp[1] == 0));
In retrospect, this seems a lot like overkill. You could also just do this:
Code: Select all static int alertbars=0;
if (alertbars != iBars(Symbol(),0)) {
alertbars = iBars(Symbol(),0);
if ((ForceUp[0]==0 && ForceUp[1]!=0) || (ForceUp[0] != 0 && ForceUp[1] == 0))
Alert(Symbol()+" color change");
}
It goes in the same spot.
George |