Help with adding alert into indicator

 

hey guys I need a little help with adding alert function into an custom indicator.This is the code of the indicator.

I added the "NewBar" part to stop alerting at every tick and alert part at the end but the problem is that it alerts at every new bar.


I would like it to alert me when indicator changes color ( after the bar is closed)


any suggestions?


#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 2
#property indicator_maximum 1
#property indicator_minimum 0

//
//
//
//
//

extern int HMA_Period = 50;
extern int HMA_PriceType = 0;
extern int HMA_Method = 1;
extern bool AlertBUY = true;
extern bool AlertSELL = true;

//
//
//
//
//

double ind_buffer0[];
double ind_buffer1[];
double ind_buffer2[];
double buffer[];


//+------------------------------------------------------------------
//| |
//+------------------------------------------------------------------

int init()
{

//
//
//
//
//

IndicatorShortName("HMA("+HMA_Period+")");
IndicatorBuffers(4);
SetIndexBuffer(0,ind_buffer0);
SetIndexBuffer(1,ind_buffer1);
SetIndexBuffer(2,ind_buffer2);
SetIndexBuffer(3,buffer);
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);

int draw_begin=HMA_Period+MathFloor(MathSqrt(HMA_Period));
for (int i = 0; i < indicator_buffers; i++)
{
SetIndexDrawBegin(i,draw_begin);
SetIndexLabel(i,"Hull Moving Average");
}
return(0);
}


//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool NewBar()
{
static datetime lastbar;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}
else
{
return(false);
}
}
int start()
{
int HalfPeriod = MathFloor(HMA_Period/2);
int HullPeriod = MathFloor(MathSqrt(HMA_Period));
int counted_bars = IndicatorCounted();
int limit,i;


if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;

//
//
//
//
//

for(i=limit; i>=0; i--)
buffer[i]=iMA(NULL,0,HalfPeriod,0,HMA_Method,HMA_PriceType,i)*2-
iMA(NULL,0,HMA_Period,0,HMA_Method,HMA_PriceType,i);
for(i=limit; i>=0; i--)
{
ind_buffer0[i] = iMAOnArray(buffer,0,HullPeriod,0,HMA_Method,i);
ind_buffer1[i] = EMPTY_VALUE;
ind_buffer2[i] = EMPTY_VALUE;

//
//
//
//
//

if (ind_buffer0[i] > ind_buffer0[i+1]) ind_buffer1[i] = 1;
if(NewBar())
{
if(AlertBUY) {
Alert(" HMA on ",Symbol()," shows BUY signal");
}
}
if (ind_buffer0[i] < ind_buffer0[i+1]) ind_buffer2[i] = 1;


}
return(0);
}

Reason: