Author: Copyright 2020, MetaQuotes Software Corp.
0 Views
0 Downloads
0 Favorites
VWAP_v2
ÿþ//+------------------------------------------------------------------+

//|                                                         VWAP.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 description "Volume Weighted Average Price"

#property indicator_chart_window

#property indicator_buffers 1

#property indicator_plots   1

#property indicator_label1  "VWAP"

#property indicator_type1   DRAW_LINE

#property indicator_color1  clrAqua

#property indicator_style1  STYLE_SOLID

#property indicator_width1  2



input ENUM_APPLIED_PRICE   InpAppliedPrice   =  PRICE_TYPICAL; // Tipo de preço

input ENUM_APPLIED_VOLUME  InpVolumeType     =  VOLUME_REAL;   // Tipo de volume



double   VWAPBuffer[];

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

//| TODO: Create a better way for check if it's a new day            |

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

datetime CreateDateTime(datetime time = D'2000.01.01 00:00:00') {

   datetime    newTime;

   MqlDateTime tstruct;



   TimeToStruct(time, tstruct);

   tstruct.hour = 0;

   tstruct.min  = 0;

   tstruct.sec  = 0;

   newTime = (StructToTime(tstruct));



   return newTime;

}

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

//|                                                                  |

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

int OnInit() {

   SetIndexBuffer(0, VWAPBuffer, INDICATOR_DATA);

   return(INIT_SUCCEEDED);

}

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

//|                                                                  |

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

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



   double      calculatedPrice = 0,

               priceWeight = 0,

               volTotal = 0;

   int         i,

               dailyIdx = 0;

   datetime    auxTime = CreateDateTime();



   if(prev_calculated < 1) i = 0;

   else i = prev_calculated - 1;



   while(i < rates_total && !IsStopped()) {

      // The purpose of this IF is to check if the day has changed

      // TODO:

      // Create a better way for this

      if(CreateDateTime(time[i]) != auxTime) {

         dailyIdx = i;

         priceWeight = 0;

         volTotal = 0;

      }

      auxTime = CreateDateTime(time[i]);



      switch(InpAppliedPrice) {

      case PRICE_CLOSE:

         calculatedPrice = close[i];

         break;

      case PRICE_OPEN:

         calculatedPrice = open[i];

         break;

      case PRICE_HIGH:

         calculatedPrice = high[i];

         break;

      case PRICE_LOW:

         calculatedPrice = low[i];

         break;

      case PRICE_MEDIAN:

         calculatedPrice = (high[i] + low[i]) / 2;

         break;

      case PRICE_WEIGHTED:

         calculatedPrice = (high[i] + low[i] + close[i] + close[i]) / 4;

         break;

      default:

         calculatedPrice = (high[i] + low[i] + close[i]) / 3;

         break;

      }



      if(InpVolumeType == VOLUME_REAL) {

         priceWeight += (calculatedPrice * volume[i]);

         volTotal += (double)volume[i];

      } else {

         priceWeight += (calculatedPrice * tick_volume[i]);

         volTotal += (double)tick_volume[i];

      }



      if(volTotal) VWAPBuffer[i] = (priceWeight / volTotal);



      i++;

   }

   return(rates_total);

}

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

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