//+------------------------------------------------------------------+ //| cycle.mq5 | //| Copyright 2020, MetaQuotes Software Corp. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2020, MetaQuotes Software Corp." #property link "https://www.mql5.com" #property version "1.00" #property indicator_separate_window #property indicator_minimum 0 #property indicator_maximum 100 #property indicator_level1 20 #property indicator_level2 50 #property indicator_level3 80 #property indicator_buffers 8 #property indicator_color1 DarkOrchid //---- input parameters input int FastMA=12; input int SlowMA=24; input int Crosses=50; input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price input ENUM_APPLIED_PRICE InpAppliedPrice2=PRICE_TYPICAL; // Applied price2 //---- buffers double MA[]; double MCD[]; double MAfast[],MAslow[]; double Cross[]; double max_min[]; double PointDeviation[]; double PeriodTimeAVG[]; //---- var double smconst,ST,max,min; int ShiftFirstCross; int ShiftCrossesCross; int k,handle1,handle2,handle3; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { SetIndexBuffer(0, MA,INDICATOR_DATA); PlotIndexSetInteger(0,PLOT_DRAW_TYPE,DRAW_LINE); PlotIndexSetInteger(0,PLOT_LINE_STYLE,STYLE_SOLID); SetIndexBuffer(1, MCD,INDICATOR_CALCULATIONS); SetIndexBuffer(2, MAfast,INDICATOR_CALCULATIONS); SetIndexBuffer(3, MAslow,INDICATOR_CALCULATIONS); SetIndexBuffer(4, Cross,INDICATOR_CALCULATIONS); SetIndexBuffer(5, max_min,INDICATOR_CALCULATIONS); SetIndexBuffer(6, PointDeviation,INDICATOR_CALCULATIONS); SetIndexBuffer(7, PeriodTimeAVG,INDICATOR_CALCULATIONS); IndicatorSetString(INDICATOR_SHORTNAME,"cycle"); PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(5,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(6,PLOT_EMPTY_VALUE,0.0); PlotIndexSetDouble(7,PLOT_EMPTY_VALUE,0.0); handle1=iMA(NULL,0,FastMA,0,MODE_SMA,InpAppliedPrice); handle2=iMA(NULL,0,SlowMA,0,MODE_SMA,InpAppliedPrice); handle3=iMA(NULL,0,FastMA,0, MODE_EMA,InpAppliedPrice2)- iMA(NULL,0,SlowMA,0, MODE_EMA, InpAppliedPrice2); ShiftFirstCross=0; ShiftCrossesCross=0; k=0; max=0.; min=1000000.; } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const int begin, const double &price[]) { int counted_bars=prev_calculated; int i,j,limit,NumberCross,BarsCross; double prev,MinMACD,MaxMACD,delta,Sum_max_min; if(rates_total<=SlowMA) return(-1); if(counted_bars>0) counted_bars--; limit=rates_total-counted_bars; if(limit>rates_total-SlowMA-1) limit=rates_total-SlowMA-1; //+------------------------------------------------------------------+ int to_copy; if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total; else { to_copy=rates_total-prev_calculated; if(prev_calculated>0) to_copy++; } //+------------------------------------------------------------------+ double High[]; ArraySetAsSeries(High,true); CopyHigh(NULL,0,0,to_copy,High); //+------------------------------------------------------------------+ double Low[]; ArraySetAsSeries(Low,true); CopyLow(NULL,0,0,to_copy,Low); //+------------------------------------------------------------------+ //| Time AVG | //+------------------------------------------------------------------+ for(i=limit;i>0;i--) { Cross[i]=0.; CopyBuffer(handle1,0,0,to_copy,MAfast); CopyBuffer(handle2,0,0,to_copy,MAslow); if(MAfast[i]>=MAslow[i] && MAfast[i+1]MAslow[i+1]) { if(ShiftFirstCross==0) ShiftFirstCross=i; if(ShiftCrossesCross==0) { k++; if(k==Crosses+1) ShiftCrossesCross=i; } Cross[i]=-1.; max_min[i]=max-min; max=0.; min=1000000.; } if(maxLow[i]) min=Low[i]; } if(limit>ShiftCrossesCross) limit=ShiftCrossesCross; for(i=limit;i>0;i--) { j=i; while(Cross[j]==0.) j++; NumberCross=0; BarsCross=0; Sum_max_min=0.; while(NumberCross0;i--) { CopyBuffer(handle3,0,0,to_copy,MCD); MinMACD=MCD[i]; MaxMACD=MCD[i]; for(j=i+1; jMaxMACD) MaxMACD=MCD[j]; } delta=MaxMACD-MinMACD; if(delta==0.) ST=50.; else { ST=(MCD[i]-MinMACD)/delta*100; } prev=MA[i+1]; MA[i]=(2./(1.+PeriodTimeAVG[i+1]/2.))*(ST-prev)+prev; } return(rates_total); } //+------------------------------------------------------------------+