//+------------------------------------------------------------------+ //| MA-Crossover_Alert.mq4 | //| Copyright © 2005, Jason Robinson (jnrtrading) | //| http://www.jnrtading.co.uk | //| Modified by Robert Hill to add LSMA and alert or send email | //| Added Global LastAlert to try to have alert only on new cross | //| but does not seem to work. So indicator does alert every bar | //+------------------------------------------------------------------+ /* +------------------------------------------------------------------+ | Allows you to enter two ma periods and it will then show you at | | Which point they crossed over. It is more usful on the shorter | | periods that get obscured by the bars / candlesticks and when | | the zoom level is out. Also allows you then to remove the mas | | from the chart. (emas are initially set at 5 and 6) | +------------------------------------------------------------------+ */ #property copyright "Copyright © 2005, Jason Robinson (jnrtrading)" #property link "http://www.jnrtrading.co.uk" #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 LawnGreen #property indicator_color2 Red extern bool Show_Alert = false; extern bool Play_Sound = true; extern bool Send_Mail = false; extern string SoundFilename = "alert.wav"; extern int FastMA_Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma extern int FastMA_Period = 5; extern int FastPriceMode = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4 extern int SlowMA_Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma, 4=lsma extern int SlowMA_Period = 12; extern int SlowPriceMode = 0;//0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4 double CrossUp[]; double CrossDown[]; int flagval1 = 0; int flagval2 = 0; double myPoint; int CurrentTrend = 0; datetime lastalert; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0, DRAW_ARROW, EMPTY, 3); SetIndexArrow(0, 233); SetIndexBuffer(0, CrossUp); SetIndexStyle(1, DRAW_ARROW, EMPTY, 3); SetIndexArrow(1, 234); SetIndexBuffer(1, CrossDown); myPoint = SetPoint(); lastalert=0; //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| LSMA with PriceMode | //| PrMode 0=close, 1=open, 2=high, 3=low, 4=median(high+low)/2, | //| 5=typical(high+low+close)/3, 6=weighted(high+low+close+close)/4 | //+------------------------------------------------------------------+ double iLsma(int TimeFrame, int LSMAPeriod, int LSMAPrice,int shift) { double wt; double ma1=iMA(NULL,TimeFrame,LSMAPeriod,0,MODE_SMA ,LSMAPrice,shift); double ma2=iMA(NULL,TimeFrame,LSMAPeriod,0,MODE_LWMA,LSMAPrice,shift); // Modified by Robert to make calculation to the whole pip level wt = MathFloor((3.0*ma2-2.0*ma1)/myPoint)*myPoint; return(wt); } double SetPoint() { double mPoint; if (Digits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int limit, i, counter; double tmp=0; double fastMAnow, slowMAnow, fastMAprevious, slowMAprevious; double Range, AvgRange; int counted_bars=IndicatorCounted(); //---- 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; for(i = 0; i <= limit; i++) { counter=i; Range=0; AvgRange=0; for (counter=i ;counter<=i+9;counter++) { AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]); } Range=AvgRange/10; if (FastMA_Mode == 4) { fastMAnow = iLsma(0,FastMA_Period, FastPriceMode, i); fastMAprevious = iLsma(0,FastMA_Period, FastPriceMode, i+1); } else { fastMAnow = iMA(NULL, 0, FastMA_Period, 0, FastMA_Mode, FastPriceMode, i); fastMAprevious = iMA(NULL, 0, FastMA_Period, 0, FastMA_Mode, FastPriceMode, i+1); } if (SlowMA_Mode == 4) { slowMAnow = iLsma(0, SlowMA_Period, SlowPriceMode, i); slowMAprevious = iLsma(0, SlowMA_Period, SlowPriceMode, i+1); } else { slowMAnow = iMA(NULL, 0, SlowMA_Period, 0, SlowMA_Mode, SlowPriceMode, i); slowMAprevious = iMA(NULL, 0, SlowMA_Period, 0, SlowMA_Mode, SlowPriceMode, i+1); } CrossUp[i] = EMPTY_VALUE; CrossDown[i] = EMPTY_VALUE; if ((fastMAnow > slowMAnow) && (fastMAprevious < slowMAprevious)) { CrossUp[i] = Low[i] - Range*0.75; if ((lastalert!=Time[1]) && (CurrentTrend != 1)) { if (Show_Alert == 1) { Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES),"\n Symbol=",Symbol()," Period=",Period()); } if (Play_Sound == 1) { PlaySound(SoundFilename); } if (Send_Mail == 1) { SendMail("BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES) +" Symbol="+Symbol()+" Period="+Period()); } lastalert=Time[1]; CurrentTrend = 1; } } else if ((fastMAnow < slowMAnow) && (fastMAprevious > slowMAprevious)) { CrossDown[i] = High[i] + Range*0.75; if ((lastalert!=Time[1]) && (CurrentTrend != -1)) { if (Show_Alert) { Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES),"\n Symbol=",Symbol()," Period=",Period()); } if (Play_Sound) { PlaySound(SoundFilename); } if (Send_Mail) { SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period()); } lastalert=Time[1]; CurrentTrend = -1; } } } return(0); }