//+------------------------------------------------------------------+ //| Zigzag_ws_Chanel.mq4 | //| Copyright © 2008, Dolsergon & MetaQuotes Software Corp. | //| http://tradecoder.narod.ru/ | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Copyright © 2008, Dolsergon & MetaQuotes Software Corp." #property link "http://tradecoder.narod.ru/; http://www.metaquotes.net/" #property indicator_chart_window #property indicator_buffers 3 #property indicator_color1 Red #property indicator_width1 2 #property indicator_color2 Pink #property indicator_color3 Pink //---- indicator parameters extern int ExtDepth=12; extern int ExtDeviation=5; extern int ExtBackstep=3; extern ENUM_BASE_CORNER btn_corner = CORNER_LEFT_UPPER; extern string btn_text = "clr"; extern string btn_Font = "Arial"; extern int btn_FontSize = 10; extern color btn_text_ON_color = clrLime; extern color btn_text_OFF_color = clrRed; extern string btn_pressed = "clr OFF"; extern string btn_unpressed = "clr ON"; extern color btn_background_color = clrDimGray; extern color btn_border_color = clrBlack; extern int button_x = 850; extern int button_y = 0; extern int btn_Width = 70; extern int btn_Height = 20; //---- indicator buffers double ZigzagBuffer[]; double HighMapBuffer[]; double LowMapBuffer[]; double BufChanelHigh[]; double BufChanelLow[]; int level=3; // recounting's depth bool downloadhistory=false; bool show_data = true; string IndicatorName, IndicatorObjPrefix ,buttonId ; string GenerateIndicatorName(const string target) { string name = target; int try = 2; while (WindowFind(name) != -1) { name = target + " #" + IntegerToString(try++); } return name; } //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int init() { IndicatorBuffers(5); //---- drawing settings SetIndexStyle(0,DRAW_SECTION); SetIndexBuffer(0,ZigzagBuffer); SetIndexEmptyValue(0,0.0); SetIndexStyle(1, DRAW_SECTION); SetIndexBuffer(1,BufChanelHigh); SetIndexEmptyValue(1,0.0); SetIndexStyle(2, DRAW_SECTION); SetIndexBuffer(2,BufChanelLow); SetIndexEmptyValue(2,0.0); SetIndexBuffer(3,HighMapBuffer); SetIndexBuffer(4,LowMapBuffer); //---- indicator short name IndicatorShortName("ZigZag_ws_Chanel("+ExtDepth+","+ExtDeviation+","+ExtBackstep+")"); //---- initialization done IndicatorName = GenerateIndicatorName(btn_text); IndicatorObjPrefix = "__" + IndicatorName + "__"; // IndicatorShortName(WindowExpertName()); IndicatorDigits(Digits); double val; if (GlobalVariableGet(IndicatorName + "_visibility", val)) show_data = val != 0; ChartSetInteger(ChartID(), CHART_EVENT_MOUSE_MOVE, 1); buttonId = IndicatorObjPrefix+btn_text; createButton(buttonId, btn_text, btn_Width, btn_Height, btn_Font, btn_FontSize, btn_background_color, btn_border_color, btn_text_ON_color); ObjectSetInteger(ChartID(), buttonId, OBJPROP_YDISTANCE, button_y); ObjectSetInteger(ChartID(), buttonId, OBJPROP_XDISTANCE, button_x); return(0); } int deinit() { //---- ObjectsDeleteAll(0,"clr"); ObjectsDeleteAll(ChartID(), IndicatorObjPrefix); //---- return(0); } void createButton(string buttonID,string buttonText,int width,int height,string font,int fontSize,color bgColor,color borderColor,color txtColor) { // ObjectDelete (ChartID(),buttonID); ObjectCreate (ChartID(),buttonID,OBJ_BUTTON,WindowOnDropped(),0,0); ObjectSetInteger(ChartID(),buttonID,OBJPROP_COLOR,txtColor); ObjectSetInteger(ChartID(),buttonID,OBJPROP_BGCOLOR,bgColor); ObjectSetInteger(ChartID(),buttonID,OBJPROP_BORDER_COLOR,borderColor); ObjectSetInteger(ChartID(),buttonID,OBJPROP_XSIZE,width); ObjectSetInteger(ChartID(),buttonID,OBJPROP_YSIZE,height); ObjectSetString (ChartID(),buttonID,OBJPROP_FONT,font); ObjectSetString (ChartID(),buttonID,OBJPROP_TEXT,buttonText); ObjectSetInteger(ChartID(),buttonID,OBJPROP_FONTSIZE,fontSize); ObjectSetInteger(ChartID(),buttonID,OBJPROP_SELECTABLE,0); ObjectSetInteger(ChartID(),buttonID,OBJPROP_CORNER,btn_corner); ObjectSetInteger(ChartID(),buttonID,OBJPROP_HIDDEN,1); ObjectSetInteger(ChartID(),buttonID,OBJPROP_XDISTANCE,9999); ObjectSetInteger(ChartID(),buttonID,OBJPROP_YDISTANCE,9999); } bool recalc = true; void handleButtonClicks() { if (ObjectGetInteger(ChartID(), buttonId, OBJPROP_STATE)) { ObjectSetInteger(ChartID(), buttonId, OBJPROP_STATE, false); show_data = !show_data; GlobalVariableSet(IndicatorName + "_visibility", show_data ? 1.0 : 0.0); recalc = true; start(); } } void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) { handleButtonClicks(); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int start() { handleButtonClicks(); recalc = false; SetIndexStyle(0,DRAW_SECTION); SetIndexStyle(1,DRAW_SECTION); SetIndexStyle(2,DRAW_SECTION); start2(); if (show_data) { ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_ON_color); ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_unpressed); } else { ObjectSetInteger(ChartID(),buttonId,OBJPROP_COLOR,btn_text_OFF_color); ObjectSetString(ChartID(),buttonId,OBJPROP_TEXT,btn_pressed); SetIndexStyle(0,DRAW_NONE); SetIndexStyle(1,DRAW_NONE); SetIndexStyle(2,DRAW_NONE); //template code } return(0); } int start2() { int i, counted_bars = IndicatorCounted(); int limit,counterZ,whatlookfor; int shift,back,lasthighpos,lastlowpos; double val,res; double curlow,curhigh,lasthigh,lastlow; if (counted_bars==0 && downloadhistory) // history was downloaded { ArrayInitialize(ZigzagBuffer,0.0); ArrayInitialize(BufChanelHigh,0.0); ArrayInitialize(BufChanelLow,0.0); ArrayInitialize(HighMapBuffer,0.0); ArrayInitialize(LowMapBuffer,0.0); } if (counted_bars==0) { limit=Bars-ExtDepth; downloadhistory=true; } if (counted_bars>0) { while (counterZ=0;i--) { ZigzagBuffer[i]=0.0; LowMapBuffer[i]=0.0; HighMapBuffer[i]=0.0; BufChanelHigh[i]=0.0; BufChanelLow[i]=0.0; } } for(shift=limit; shift>=0; shift--) { val=Low[iLowest(NULL,0,MODE_LOW,ExtDepth,shift)]; if(val==lastlow) val=0.0; else { lastlow=val; if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0; else { for(back=1; back<=ExtBackstep; back++) { res=LowMapBuffer[shift+back]; if((res!=0)&&(res>val)) LowMapBuffer[shift+back]=0.0; } } } if (Low[shift]==val) LowMapBuffer[shift]=val; else LowMapBuffer[shift]=0.0; //--- high val=High[iHighest(NULL,0,MODE_HIGH,ExtDepth,shift)]; if(val==lasthigh) val=0.0; else { lasthigh=val; if((val-High[shift])>(ExtDeviation*Point)) val=0.0; else { for(back=1; back<=ExtBackstep; back++) { res=HighMapBuffer[shift+back]; if((res!=0)&&(res=0;shift--) { res=0.0; switch(whatlookfor) { case 0: // look for peak or lawn if (lastlow==0 && lasthigh==0) { if (HighMapBuffer[shift]!=0) { lasthigh=High[shift]; lasthighpos=shift; whatlookfor=-1; SetHighZZ(shift, lasthigh); res=1; } if (LowMapBuffer[shift]!=0) { lastlow=Low[shift]; lastlowpos=shift; whatlookfor=1; SetLowZZ(shift, lastlow); res=1; } } break; case 1: // look for peak if (LowMapBuffer[shift]!=0.0 && LowMapBuffer[shift]lasthigh && LowMapBuffer[shift]==0.0) { SetHighZZ(lasthighpos, 0.0); lasthighpos=shift; lasthigh=HighMapBuffer[shift]; SetHighZZ(shift, lasthigh); } if (LowMapBuffer[shift]!=0.0 && HighMapBuffer[shift]==0.0) { lastlow=LowMapBuffer[shift]; lastlowpos=shift; SetLowZZ(shift, lastlow); whatlookfor=1; } break; default: return; } } return(0); } //================================================================================================= void SetLowZZ(int pShift, double pValue) { ZigzagBuffer[pShift]=pValue; BufChanelLow[pShift]=pValue; } //================================================================================================= void SetHighZZ(int pShift, double pValue) { ZigzagBuffer[pShift]=pValue; BufChanelHigh[pShift]=pValue; }