//+------------------------------------------------------------------+ //| AutoFibo_MK.mq5 | //| Copyright © 2024, MetaQuotes Software Corp. | //| http://www.metaquotes.net/ | //+------------------------------------------------------------------+ #property copyright "Marek Korzeniewski" #property link "sunok.fx@gmail.com" #property version "1.00" #property description "AutoFibo based on Zigzag by Nikolay Kositsin" //+------------------------------------------------------------------+ //| Indicator drawing parameters | //+------------------------------------------------------------------+ //---- drawing the indicator in the main window #property indicator_chart_window //---- 3 buffers are used for calculation and drawing the indicator #property indicator_buffers 3 //---- one plot is used #property indicator_plots 1 //---- ZIGZAG is used for the indicator #property indicator_type1 DRAW_COLOR_ZIGZAG #property indicator_label1 "AutoFibo" //---- displaying the indicator label #property indicator_color1 Magenta,BlueViolet//---- blue-violet color is used as the color of the indicator line #property indicator_style1 STYLE_DASH //---- the indicator line is a long dashed line #property indicator_width1 1 //---- indicator line width is equal to 1 //+------------------------------------------------------------------+ //| Indicator input parameters | //+------------------------------------------------------------------+ input int ExtDepth = 12; input int ExtDeviation = 6; input int ExtBackstep = 4; //---- Fibo features at the last high input bool DynamicFiboFlag = true; // DynamicFibo display flag input color DynamicFibo_color = Blue; // DynamicFibo color input ENUM_LINE_STYLE DynamicFibo_style= STYLE_DOT; // DynamicFibo style input int DynamicFibo_width = 1; // DynamicFibo line width input bool DynamicFibo_AsRay = true; // DynamicFibo ray //---- Fibo features at the second to last high input bool StaticFiboFlag = true; // StaticFibo display flag input color StaticFibo_color = Red; // StaticFibo color input ENUM_LINE_STYLE StaticFibo_style = STYLE_DOT; // StaticFibo style input int StaticFibo_width = 1; // StaticFibo line width input bool StaticFibo_AsRay = false; // StaticFibo ray //+----------------------------------------------+ double LowestBuffer[]; double HighestBuffer[]; double ColorBuffer[]; //---- declaration of memory variables for recalculation of the indicator only at the previously not calculated bars int LASTlowpos,LASThighpos,LASTColor; double LASTlow0,LASTlow1,LASThigh0,LASThigh1; //---- declaration of the integer variables for the start of data calculation int StartBars; //+------------------------------------------------------------------+ //| Fibo Create | //+------------------------------------------------------------------+ void CreateFibo(long chart_id, // chart ID string name, // object name int nwin, // window index datetime time1, // price level time 1 double price1, // price level 1 datetime time2, // price level time 2 double price2, // price level 2 color Color, // line color int style, // line style int width, // line width int ray, // ray direction: -1 - to the left, +1 - to the right, other values - no ray string text) // text { //---- ObjectCreate(chart_id,name,OBJ_FIBO,nwin,time1,price1,time2,price2); ObjectSetInteger(chart_id,name,OBJPROP_COLOR,Color); ObjectSetInteger(chart_id,name,OBJPROP_STYLE,style); ObjectSetInteger(chart_id,name,OBJPROP_WIDTH,width+2); if(ray>0)ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,true); if(ray<0)ObjectSetInteger(chart_id,name,OBJPROP_RAY_LEFT,true); if(ray==0) { ObjectSetInteger(chart_id,name,OBJPROP_RAY_RIGHT,false); ObjectSetInteger(chart_id,name,OBJPROP_RAY_LEFT,false); } ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectSetInteger(chart_id,name,OBJPROP_BACK,false); for(int numb=0; numb<10; numb++) { ObjectSetInteger(chart_id,name,OBJPROP_LEVELCOLOR,numb,Color); ObjectSetInteger(chart_id,name,OBJPROP_LEVELSTYLE,numb,style); ObjectSetInteger(chart_id,name,OBJPROP_LEVELWIDTH,numb,width); } //---- } //+------------------------------------------------------------------+ //| Fibo reinstallation | //+------------------------------------------------------------------+ void SetFibo(long chart_id, // chart ID string name, // object name int nwin, // window index datetime time1, // price level time 1 double price1, // price level 1 datetime time2, // price level time 2 double price2, // price level 2 color Color, // line color int style, // line style int width, // line width int ray, // ray direction: -1 - to the left, 0 - no ray, +1 - to the right string text) // text { //---- if(ObjectFind(chart_id,name)==-1) CreateFibo(chart_id,name,nwin,time1,price1,time2,price2,Color,style,width,ray,text); else { ObjectSetString(chart_id,name,OBJPROP_TEXT,text); ObjectMove(chart_id,name,0,time1,price1); ObjectMove(chart_id,name,1,time2,price2); } //---- } //+------------------------------------------------------------------+ //| Searching for the very first ZigZag high in time series buffers | //+------------------------------------------------------------------+ int FindFirstExtremum(int StartPos,int Rates_total,double &UpArray[],double &DnArray[],int &Sign,double &Extremum) { //---- if(StartPos>=Rates_total)StartPos=Rates_total-1; for(int bar=StartPos; bar=Rates_total)StartPos=Rates_total-1; if(Direct==-1) for(int bar=StartPos; barrates_total || prev_calculated<=0)// checking for the first start of the indicator calculation { limit=rates_total-StartBars; // starting index for calculation of all bars climit=limit; // starting index for the indicator coloring lastlow1=-1; lasthigh1=-1; lastlowpos=-1; lasthighpos=-1; } else { limit=rates_total-prev_calculated; // starting index for calculation of new bars climit=limit+StartBars; // starting index for the indicator coloring //---- restore values of the variables lastlow0=LASTlow0; lasthigh0=LASThigh0; lastlow1=LASTlow1; lasthigh1=LASThigh1; lastlowpos=LASTlowpos+limit; lasthighpos=LASThighpos+limit; } //---- indexing elements in arrays as timeseries ArraySetAsSeries(high,true); ArraySetAsSeries(low,true); ArraySetAsSeries(time,true); //---- first big indicator calculation loop for(bar=limit; bar>=0 && !IsStopped(); bar--) { //---- store values of the variables before running at the current bar if(rates_total!=prev_calculated && bar==0) { LASTlow0=lastlow0; LASThigh0=lasthigh0; } //---- low val=low[ArrayMinimum(low,bar,ExtDepth)]; if(val==lastlow0) val=0.0; else { lastlow0=val; if((low[bar]-val)>(ExtDeviation*_Point))val=0.0; else { for(back=1; back<=ExtBackstep; back++) { res=LowestBuffer[bar+back]; if((res!=0) && (res>val)) { LowestBuffer[bar+back]=0.0; } } } } LowestBuffer[bar]=val; //---- high val=high[ArrayMaximum(high,bar,ExtDepth)]; if(val==lasthigh0) val=0.0; else { lasthigh0=val; if((val-high[bar])>(ExtDeviation*_Point))val=0.0; else { for(back=1; back<=ExtBackstep; back++) { res=HighestBuffer[bar+back]; if((res!=0) && (res=0 && !IsStopped(); bar--) { //---- store values of the variables before running at the current bar if(rates_total!=prev_calculated && bar==0) { LASTlow1=lastlow1; LASThigh1=lasthigh1; //---- LASTlowpos=lastlowpos; LASThighpos=lasthighpos; } curlow=LowestBuffer[bar]; curhigh=HighestBuffer[bar]; //---- if((curlow==0) && (curhigh==0))continue; //---- if(curhigh!=0) { if(lasthigh1>0) { if(lasthigh10) { if(lastlow1>curlow) { LowestBuffer[lastlowpos]=0; } else { LowestBuffer[bar]=0; } } //---- if((curlow=0 && !IsStopped(); bar--) { Max=HighestBuffer[bar]; Min=LowestBuffer[bar]; if(!Max && !Min) ColorBuffer[bar]=ColorBuffer[bar+1]; if(Max && Min) { if(ColorBuffer[bar+1]==0) ColorBuffer[bar]=1; else ColorBuffer[bar]=0; } if( Max && !Min) ColorBuffer[bar]=1; if(!Max && Min) ColorBuffer[bar]=0; } //---- Fibo creation if(StaticFiboFlag || DynamicFiboFlag) { bar1=FindFirstExtremum(0,rates_total,HighestBuffer,LowestBuffer,sign,price1); bar2=FindSecondExtremum(sign,bar1,rates_total,HighestBuffer,LowestBuffer,sign,price2); if(DynamicFiboFlag) { SetFibo(0,"DynamicFibo",0,time[bar2],price2,time[bar1],price1, DynamicFibo_color,DynamicFibo_style,DynamicFibo_width,DynamicFibo_AsRay,"DynamicFibo"); } if(StaticFiboFlag) { bar3=FindSecondExtremum(sign,bar2,rates_total,HighestBuffer,LowestBuffer,sign,price3); SetFibo(0,"StaticFibo",0,time[bar3],price3,time[bar2],price2, StaticFibo_color,StaticFibo_style,StaticFibo_width,StaticFibo_AsRay,"StaticFibo"); } ChartRedraw(0); } //---- return(rates_total); } //+------------------------------------------------------------------+