//+------------------------------------------------------------------+ //| VWAP.mq5 | //| Copyright 2018, MetaQuotes Software Corp. | //| https://mql5.com | //+------------------------------------------------------------------+ #property copyright "Copyright 2018, MetaQuotes Software Corp." #property link "https://mql5.com" #property version "1.00" #property description "Advance Volume Weighted Average Price indicator" #property indicator_chart_window #property indicator_buffers 4 #property indicator_plots 3 //--- plot Daily #property indicator_label1 "Daily VWAP" #property indicator_type1 DRAW_LINE #property indicator_color1 clrDodgerBlue #property indicator_style1 STYLE_SOLID #property indicator_width1 1 //--- plot Weekly #property indicator_label2 "Weekly VWAP" #property indicator_type2 DRAW_LINE #property indicator_color2 clrRed #property indicator_style2 STYLE_SOLID #property indicator_width2 1 //--- plot Monthly #property indicator_label3 "Monthly VWAP" #property indicator_type3 DRAW_LINE #property indicator_color3 clrGreen #property indicator_style3 STYLE_SOLID #property indicator_width3 1 //--- enums enum ENUM_INPUT_YES_NO { INPUT_YES = 1, // Yes INPUT_NO = 0 // No }; //--- input parameters input ENUM_INPUT_YES_NO InpShowDaily = INPUT_YES; // Show daily VWAP input ENUM_INPUT_YES_NO InpShowWeekly = INPUT_YES; // Show weekly VWAP input ENUM_INPUT_YES_NO InpShowMonthly = INPUT_YES; // Show monthly VWAP //--- indicator buffers double BufferDaily[]; double BufferWeekly[]; double BufferMonthly[]; double BufferWP[]; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ int OnInit() { //--- indicator buffers mapping SetIndexBuffer(0,BufferDaily,INDICATOR_DATA); SetIndexBuffer(1,BufferWeekly,INDICATOR_DATA); SetIndexBuffer(2,BufferMonthly,INDICATOR_DATA); SetIndexBuffer(3,BufferWP,INDICATOR_CALCULATIONS); //--- setting indicator parameters IndicatorSetString(INDICATOR_SHORTNAME,"Advance VWAP"); IndicatorSetInteger(INDICATOR_DIGITS,Digits()); //--- setting buffer arrays as timeseries ArraySetAsSeries(BufferDaily,true); ArraySetAsSeries(BufferWeekly,true); ArraySetAsSeries(BufferMonthly,true); ArraySetAsSeries(BufferWP,true); //--- return(INIT_SUCCEEDED); } //+------------------------------------------------------------------+ //| Custom indicator iteration function | //+------------------------------------------------------------------+ 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[]) { //--- Установка массивов буферов как таймсерий ArraySetAsSeries(high,true); ArraySetAsSeries(low,true); ArraySetAsSeries(close,true); ArraySetAsSeries(time,true); ArraySetAsSeries(tick_volume,true); //--- Проверка и расчёт количества просчитываемых баров if(rates_total<4) return 0; //--- Проверка и расчёт количества просчитываемых баров int limit=rates_total-prev_calculated; if(limit>1) { limit=rates_total-1; ArrayInitialize(BufferDaily,EMPTY_VALUE); ArrayInitialize(BufferWeekly,EMPTY_VALUE); ArrayInitialize(BufferMonthly,EMPTY_VALUE); ArrayInitialize(BufferWP,0); } //--- Подготовка данных for(int i=limit; i>=0 && !IsStopped(); i--) BufferWP[i]=tick_volume[i]*(high[i]+low[i]+close[i])/3.0; //--- Расчёт индикатора for(int i=limit; i>=0 && !IsStopped(); i--) { BufferDaily[i]=(InpShowDaily ? CalculateDaily(rates_total,i,time,tick_volume) : EMPTY_VALUE); BufferWeekly[i]=(InpShowWeekly ? CalculateWeekly(rates_total,i,time,tick_volume) : EMPTY_VALUE); BufferMonthly[i]=(InpShowMonthly ? CalculateMonthly(rates_total,i,time,tick_volume) : EMPTY_VALUE); } //--- return value of prev_calculated for next call return(rates_total); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ double CalculateDaily(const int rates_total,const int shift,const datetime &time[],const long &volume[]) { int index=shift; double sum1=0; double sum2=0; int DT=TimeDay(time[shift]); while(index