Author: Copyright 2018, MetaQuotes Software Corp.
Price Data Components
0 Views
0 Downloads
0 Favorites
VWAP_v1
ÿþ//+------------------------------------------------------------------+

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

  {

//--- #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 8 @0AGQB :>;8G5AB20 ?@>AG8BK205<KE 10@>2

   if(rates_total<4) return 0;

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

   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);

     }



//--- >43>B>2:0 40==KE

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

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

   

//---  0AGQB 8=48:0B>@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<rates_total-1 && TimeDay(time[index])==DT)

     {

      sum1+=BufferWP[index];

      sum2+=(double)volume[index];

      index++;

     }

   return(sum2!=0 ? sum1/sum2 : EMPTY_VALUE);

  }

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

//|                                                                  |

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

double CalculateWeekly(const int rates_total,const int shift,const datetime &time[],const long &volume[])

  {

   int index=shift+1;

   double sum1=BufferWP[shift];

   double sum2=(double)volume[shift];

   while(index<rates_total-2 && TimeDayOfWeek(time[index+1])<=TimeDayOfWeek(time[index]))

     {

      sum1+=BufferWP[index];

      sum2+=(double)volume[index];

      index++;

     }

   return(sum2!=0 ? sum1/sum2 : EMPTY_VALUE);

  }

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

//|                                                                  |

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

double CalculateMonthly(const int rates_total,const int shift,const datetime &time[],const long &volume[])

  {

   int index=shift;

   double sum1=0;

   double sum2=0;

   int DT=TimeMonth(time[shift]);

   while(index<rates_total-1 && TimeMonth(time[index])==DT)

     {

      sum1+=BufferWP[index];

      sum2+=(double)volume[index];

      index++;

     }

   return(sum2!=0 ? sum1/sum2 : EMPTY_VALUE);

  }

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

//| >72@0I05B 45=L <5AOF0 C:070==>9 40BK                            |

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

int TimeDay(const datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm)) return WRONG_VALUE;

   return tm.day;

  }

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

//| >72@0I05B 45=L =545;8 C:070==>9 40BK                            |

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

int TimeDayOfWeek(const datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm)) return WRONG_VALUE;

   return tm.day_of_week;

  }

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

//| >72@0I05B =><5@ <5AOF0 C:070==>3> 2@5<5=8                       |

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

int TimeMonth(const datetime time)

  {

   MqlDateTime tm;

   if(!TimeToStruct(time,tm)) return WRONG_VALUE;

   return tm.mon;

  }

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

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 ---