//+------------------------------------------------------------------+ //| Chart_Patterns ZigZag.mq4 based | //+------------------------------------------------------------------+ #property copyright "megabaks" #property link "megabaks@jabber.ru" #property indicator_chart_window #property indicator_buffers 1 #property indicator_color1 White //---- indicator parameters extern int maxExtDepth =12; extern int minExtDepth =11; extern int ExtDeviation =5; extern int ExtBackstep =3; extern bool show_zig_zag =true; extern bool show_targets =true; extern int max_percent_diff =50; extern int max_wave_diff =3; extern color triangle =Green; extern color head_shoulders =Red; extern color double_triple =Blue; //---- indicator buffers //ZigZag double ZigzagBuffer[]; double HighMapBuffer[]; double LowMapBuffer[]; //ZigZag's Extremums double EPrice[]; double ETime[]; double EShift[]; double EVolume[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(7); //---- drawing settings if ( show_zig_zag ) SetIndexStyle(0,DRAW_SECTION); else SetIndexStyle(0,DRAW_NONE); //---- indicator buffers mapping SetIndexBuffer(0,ZigzagBuffer); SetIndexBuffer(1,HighMapBuffer); SetIndexBuffer(2,LowMapBuffer); SetIndexBuffer(3,EPrice); SetIndexBuffer(4,ETime); SetIndexBuffer(5,EShift); SetIndexBuffer(6,EVolume); SetIndexEmptyValue(0,0.0); //---- initialization done return(0); } int deinit() { for (int i=0;iEPrice[sbar] && EShift[fbar]>EShift[sbar]) { count_diff = (EShift[fbar]-EShift[sbar]); price_diff = (EPrice[fbar]-EPrice[sbar]); speed = (price_diff/count_diff); } if ( EPrice[fbar]>EPrice[sbar] && EShift[fbar]EShift[sbar]) { count_diff = (EShift[fbar]-EShift[sbar]); price_diff = (EPrice[sbar]-EPrice[fbar]); speed = (price_diff/count_diff); } if ( EPrice[fbar] sparam && ( (1.0+fparam*100000)/(1.0+sparam*100000)*100 )<=(100+max_percent_diff) ) return(TRUE); if ( fparam < sparam && ( (1.0+sparam*100000)/(1.0+fparam*100000)*100 )<=(100+max_percent_diff) ) return(TRUE); } /////////// bool max_diff_int( int fparam, int sparam ) { if ( fparam > sparam && ( (1+fparam*100000)/(1+sparam*100000)*100 )<=(100+max_percent_diff) ) return(TRUE); if ( fparam < sparam && ( (1+sparam*100000)/(1+fparam*100000)*100 )<=(100+max_percent_diff) ) return(TRUE); } /////////// bool max_diff_triangle( int fparam1, int fparam2, int sparam1, int sparam2, int X ) { double fdiff; double sdiff; if ( EPrice[fparam1]>EPrice[fparam2] ) fdiff=(EPrice[fparam1]-EPrice[fparam2]); if ( EPrice[fparam1]EPrice[sparam2] ) sdiff=(EPrice[sparam1]-EPrice[sparam2]); if ( EPrice[sparam1]= sdiff && (1.0+fdiff*100000)/(1.0+sdiff*100000)<=X ) return(TRUE); if ( fdiff <= sdiff && (1.0+sdiff*100000)/(1.0+fdiff*100000)<=X ) return(TRUE); } /////////// void ObjectReCreate(string name, int type, int window, datetime time1, double price1, datetime time2=0, double price2=0, datetime time3=0, double price3=0) { if (ObjectFind(name)==-1) { ObjectCreate(name, type, window, time1, price1, time2, price2, time3, price3); } } //////////////////////////////// /////////////PATTERNS ///////////// HEAD_AND_SHOULDERS void HEAD_AND_SHOULDERS(int bar, int name) { if ( ( // TOP EPrice[bar]>EPrice[bar+1] && EPrice[bar+2]>EPrice[bar] && EPrice[bar+2]>EPrice[bar+4]&& EPrice[bar+4]>EPrice[bar+3]&& EPrice[bar+4]>EPrice[bar] //A higher left shoulder peak when compared to the right shoulder top results in a larger decline post breakout. && EPrice[bar+1]>EPrice[bar+3] // Patterns with up-sloping necklines perform better. && EPrice[bar+4]>EPrice[bar+6] && EPrice[bar+4]>EPrice[bar+1] && ( max_diff_int(EShift[bar+4]-EShift[bar+2], EShift[bar+2]-EShift[bar]) ) && ( max_diff_int(EShift[bar+3]-EShift[bar+2], EShift[bar+2]-EShift[bar+1]) ) && ( max_diff_double(EPrice[bar+2]-EPrice[bar+4], EPrice[bar+2]-EPrice[bar]) ) && ( max_diff_double(EPrice[bar+2]-EPrice[bar+3], EPrice[bar+2]-EPrice[bar+1]) ) ) || ( //BOTTOM EPrice[bar]EPrice[bar+3]) { price=EPrice[bar+3]+(speed_line(bar+3,bar+1)*(EShift[bar+3]-EShift[bar])); target=price-(EPrice[bar+2]-price); } if (EPrice[bar+2]=EPrice[bar+2] && EPrice[bar]EPrice[bar+2] && ( EPrice[bar+4]>EPrice[bar] && (EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )<=(EPrice[bar+2]) && max_diff_double((EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) || ( EPrice[bar+4](EPrice[bar+1]-EPrice[bar]) ) || ( EPrice[bar]<=EPrice[bar+2] && EPrice[bar]>EPrice[bar+1]&& EPrice[bar+3]=EPrice[bar] && (EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )>=(EPrice[bar+2]) && max_diff_double((EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) || ( EPrice[bar+4]<=EPrice[bar] && (EPrice[bar] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )>=(EPrice[bar+2]) && max_diff_double((EPrice[bar] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) && max_diff_triangle(bar+3, bar+4, bar+1, bar, 3) && (EPrice[bar+2]-EPrice[bar+3])>(EPrice[bar]-EPrice[bar+1]) ) ///Reverse Triangle || ( EPrice[bar]>EPrice[bar+2] && EPrice[bar]>EPrice[bar+1]&& EPrice[bar+3]EPrice[bar+1] && ( EPrice[bar+4]>=EPrice[bar] && (EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )>=(EPrice[bar+2]) && max_diff_double((EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) || ( EPrice[bar+4]<=EPrice[bar] && (EPrice[bar+4] + (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )>=(EPrice[bar+2]) && max_diff_double((EPrice[bar+4] + (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) && max_diff_triangle(bar+3, bar+4, bar+1, bar, 3) && (EPrice[bar+2]-EPrice[bar+3])>(EPrice[bar+4]-EPrice[bar+3]) && (EPrice[bar+2]-EPrice[bar+3])<(EPrice[bar]-EPrice[bar+1]) ) || ( EPrice[bar]EPrice[bar+2]&& EPrice[bar+3]=EPrice[bar] && (EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )<=(EPrice[bar+2]) && max_diff_double((EPrice[bar+4] - (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) || ( EPrice[bar+4]<=EPrice[bar] && (EPrice[bar+4] + (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) )<=(EPrice[bar+2]) && max_diff_double((EPrice[bar+4] + (speed_line(bar+4,bar) * (EShift[bar+4]-EShift[bar+2])) ),(EPrice[bar+2])) ) && max_diff_triangle(bar+3, bar+4, bar+1, bar, 3) && (EPrice[bar+3]-EPrice[bar+2])>(EPrice[bar+3]-EPrice[bar+4]) && (EPrice[bar+3]-EPrice[bar+2])<(EPrice[bar+1]-EPrice[bar]) ) ) ///draw its { ObjectReCreate("Triangle1 "+name, OBJ_TREND, 0,ETime[bar],EPrice[bar],ETime[bar+4],EPrice[bar+4]); ObjectReCreate("Triangle2 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar+3],EPrice[bar+3]); ObjectReCreate("Triangle3 "+name, OBJ_TREND, 0,ETime[bar+4],EPrice[bar+4],ETime[bar+3],EPrice[bar+3]); ObjectSet ("Triangle1 "+name, OBJPROP_RAY, false); ObjectSet ("Triangle2 "+name, OBJPROP_RAY, false); ObjectSet ("Triangle3 "+name, OBJPROP_RAY, false); ObjectSet ("Triangle1 "+name, OBJPROP_WIDTH, 2); ObjectSet ("Triangle2 "+name, OBJPROP_WIDTH, 2); ObjectSet ("Triangle3 "+name, OBJPROP_WIDTH, 1); ObjectSet ("Triangle3 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("Triangle1 "+name, OBJPROP_COLOR, triangle); ObjectSet ("Triangle2 "+name, OBJPROP_COLOR, triangle); ObjectSet ("Triangle3 "+name, OBJPROP_COLOR, triangle); } } /////////////////////////////////// //// DOUBLE_TOP_BOTTOM void DOUBLE_TOP_BOTTOM(int bar, int name) { if ( ( //TOP EPrice[bar+1]EPrice[bar+1] && max_diff_double(EPrice[bar+2]-EPrice[bar+1], EPrice[bar]-EPrice[bar+1]) && max_diff_int(EShift[bar+2]-EShift[bar+1],EShift[bar+1]-EShift[bar]) && EPrice[bar+2]>EPrice[bar+4] ) || ( //BOTTOM EPrice[bar+1]>EPrice[bar+2] && EPrice[bar]EPrice[bar+1] && EPrice[bar+2]<=EPrice[bar]) target=EPrice[bar+1]-(EPrice[bar+2]-EPrice[bar+1]); if (EPrice[bar+2]>EPrice[bar+1] && EPrice[bar+2]>=EPrice[bar]) target=EPrice[bar+1]-(EPrice[bar]-EPrice[bar+1]); if (EPrice[bar+2]=EPrice[bar]) target=EPrice[bar+1]+(EPrice[bar+1]-EPrice[bar+2]); ObjectReCreate("DOUBLE5 "+name, OBJ_TREND, 0,ETime[bar+1],target,ETime[bar],target); ObjectReCreate("DOUBLE6 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar+1],target); ObjectSet ("DOUBLE5 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE6 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE5 "+name, OBJPROP_WIDTH, 1); ObjectSet ("DOUBLE6 "+name, OBJPROP_WIDTH, 1); ObjectSet ("DOUBLE5 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("DOUBLE6 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("DOUBLE5 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("DOUBLE6 "+name, OBJPROP_COLOR, double_triple); } ObjectReCreate("DOUBLE1 "+name, OBJ_TREND, 0,ETime[bar+3],EPrice[bar+3],ETime[bar+2],EPrice[bar+2]); ObjectReCreate("DOUBLE2 "+name, OBJ_TREND, 0,ETime[bar+2],EPrice[bar+2],ETime[bar+1],EPrice[bar+1]); ObjectReCreate("DOUBLE3 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar],EPrice[bar]); ObjectReCreate("DOUBLE4 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar],EPrice[bar+1]); ObjectSet ("DOUBLE1 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE2 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE3 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE4 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE1 "+name, OBJPROP_WIDTH, 2); ObjectSet ("DOUBLE2 "+name, OBJPROP_WIDTH, 2); ObjectSet ("DOUBLE3 "+name, OBJPROP_WIDTH, 2); ObjectSet ("DOUBLE4 "+name, OBJPROP_WIDTH, 1); ObjectSet ("DOUBLE4 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("DOUBLE1 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("DOUBLE2 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("DOUBLE3 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("DOUBLE4 "+name, OBJPROP_COLOR, double_triple); if (EShift[bar-1]!=0) { ObjectReCreate("DOUBLE7 "+name, OBJ_TREND, 0,ETime[bar],EPrice[bar],ETime[bar-1],EPrice[bar-1]); ObjectSet ("DOUBLE7 "+name, OBJPROP_RAY, false); ObjectSet ("DOUBLE7 "+name, OBJPROP_WIDTH, 2); ObjectSet ("DOUBLE7 "+name, OBJPROP_COLOR, double_triple); } } } /////////////////////////////////// //// TRIPLE_TOP_BOTTOM void TRIPLE_TOP_BOTTOM(int bar, int name) { if ( ( //TOP EPrice[bar+1]EPrice[bar+1] && EPrice[bar+3]EPrice[bar+6] && ( EPrice[bar+3]>EPrice[bar+5] || max_diff_double(EPrice[bar+4]-EPrice[bar+3], EPrice[bar+4]-EPrice[bar+6]) ) ) || ( //BOTTOM EPrice[bar+1]>EPrice[bar+2] && EPrice[bar]EPrice[bar+4] && EPrice[bar+3]>EPrice[bar+2] && max_diff_double(EPrice[bar+1]-EPrice[bar+2], EPrice[bar+1]-EPrice[bar]) && max_diff_double(EPrice[bar+3]-EPrice[bar+2], EPrice[bar+3]-EPrice[bar+4]) && max_diff_double(EPrice[bar+3]-EPrice[bar+4], EPrice[bar+1]-EPrice[bar+2]) && max_diff_double(EPrice[bar+1]-EPrice[bar+4], EPrice[bar+3]-EPrice[bar+1]) && max_diff_int(EShift[bar+2]-EShift[bar+1],EShift[bar+1]-EShift[bar]) && max_diff_int(EShift[bar+4]-EShift[bar+3],EShift[bar+3]-EShift[bar+2]) && EPrice[bar+4]EPrice[bar+1]) { if (EPrice[bar+1]>=EPrice[bar+3]) { if (EPrice[bar+2]=EPrice[bar+3]) { if (EPrice[bar+2]>EPrice[bar] && EPrice[bar+2]>EPrice[bar+4]) target=EPrice[bar+1]+(EPrice[bar+3]-EPrice[bar+2]); if (EPrice[bar]>EPrice[bar+2] && EPrice[bar]>EPrice[bar+4]) target=EPrice[bar+1]+(EPrice[bar+3]-EPrice[bar]); if (EPrice[bar+4]>EPrice[bar+2] && EPrice[bar+4]>EPrice[bar]) target=EPrice[bar+1]+(EPrice[bar+3]-EPrice[bar+4]); } if (EPrice[bar+1]<=EPrice[bar+3]) { if (EPrice[bar+2]>EPrice[bar] && EPrice[bar+2]>EPrice[bar+4]) target=EPrice[bar+1]+(EPrice[bar+1]-EPrice[bar+2]); if (EPrice[bar]>EPrice[bar+2] && EPrice[bar]>EPrice[bar+4]) target=EPrice[bar+1]+(EPrice[bar+1]-EPrice[bar]); if (EPrice[bar+4]>EPrice[bar+2] && EPrice[bar+4]>EPrice[bar]) target=EPrice[bar+1]+(EPrice[bar+1]-EPrice[bar+4]); } } ObjectReCreate("TRIPLE5 "+name, OBJ_TREND, 0,ETime[bar+1],target,ETime[bar],target); ObjectReCreate("TRIPLE6 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar+1],target); ObjectSet ("TRIPLE5 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE6 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE5 "+name, OBJPROP_WIDTH, 1); ObjectSet ("TRIPLE6 "+name, OBJPROP_WIDTH, 1); ObjectSet ("TRIPLE5 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("TRIPLE6 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("TRIPLE5 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("TRIPLE6 "+name, OBJPROP_COLOR, double_triple); } ObjectReCreate("TRIPLE1 "+name, OBJ_TREND, 0,ETime[bar+3],EPrice[bar+3],ETime[bar+2],EPrice[bar+2]); ObjectReCreate("TRIPLE2 "+name, OBJ_TREND, 0,ETime[bar+2],EPrice[bar+2],ETime[bar+1],EPrice[bar+1]); ObjectReCreate("TRIPLE3 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar],EPrice[bar]); ObjectReCreate("TRIPLE4 "+name, OBJ_TREND, 0,ETime[bar+1],EPrice[bar+1],ETime[bar],EPrice[bar+1]); ObjectReCreate("TRIPLE7 "+name, OBJ_TREND, 0,ETime[bar+3],EPrice[bar+3],ETime[bar+4],EPrice[bar+4]); ObjectReCreate("TRIPLE8 "+name, OBJ_TREND, 0,ETime[bar+4],EPrice[bar+4],ETime[bar+5],EPrice[bar+5]); ObjectSet ("TRIPLE1 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE2 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE3 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE4 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE7 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE8 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE1 "+name, OBJPROP_WIDTH, 2); ObjectSet ("TRIPLE2 "+name, OBJPROP_WIDTH, 2); ObjectSet ("TRIPLE3 "+name, OBJPROP_WIDTH, 2); ObjectSet ("TRIPLE4 "+name, OBJPROP_WIDTH, 1); ObjectSet ("TRIPLE7 "+name, OBJPROP_WIDTH, 2); ObjectSet ("TRIPLE8 "+name, OBJPROP_WIDTH, 2); ObjectSet ("TRIPLE4 "+name, OBJPROP_STYLE, STYLE_DOT); ObjectSet ("TRIPLE1 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("TRIPLE2 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("TRIPLE3 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("TRIPLE4 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("TRIPLE7 "+name, OBJPROP_COLOR, double_triple); ObjectSet ("TRIPLE8 "+name, OBJPROP_COLOR, double_triple); if (EShift[bar-1]!=0) { ObjectReCreate("TRIPLE9 "+name, OBJ_TREND, 0,ETime[bar],EPrice[bar],ETime[bar-1],EPrice[bar-1]); ObjectSet ("TRIPLE9 "+name, OBJPROP_RAY, false); ObjectSet ("TRIPLE9 "+name, OBJPROP_WIDTH, 2); ObjectSet ("TRIPLE9 "+name, OBJPROP_COLOR, double_triple); } } } ///////////////////////////////////