//+------------------------------------------------------------------+ //| EMA_CROSS_IND.mq4 | //| Codersguru | //| http://www.forex-tsd.com | //+------------------------------------------------------------------+ #property copyright "Codersguru" #property link "http://www.forex-tsd.com" #property indicator_chart_window #property indicator_buffers 2 #property indicator_color1 Red #property indicator_color2 Blue extern int ShortEma = 10; extern int LongEma = 80; extern color UpCross = Gold; extern color DownCross = DarkGreen; extern int ArrowCode1 = 233; extern int ArrowCode2 = 234; extern bool reversal = true; extern bool Alert_On = true; //---- buffers double ExtMapBuffer1[]; double ExtMapBuffer2[]; void DrawArrow( int bar , double value , int trend) { static int cnt = 0; cnt++; string crossing = "CROSSING_" + cnt; string label = "LABEL" + cnt; ObjectCreate(crossing, OBJ_ARROW, 0, Time[bar], value); if(reversal==false) { if (trend == 1) trend = 2; else if (trend == 2) trend = 1; } if(trend == 1) { ObjectSet(crossing, OBJPROP_ARROWCODE, ArrowCode1); ObjectSet(crossing, OBJPROP_COLOR , UpCross); } if(trend == 2) { ObjectSet(crossing, OBJPROP_ARROWCODE, ArrowCode2); ObjectSet(crossing, OBJPROP_COLOR , DownCross); } ObjectSet(crossing, OBJPROP_WIDTH , 1); ObjectsRedraw(); } int Crossed (double line1 , double line2) { static int last_direction = 0; static int current_direction = 0; //Don't work in the first load, wait for the first cross! static bool first_time = true; if(first_time == true) { first_time = false; return (0); } if(line1>line2)current_direction = 1; //up if(line1<line2)current_direction = 2; //down if(current_direction != last_direction) //changed { last_direction = current_direction; return (last_direction); } else { return (0); //not changed } } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { //---- indicators SetIndexStyle(0,DRAW_LINE, STYLE_SOLID,2); SetIndexBuffer(0,ExtMapBuffer1); SetIndexStyle(1,DRAW_LINE, STYLE_SOLID,2); SetIndexBuffer(1,ExtMapBuffer2); string short_name = "EMA CROSS INDICATOR!"; IndicatorShortName(short_name); //---- return(0); } //+------------------------------------------------------------------+ //| Custor indicator deinitialization function | //+------------------------------------------------------------------+ int deinit() { //---- //Delete all the drawn objects! int I = WindowsTotal(); for (int count = 0; count < WindowsTotal(); count++) { ObjectsDeleteAll(count, OBJ_ARROW); ObjectsDeleteAll(count, OBJ_TEXT); } //---- return(0); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ int start() { double SEma, LEma; int counted_bars=IndicatorCounted(); if (counted_bars<0) return(-1); if (counted_bars>0) counted_bars--; int pos=Bars-counted_bars; static int isCrossed = 0; //---- main calculation loop while(pos>=0) { SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,pos); LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,pos); isCrossed = Crossed (LEma,SEma); if(isCrossed > 0) { DrawArrow (pos + 1 , SEma , isCrossed); } ExtMapBuffer1[pos]= SEma; ExtMapBuffer2[pos]= LEma; pos--; } //---- SEma = iMA(NULL,0,ShortEma,0,MODE_EMA,PRICE_CLOSE,0); LEma = iMA(NULL,0,LongEma,0,MODE_EMA,PRICE_CLOSE,0); isCrossed = Crossed (LEma,SEma); if(isCrossed > 0) { if(Alert_On) { if (isCrossed == 1) Alert("Short EMA(" + ShortEma + ") has been crossed Long EMA(" + LongEma + ") Upward"); if (isCrossed == 2) Alert("Short EMA(" + ShortEma + ")has been crossed Long EMA(" + LongEma + ") Downward"); } } return(0); } //+------------------------------------------------------------------+
Sample
Analysis
Market Information Used:
Series array that contains open time of each bar
Indicator Curves created:
Implements a curve of type DRAW_LINE
Indicators Used:
Moving average indicator
Custom Indicators Used:
Order Management characteristics:
Other Features:
It issuies visual alerts to the screen