Request add pop alert--Bollinger Bands

Post Reply
blueface
Trader
Posts: 27
Joined: Wed Dec 24, 2014 12:09 pm

Request add pop alert--Bollinger Bands

Post by blueface »

Hi guys


Do not know whether you can help me modify Bollinger Bands indicator?

I want to add pop alert ,when the candle touch the middle of the band

and add a option on/off to open and closed the alert
Snap1.jpg
Best Regards.
Bands.mq4
You do not have the required permissions to view the files attached to this post.
User avatar
renexxxx
Trader
Posts: 860
Joined: Sat Dec 31, 2011 3:48 am

Request add pop alert--Bollinger Bands

Post by renexxxx »

Hi blueface,

This is really simple and I will tell you how:

First you need the parameters to be able to switch alerts on/off and the required time between subsequent alerts (otherwise, if price would park on top of the middle bollinger band, you would get bombarded with alerts (for each tick)).
So add this to the list of parameters:

Code: Select all

input bool   AlertsOn            = true;     // Switch alert on (true) or off (false)
input int    AlertRepeatSeconds  =  600;     // Time in seconds between alerts
We need to do the comparison in OnCalculate(), as this is the event handler that is called when a new tick is received. We must remember when we last sent an alert -- in order to not send a new alert when less than AlertRepeatSeconds has passed since then. So we add a static variable to OnCalculate() like so:

Code: Select all

   // To remember the last time an alert was sent
   // Since this variable is 'static' it 'lives' within the function body
   // and doesn't get re-initialized each time the function is called.
   static datetime lastAlertTime = -1;
You might ask: Why not make this is global variable (declared at the top of the .mq4 file)? It is extremely bad to use global variables except if there really is no other way. When I learned to code some 35-years ago, I was told that one of my hands would be cut off if I were to use a global variable for something that can be declared locally. I am pleased to say that I still have use of both my hands.

Then we still need to add the code for actually checking the condition and popping up the Alert(), like so:

Code: Select all

//--- alert if price touches ExtMovingBuffer[0]
     if ( AlertsOn && ( lastAlertTime + AlertRepeatSeconds < TimeLocal() ) && ( Ask >= ExtMovingBuffer[0] ) && ( Bid <= ExtMovingBuffer[0] ) ) {
         Alert(StringFormat("[%s]: Price is touching middle BollingerBands line", Symbol()) );
         lastAlertTime = TimeLocal();
     }
Hope this is explanation is understandable.
You do not have the required permissions to view the files attached to this post.
blueface
Trader
Posts: 27
Joined: Wed Dec 24, 2014 12:09 pm

Request add pop alert--Bollinger Bands

Post by blueface »

renexxxx » Sat Aug 29, 2015 10:50 pm wrote:Hi blueface,
Hope this is explanation is understandable.
Hi bro

I will test tomorrow,thx a lot.

Kind Regards.
Post Reply

Return to “Coders Hangout”