//+------------------------------------------------------------------+ //| 2MA_CrossOver_AlertNoMAs.mq4 | //| Copyright © 2008, Robert Hill | //| | //| Will send alert when MAs first cross | //| Option to not show MAs on chart | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Robert Hill" #property indicator_chart_window #property indicator_buffers 4 #property indicator_color1 Green #property indicator_color2 Red #property indicator_color3 Yellow #property indicator_color4 Aqua #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 1 #property indicator_width4 1 extern int fastMA_Period=10; extern int slowMA_Period=10; extern string m = "--Moving Average Types--"; extern string m0 = " 0 = SMA"; extern string m1 = " 1 = EMA"; extern string m2 = " 2 = SMMA"; extern string m3 = " 3 = LWMA"; extern string m4 = " 4 = LSMA"; extern int fastMA_Type=1; extern int slowMA_Type=1; extern string p = "--Applied Price Types--"; extern string p0 = " 0 = close"; extern string p1 = " 1 = open"; extern string p2 = " 2 = high"; extern string p3 = " 3 = low"; extern string p4 = " 4 = median(high+low)/2"; extern string p5 = " 5 = typical(high+low+close)/3"; extern string p6 = " 6 = weighted(high+low+close+close)/4"; extern int fastMA_AppliedPrice = 1;//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_AppliedPrice = 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 bool Show_MAs = true; extern bool Show_Alert = false; extern bool Play_Sound = true; extern bool Send_Mail = false; extern string SoundFilename = "alert.wav"; double CrossUp[]; double CrossDown[]; double MA_Fast[]; double MA_Slow[]; int CurrentTrend = 0; double myPoint; datetime lastalert; string lblFast, lblSlow; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0, DRAW_ARROW, EMPTY); SetIndexArrow(0, 233); SetIndexBuffer(0, CrossUp); SetIndexStyle(1, DRAW_ARROW, EMPTY); SetIndexArrow(1, 234); SetIndexBuffer(1, CrossDown); SetIndexStyle(2, DRAW_LINE, STYLE_SOLID); SetIndexBuffer(2, MA_Fast); SetIndexStyle(3, DRAW_LINE, STYLE_SOLID); SetIndexBuffer(3, MA_Slow); lblFast = getLbl(fastMA_Type); SetIndexLabel(2, lblFast + "(" + DoubleToStr(fastMA_Period, 0) + ")"); lblSlow = getLbl(slowMA_Type); SetIndexLabel(3, lblSlow + "(" + DoubleToStr(slowMA_Period, 0) + ")"); myPoint = SetPoint(Symbol()); lastalert=0; //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { int limit, i, counter; double tmp=0; double fastMAnow, slowMAnow, fastMAprevious, slowMAprevious, fastMAafter, slowMAafter; 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 = 1; 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_Type > 3) { fastMAnow = iLsma(fastMA_Period, fastMA_AppliedPrice,i); fastMAprevious = iLsma(fastMA_Period, fastMA_AppliedPrice,i+1); fastMAafter = iLsma(fastMA_Period, fastMA_AppliedPrice, i-1); } else { fastMAnow = iMA(NULL, 0, fastMA_Period, 0, fastMA_Type, fastMA_AppliedPrice, i); fastMAprevious = iMA(NULL, 0, fastMA_Period, 0, fastMA_Type, fastMA_AppliedPrice, i+1); fastMAafter = iMA(NULL, 0, fastMA_Period, 0, fastMA_Type, fastMA_AppliedPrice, i-1); } if (slowMA_Type > 3) { slowMAnow = iLsma(slowMA_Period, slowMA_AppliedPrice,i); slowMAprevious = iLsma(slowMA_Period, slowMA_AppliedPrice,i+1); slowMAafter = iLsma(slowMA_Period, slowMA_AppliedPrice, i-1); } else { slowMAnow = iMA(NULL, 0, slowMA_Period, 0, slowMA_Type, slowMA_AppliedPrice, i); slowMAprevious = iMA(NULL, 0, slowMA_Period, 0, slowMA_Type, slowMA_AppliedPrice, i+1); slowMAafter = iMA(NULL, 0, slowMA_Period, 0, slowMA_Type, slowMA_AppliedPrice, i-1); } if (Show_MAs) { MA_Fast[i] = fastMAnow; MA_Slow[i] = slowMAnow; } CrossUp[i] = EMPTY_VALUE; CrossDown[i] = EMPTY_VALUE; // Check for a fresh cross to reduce arrows and alerts if ((fastMAnow > slowMAnow) && (fastMAprevious < slowMAprevious) && (fastMAafter > slowMAafter)) { CrossUp[i] = Low[i] - Range*1.5; if ((lastalert!=Time[1]) && (CurrentTrend != 1)) { DoAlert("BUY"); lastalert=Time[1]; CurrentTrend = 1; } } // Check for a fresh cross to reduce arrows and alerts if ((fastMAnow < slowMAnow) && (fastMAprevious > slowMAprevious) && (fastMAafter < slowMAafter)) { CrossDown[i] = High[i] + Range*1.5; if ((lastalert!=Time[1]) && (CurrentTrend != -1)) { DoAlert("SELL"); lastalert=Time[1]; CurrentTrend = -1; } } } return(0); } double SetPoint(string mySymbol) { double mPoint, myDigits; myDigits = MarketInfo (mySymbol, MODE_DIGITS); if (myDigits < 4) mPoint = 0.01; else mPoint = 0.0001; return(mPoint); } string getLbl(int MA_Type) { string lbl; switch(MA_Type) { case 0 : lbl = "SMA"; break; case 1 : lbl = "EMA"; break; case 2 : lbl = "SMMA"; break; case 3 : lbl = "LWMA"; break; default : lbl = "LSMA"; // Used for any value less than 0 or greater than 3 break; } return(lbl); } double iLsma( int LSMAPeriod, int LSMAPrice,int shift) { double wt; double ma1=iMA(NULL,0,LSMAPeriod,0,MODE_SMA ,LSMAPrice,shift); double ma2=iMA(NULL,0,LSMAPeriod,0,MODE_LWMA,LSMAPrice,shift); wt = MathFloor((3.0*ma2-2.0*ma1)/myPoint)*myPoint; return(wt); } void DoAlert(string which) { string msg; msg = Symbol() + " " + Period() + "min " + lblFast + "(" + fastMA_Period + ")/" + lblSlow + "(" + slowMA_Period + ")"; if (which == "BUY") msg = "Cross Up on " + msg; else msg = "Cross Down on " + msg; if (Show_Alert) Alert(msg); if (Play_Sound) PlaySound(SoundFilename); if (Send_Mail) SendMail(msg, ""); }