//+------------------------------------------------------------------+ //| 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 3 #property indicator_color1 Aqua #property indicator_width1 1 #property indicator_color2 LawnGreen #property indicator_width2 2 #property indicator_color3 Red #property indicator_width3 2 extern bool SoundON=true; extern bool EmailON=false; extern bool ShowArrows = true; extern int MA_Period = 200; extern string m="MA Modes"; extern string m1="0=sma, 1=ema, 2=smma, 3=lwma"; extern string pm = "Price Modes"; extern string pm1 = " 0=close, 1=open, 2=high, 3=low"; extern string pm2 = " 4=median(high+low)/2"; extern string pm3 = " 5=typical(high+low+close)/3"; extern string pm4 = " 6=weighted(high+low+close+close)/4"; extern int MA_Mode = 1; //0=sma, 1=ema, 2=smma, 3=lwma extern int MA_PriceMode = 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 MA_Buf[]; double CrossUp[]; double CrossDown[]; int flagval1 = 0; int flagval2 = 0; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators // IndicatorBuffers(3); SetIndexStyle(0, DRAW_LINE, EMPTY); SetIndexBuffer(0, MA_Buf); SetIndexStyle(1, DRAW_ARROW, EMPTY); SetIndexArrow(1, 233); SetIndexBuffer(1, CrossUp); SetIndexStyle(2, DRAW_ARROW, EMPTY); SetIndexArrow(2, 234); SetIndexBuffer(2, CrossDown); //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int limit, i, counter; double tmp=0, CurrentClose, PreviousClose; double MAnow, MAprevious; 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; MAnow = iMA(NULL, 0, MA_Period, 0, MA_Mode, MA_PriceMode, i); MAprevious = iMA(NULL, 0, MA_Period, 0, MA_Mode, MA_PriceMode, i+1); MA_Buf[i] = MAnow; CurrentClose = iClose(NULL, 0, i); PreviousClose = iClose(NULL, 0, i+1); if ( (CurrentClose > MAnow) && (PreviousClose < MAprevious) ) { if (i == 0 && flagval1==0) { flagval1=1; flagval2=0; if (SoundON) Alert("BUY signal at Ask=",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period()); if (EmailON) SendMail("BUY signal alert","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period()); } if (ShowArrows) CrossUp[i] = Low[i] - Range*0.75; } else if ( (CurrentClose < MAnow) && (PreviousClose > MAprevious)) { if (i == 0 && flagval2==0) { flagval2=1; flagval1=0; if (SoundON) Alert("SELL signal at Ask=",Ask,"\n Bid=",Bid,"\n Date=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period()); if (EmailON) SendMail("SELL signal alert","SELL signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period()); } if (ShowArrows) CrossDown[i] = High[i] + Range*0.75; } } return(0); }