Lenar. Alert only once per 5 Minute Bar?

 
The indicator below gives multiple alerts when the condition keeps recurring.
Is it possible for Mt4 to send an alert only ONCE per 5 Minute Bar even if the same condition continues?

See the indicator below.

//+------------------------------------------------------------------+
//| Juice.mq4 |
//| Perky_z |
//| http://fxovereasy.atspace.com/index |
//+------------------------------------------------------------------+
#property copyright "perky"
#property link "http://fxovereasy.atspace.com/index"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 LimeGreen
#property indicator_color2 FireBrick
//---- indicator parameters
extern bool Alerts = true;
extern int Periyod=4;
extern double Level=0.0004;
extern bool LevelLines = true;

//---- indicator buffers
double OsMAUpBuffer[];
double OsMADownBuffer[];
double OsMABuffer[];
double MACDBuffer[];
double SignalBuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexDrawBegin(0,Level);
IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+2);
//---- 3 indicator buffers mapping
if(!SetIndexBuffer(0,OsMAUpBuffer) &&
!SetIndexBuffer(1,OsMADownBuffer) &&
!SetIndexBuffer(2,OsMABuffer) &&
!SetIndexBuffer(3,MACDBuffer) &&
!SetIndexBuffer(4,SignalBuffer))
Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("Juice("+Periyod+","+Level+")");


SetLevelStyle(STYLE_DASH,1,Silver);

//---- initialization done
return(0);
}

int SetLevelLines()
{
if( LevelLines )

{
if (StringSubstr(Symbol(),3,3)=="JPY")
{
SetLevelValue(1,0.05);
SetLevelValue(2,0.10);
SetLevelValue(3,0.15);
SetLevelValue(4,0.20);
}
else
{
SetLevelValue(1,0.0005);
SetLevelValue(2,0.0010);
SetLevelValue(3,0.0015);
SetLevelValue(4,0.0020);
}
}
else
{
SetLevelValue(1,0.0);
SetLevelValue(2,0.0);
SetLevelValue(3,0.0);
SetLevelValue(4,0.0);
}
}

//+------------------------------------------------------------------+
//| Moving Average of Oscillator |
//+------------------------------------------------------------------+
int start()
{
//if ( Period != 15) Alert ("Juice Is Recommended for 15 Min Chart only!!");
int limit,i;
int counted_bars=IndicatorCounted();
double Juice;
bool DoAlert = true;


//---- check for possible errors
if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
if (StringSubstr(Symbol(),3,3)=="JPY")
{
Level=Level*100;
}
if (Level== 0.0004 && Period()==5 ) Level=0.0002;
if (Level== 0.04 && Period()==5 ) Level=0.02; // for JPY crosses

SetLevelLines();

//---- main loop
for(i=0; i<limit; i++){
OsMABuffer[i]=iStdDev (NULL,0,Periyod,MODE_EMA,0,PRICE_CLOSE,i)-(Level);
Juice = OsMABuffer[i];
if(OsMABuffer[i]>0){
OsMAUpBuffer[i]=OsMABuffer[i];
OsMADownBuffer[i]=0;
}else if(OsMABuffer[i]<0){
OsMADownBuffer[i]=OsMABuffer[i];
OsMAUpBuffer[i]=0;
}else{
OsMAUpBuffer[i]=0;
OsMADownBuffer[i]=0;
}
}

if (Alerts)
{
if (Juice > 5*Point && Period() == 5)
{
if (DoAlert)
{
Alert("Juice above 7 for ", Symbol());
PlaySound("Tick.wav");
DoAlert = false;
}
}
else
{
DoAlert = true;
}
}


//---- done
return(0);
}
//+------------------------------------------------------------------+
 
I have accomplished this by creating a static variable, and when the alert occurs I assign the iTime() of the bar causing the alert to that static variable. Then, before issuing another alert I check if the iTime() is the same as it was on the last alert and if so, then do not do another alert. If it is different, then I do the alert and put the new iTime() value into the static variable so subsequent alerts can be tested.

the bar number of a bar changes as new candles appear, but the iTime() of a bar does not change.

something like:


static datetime lastPriceAlertedBar=0;
...
...
barToCheck=0;
...
...

if(alertCondition==true && lastPriceAlertedBar==iTime(NULL,0,barToCheck))
  {
  Alert("Alert");
  lastPriceAlertedBar==iTime(NULL,0,barToCheck);
  }
...
...




Hope this helps.

 
Hope this helps. [/quote]

Thanks for the awesome code ChristianB.

A friend has combineing the alert with the code on his priority list.

Thanks again.
Leigh.
 
No problem. After looking at it again I think I reversed the logic on the 2nd part of the if condition, but you got the idea anyway :)