//+------------------------------------------------------------------+ //| AD.mq5 | //| Copyright 2009-2017, MetaQuotes Software Corp. | //| http://www.mql5.com | //+------------------------------------------------------------------+ #property copyright "2009-2017, MetaQuotes Software Corp." #property link "http://www.mql5.com" #property description "Accumulation/Distribution" #property indicator_separate_window #property indicator_buffers 7 #property indicator_plots 7 #property indicator_type6 DRAW_LINE #property indicator_color6 LightSeaGreen #property indicator_label6 "A/D" //--- input params //---- buffers double ExtADbuffer[]; double Buffer0[]; double Buffer1[]; double ExtMainBuffer[]; double ExtSignalBuffer[]; double ExtHighesBuffer[]; double ExtLowesBuffer[]; double Buffer2[]; int InpKPeriod=9; // K period int InpSlowing=3; // Slowing datetime Starttime=D'2019.05.17 00:15:00'; int BRS=iBarShift(_Symbol,PERIOD_M15,Starttime,false); int b,m; int limit=iBarShift(NULL,PERIOD_CURRENT,Starttime,false); // стартовый номер для расчёта всех баров //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { //--- indicator digits IndicatorSetInteger(INDICATOR_DIGITS,0); //--- indicator short name IndicatorSetString(INDICATOR_SHORTNAME,"A/D"); //---- index buffer SetIndexBuffer(0,ExtADbuffer); ArrayInitialize(ExtADbuffer,0.0); SetIndexBuffer(1,Buffer1,INDICATOR_COLOR_INDEX); SetIndexBuffer(6,Buffer2,INDICATOR_DATA); SetIndexBuffer(5,ExtMainBuffer,INDICATOR_DATA); SetIndexBuffer(2,ExtSignalBuffer,INDICATOR_DATA); SetIndexBuffer(3,ExtHighesBuffer,INDICATOR_DATA); SetIndexBuffer(4,ExtLowesBuffer,INDICATOR_DATA); // ArraySetAsSeries(Buffer2, true); ArraySetAsSeries(ExtMainBuffer, true); ArraySetAsSeries(ExtSignalBuffer, true); ArraySetAsSeries(ExtHighesBuffer, true); ArraySetAsSeries(ExtLowesBuffer, true); ArrayInitialize(ExtMainBuffer,0); //--- set index draw begin // PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,1); //---- OnInit done } //+------------------------------------------------------------------+ //| Accumulation/Distribution | //+------------------------------------------------------------------+ int OnCalculate(const int rates_total, const int prev_calculated, const datetime &time[], const double &open[], const double &high[], const double &low[], const double &close[], const long &tick_volume[], const long &volume[], const int &spread[]) { //--- check for bars count if(rates_total<2) return(0); //exit with zero result //--- get current position int pos=rates_total-BRS-1; if(pos<0) pos=rates_total-BRS-1; // Print(rates_total," ",BRS," ",pos); //--- calculate with appropriate volumes Calculate(rates_total,pos,high,low,close,volume); if(prev_calculated>rates_total || prev_calculated<=0)// проверка на первый старт расчёта индикатора { limit=iBarShift(NULL,PERIOD_CURRENT,Starttime,false); // стартовый номер для расчёта всех баров }else{ limit=rates_total-prev_calculated; // стартовый номер для расчёта новых баров } if(m!=rates_total) { m=rates_total; int i,k; ArrayReverse(ExtADbuffer); for(i=limit-InpKPeriod-1;i>=0 && !IsStopped();i--) { double dmin=100000000.0; double dmax=-100000000.0; for(k=i+InpKPeriod-1;k>=i;k--) { ArrayResize(ExtLowesBuffer,k+1); ArrayResize(ExtHighesBuffer,k+1); if(dmin>low[k]) dmin=ExtADbuffer[k]; if(dmax0)dmin=dmin*-1; if (dmax<0)dmax=dmax*-1; ExtLowesBuffer[i]=dmin; ExtHighesBuffer[i]=dmax; // ExtMainBuffer[i]=delta[i]; } for(i=limit-InpKPeriod-1;i>=0 && !IsStopped();i--) { double sumlow=0.0; double sumhigh=0.0; for(k=i+InpSlowing-1;k>=i;k--) { ArrayResize(ExtLowesBuffer,k+1); ArrayResize(ExtHighesBuffer,k+1); sumlow +=(0-ExtLowesBuffer[k]); sumhigh+=(ExtHighesBuffer[k]-ExtLowesBuffer[k]); } // Print (sumhigh," ",ExtHighesBuffer[i]," ",ExtLowesBuffer[i], " ",Buffer2[i]); ArrayResize(ExtMainBuffer,i+1); if(sumhigh==0.0) ExtMainBuffer[i]=100.0; if(sumhigh!=0.0) ExtMainBuffer[i]=sumlow/sumhigh*100; } ArrayReverse(ExtADbuffer); } return(rates_total); } //+------------------------------------------------------------------+ //| Calculating with selected volume | //+------------------------------------------------------------------+ void Calculate(const int rates_total,const int pos, const double &high[], const double &low[], const double &close[], const long &volume[]) { double hi,lo,cl; //--- main cycle for(int i=pos;i0) sum+=ExtADbuffer[i-1]; ExtADbuffer[i]=sum; } //---- } //+------------------------------------------------------------------+