Hi,
I have a moving averages cross indior that produces an alert each time that the moving averages cross.
That I would enjoy the indior to create an alert only when a cross has just occurred (i.e. on the current bar). However, with this indior, each time I open up MT4 or switch between timeframes I'm getting alerts even though the cross has occurred 2-3 bars ago.
Here's a copy of the code under. Many thanks.
#property indior_chart_window
#property indior_buffers 2
#property indior_color1 DarkGoldenrod
#property indior_color2 FireBrick
double CrossUp[];
double CrossDown[];
extern int FasterEMA = 1;
extern int SlowerEMA = 50;
extern bool SoundON=true;
double alertTag;
double control=2147483647;
// ------------------------------------------------------------------
//| Custom indior initialization function |
// ------------------------------------------------------------------
int init()
//-- indiors
SetIndexStyle(0, DRAW_ARROW, EMPTY,1);
SetIndexArrow(0, 233);
SetIndexBuffer(0, CrossUp);
SetIndexStyle(1, DRAW_ARROW, EMPTY,1);
SetIndexArrow(1, 234);
SetIndexBuffer(1, CrossDown);
//--
return(0);
// ------------------------------------------------------------------
//| Custom indior deinitialization function |
// ------------------------------------------------------------------
int deinit()
//--
//--
return(0);
// ------------------------------------------------------------------
//| Custom indior iteration function |
// ------------------------------------------------------------------
int start()
int limit, i, counter;
double fasterEMAnow, slowerEMAnow, fasterEMAprevious, slowerEMAprevious, fasterEMAafter, slowerEMAafter;
double Range, AvgRange;
int counted_bars=IndiorCounted();
//-- check for possible errors
if(counted_barslt;0) return(-1);
//-- last counted bar will be recounted
if(counted_barsgt;0) counted_bars--;
limit=Bars-counted_bars;
for(i = 0; i lt;= limit; i )
counter=i;
Range=0;
AvgRange=0;
for (counter=i ;counterlt;=i 9;counter )
AvgRange=AvgRange MathAbs(High[counter]-Low[counter]);
Range=AvgRange/10;
fasterEMAnow = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i);
fasterEMAprevious = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i 1);
fasterEMAafter = iMA(NULL, 0, FasterEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);
slowerEMAnow = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i);
slowerEMAprevious = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i 1);
slowerEMAafter = iMA(NULL, 0, SlowerEMA, 0, MODE_EMA, PRICE_CLOSE, i-1);
if ((fasterEMAnow gt; slowerEMAnow) (fasterEMAprevious lt; slowerEMAprevious) (fasterEMAafter gt; slowerEMAafter))
CrossUp[i] = Low[i] - Range*0.5;
else if ((fasterEMAnow lt; slowerEMAnow) (fasterEMAprevious gt; slowerEMAprevious) (fasterEMAafter lt; slowerEMAafter))
CrossDown[i] = High[i] Range*0.5;
if (SoundON==true i==1 CrossUp[i] gt; CrossDown[i] alertTag! =Time[0])
Alert(MACD 0 cross DOWN on ,Symbol(), ,Period());
alertTag = Time[0];
SendNotifiion(MACD 0 cross DOWN on _Symbol);
if (SoundON==accurate I==1 CrossUp[I] lt; CrossDown[I] alertTag! =Time[0])
Alert(MACD 0 cross UP on ,Symbol(), ,Period());
alertTag = Time[0];
SendNotifiion(MACD 0 cross UP on _Symbol);
return(0);