Any_Time_Frame_VWAP

Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
Series array that contains open time of each bar
0 Views
0 Downloads
0 Favorites
Any_Time_Frame_VWAP
ÿþ//+------------------------------------------------------------------+

//|                                          Any_Time_Frame_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 "Any timeframe Volume Weighted Average Price indicator"

#property indicator_chart_window

#property indicator_buffers 2

#property indicator_plots   1

//--- plot VWAP

#property indicator_label1  "VWAP"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrDodgerBlue

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- input parameters

input ENUM_TIMEFRAMES   InpTimeframe   =  PERIOD_D1;  // Timeframe

//--- indicator buffers

double         BufferVWAP[];

double         BufferWP[];

//--- global variables

ENUM_TIMEFRAMES   timeframe_h;

//+------------------------------------------------------------------+

//| Custom indicator initialization function                         |

//+------------------------------------------------------------------+

int OnInit()

  {

//--- timer

   EventSetTimer(90);

//--- set global variables

   timeframe_h=(InpTimeframe>Period() ? InpTimeframe : Period());

//--- indicator buffers mapping

   SetIndexBuffer(0,BufferVWAP,INDICATOR_DATA);

   SetIndexBuffer(1,BufferWP,INDICATOR_CALCULATIONS);

//--- setting plot buffer parameters

   PlotIndexSetString(0,PLOT_LABEL,TimeframeToString(timeframe_h)+" VWAP");

//--- setting buffer arrays as timeseries

   ArraySetAsSeries(BufferVWAP,true);

   ArraySetAsSeries(BufferWP,true);

//--- setting indicator parameters

   IndicatorSetString(INDICATOR_SHORTNAME,"Any timeframe VWAP");

   IndicatorSetInteger(INDICATOR_DIGITS,Digits());

//--- Get time

   Time(NULL,timeframe_h,1);

//---

   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[])

  {

//--- #AB0=>2:0 <0AA82>2 1CD5@>2 :0: B09<A5@89

   ArraySetAsSeries(high,true);

   ArraySetAsSeries(low,true);

   ArraySetAsSeries(close,true);

   ArraySetAsSeries(time,true);

   ArraySetAsSeries(tick_volume,true);

//--- @>25@:0 :>;8G5AB20 4>ABC?=KE 10@>2

   if(rates_total<4 || Time(NULL,timeframe_h,1)==0) return 0;

//--- @>25@:0 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   int limit=rates_total-prev_calculated;

   if(limit>1)

     {

      limit=rates_total-2;

      ArrayInitialize(BufferVWAP,EMPTY_VALUE);

      ArrayInitialize(BufferWP,0);

     }



//---  0AGQB 8=48:0B>@0

   for(int i=limit; i>=0 && !IsStopped(); i--)

     {

      BufferWP[i]=tick_volume[i]*(high[i]+low[i]+close[i])/3;



      int index=iBarShift(NULL,timeframe_h,time[i]);

      if(index==WRONG_VALUE)

         continue;

      datetime time_from=Time(NULL,timeframe_h,index);

      if(time_from==0)

         continue;

      BufferVWAP[i]=Calculate(rates_total,time_from,i,time,tick_volume);

     }

   

//--- return value of prev_calculated for next call

   return(rates_total);

  }

//+------------------------------------------------------------------+

//| Custom indicator timer function                                  |

//+------------------------------------------------------------------+

void OnTimer()

  {

   if(timeframe_h==Period())

      return;

   Time(NULL,timeframe_h,1);

  }  

//+------------------------------------------------------------------+

//|                                                                  |

//+------------------------------------------------------------------+

double Calculate(const int rates_total,const datetime from,const int to,const datetime &time[],const long &volume[])

  {

   double wp_summ=0;

   double volume_summ=0;

   int i=to;

   while(i<rates_total && time[i]>=from)

     {

      wp_summ+=BufferWP[i];

      volume_summ+=(double)volume[i];

      i++;

     }

   return(volume_summ!=0 ? wp_summ/volume_summ : 0);

  }

//+------------------------------------------------------------------+

//| >72@0I05B Time                                                  |

//+------------------------------------------------------------------+

datetime Time(const string symbol_name,const ENUM_TIMEFRAMES timeframe,const int shift)

  {

   datetime array[];

   return(CopyTime(symbol_name,timeframe,shift,1,array)==1 ? array[0] : 0);

  }

//+------------------------------------------------------------------+

//| Timeframe to string                                              |

//+------------------------------------------------------------------+

string TimeframeToString(const ENUM_TIMEFRAMES timeframe)

  {

   return StringSubstr(EnumToString(timeframe),7);

  }

//+------------------------------------------------------------------+

Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---