VWAP_TimeFrame

Author: Copyright 2019, MetaQuotes Software Corp.
Price Data Components
Miscellaneous
It issuies visual alerts to the screen
0 Views
0 Downloads
0 Favorites
VWAP_TimeFrame
ÿþ//+------------------------------------------------------------------+

//|                                               VWAP_TimeFrame.mq5 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2019, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property indicator_chart_window

#property indicator_buffers 5

#property indicator_plots   2

//--- plot Pvol

#property indicator_label1  "Pvol"

#property indicator_type1   DRAW_COLOR_LINE

#property indicator_color1  clrSnow,clrGreen,clrYellow,C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0',C'0,0,0'

#property indicator_style1  STYLE_SOLID

#property indicator_width1  1

//--- plot Settle

#property indicator_label2  "Settle"

#property indicator_type2   DRAW_LINE

#property indicator_color2  clrAqua

#property indicator_style2  STYLE_DASHDOT

#property indicator_width2  1

#define _setPrice(_priceType,_where,_index) { \

   switch(_priceType) \

   { \

      case PRICE_CLOSE:    _where = close[_index];                                              break; \

      case PRICE_OPEN:     _where = open[_index];                                               break; \

      case PRICE_HIGH:     _where = high[_index];                                               break; \

      case PRICE_LOW:      _where = low[_index];                                                break; \

      case PRICE_MEDIAN:   _where = (high[_index]+low[_index])/2.0;                             break; \

      case PRICE_TYPICAL:  _where = (high[_index]+low[_index]+close[_index])/3.0;               break; \

      case PRICE_WEIGHTED: _where = (high[_index]+low[_index]+close[_index]+close[_index])/4.0; break; \

      default : _where=0.0; \

  } \

}

input ENUM_TIMEFRAMES TimePeriod = PERIOD_W1;

input ENUM_APPLIED_PRICE Applied = PRICE_TYPICAL;

//--- indicator buffers

double  ExtPvolBuffer[];

double  ExtPvolColors[];

double  ExtSettleBuffer[];

double  ExtVolumeSumBuffer[];

double  ExtBarBuffer[];

double  ExtPeriodLimit = 1.0;

datetime _times[];

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

//| Custom indicator initialization function                         |

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

int OnInit()

  {

//--- check for input value

   if(_Period >= TimePeriod)

     {

      Alert("Incorrect input parameter TimePeriod =" + EnumToString(TimePeriod));

      return(INIT_PARAMETERS_INCORRECT);

     }

//--- indicator name

   IndicatorSetString(INDICATOR_SHORTNAME,"VWAP_TimeFrame(" + EnumToString(TimePeriod) + "," + EnumToString(_Period) + ")");

//--- indicator buffers mapping

   SetIndexBuffer(0,ExtPvolBuffer,INDICATOR_DATA);

   SetIndexBuffer(1,ExtPvolColors,INDICATOR_COLOR_INDEX);

   SetIndexBuffer(2,ExtSettleBuffer,INDICATOR_DATA);

   SetIndexBuffer(3,ExtVolumeSumBuffer,INDICATOR_CALCULATIONS);

   SetIndexBuffer(4,ExtBarBuffer,INDICATOR_CALCULATIONS);

   IndicatorSetInteger(INDICATOR_DIGITS,_Digits);

//---

   ExtPeriodLimit = ceil((double)PeriodSeconds(TimePeriod) / (double)PeriodSeconds(_Period));

   ResetLastError();

   int  failed = 0;

   while(!SeriesInfoInteger(_Symbol,TimePeriod,SERIES_SYNCHRONIZED) && !IsStopped())

     {

      if(++failed >= 1000) //--- no more than 1000 failed attempts

        {

         Print("No Synchronized:",GetLastError());

         break;

        }

      Sleep(10);

     }

   failed = 0;

   while(CopyTime(_Symbol,TimePeriod,0,Bars(_Symbol,TimePeriod),_times)<=0 && !IsStopped())

     {

      if(++failed >= 1000) //--- no more than 1000 failed attempts

        {

         Print("No History uploaded:",GetLastError());//"History is still being uploaded!"

         break;

        }

      Sleep(10);

     }

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

  {

   int pos = prev_calculated > 0 ? (prev_calculated - 1) : 0;

   int copied = GetTimes(_times,time[pos],_Symbol,TimePeriod);

   if(copied * ExtPeriodLimit < rates_total - pos)//total*1.158,0+1,1+1

      return(0);

   if(copied > 2)// Update to new days

      Print("Getting Time Count:",copied,"/",rates_total - pos,",Time:",_times[0],"/",time[pos]);

//---

   if(rates_total > prev_calculated) // Update to new bars

     {

      if(IS_DEBUG_MODE || IS_PROFILE_MODE)

         Print("Getting New Time:",_times[copied - 1],"/",time[rates_total - 1]);

      if(time[rates_total - 1] > _times[copied - 1] + PeriodSeconds(TimePeriod))

         return(0);

     }

//---

   for(int i = 0; i <= copied; i++)

     {

      int shift = pos;

      for(; pos < rates_total && (pos - shift) <= ExtPeriodLimit && !_StopFlag; pos++)

        {

         double price=EMPTY_VALUE;

         _setPrice(Applied,price,pos);

         if(i < copied)

           {

            if(time[pos] >= _times[i])

               break;

           }

         else

            shift = pos - Bars(_Symbol,_Period,_times[i - 1],time[pos]) + 1; //i==count





         ExtBarBuffer[pos] = pos - shift; //ExtBarBuffer[pos-1]+1

         if(ExtBarBuffer[pos] > 0)

           {

            double amount = ExtPvolBuffer[pos - 1] * ExtVolumeSumBuffer[pos - 1] + price * tick_volume[pos];

            ExtVolumeSumBuffer[pos] = ExtVolumeSumBuffer[pos - 1] + tick_volume[pos];

            ExtPvolBuffer[pos] = (ExtVolumeSumBuffer[pos] > 0) ? amount / ExtVolumeSumBuffer[pos] : ExtPvolBuffer[pos - 1];

            ExtPvolColors[pos] = (price < ExtPvolBuffer[pos] && ExtPvolBuffer[pos] <= ExtPvolBuffer[pos - 1]) ? 1 :

                                 (price > ExtPvolBuffer[pos] && ExtPvolBuffer[pos] >= ExtPvolBuffer[pos - 1]) ? 2 : 0;

           }

         else

           {

            ExtVolumeSumBuffer[pos] = (double)tick_volume[pos];

            ExtPvolBuffer[pos] = price;

            ExtPvolColors[pos] = 0;

           }

         ExtSettleBuffer[pos] = (ExtBarBuffer[pos]>1) ? ExtSettleBuffer[pos-1]:(shift > 0)? ExtPvolBuffer[shift - 1] : price;

        }

     }



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

   return(rates_total);

  }

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

//|  As Series False(start-now)                                      |

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

int GetTimes(datetime &times[],datetime start,string symbol = NULL,ENUM_TIMEFRAMES timeframe = PERIOD_D1)

  {

   int count = -1;

   long bars = 0;



   if((bars = Bars(symbol,timeframe,start - PeriodSeconds(timeframe),(datetime)UINT_MAX))> 0)

      if((count = CopyTime(symbol,timeframe,0,(int)bars,times))== bars)

         return count;



   if((count = CopyTime(symbol,timeframe,start - PeriodSeconds(timeframe),(datetime)UINT_MAX,times))> 0)

      return count;



   ResetLastError();

   if(SeriesInfoInteger(symbol,timeframe,SERIES_BARS_COUNT,bars))

      if((count = CopyTime(symbol,timeframe,0,(int)bars,times)) == bars)

         return count;



   int code = GetLastError();

   if(code != 4401)

      printf("Getting Time is failed! Error:%d Bars:%d",code,bars);//—Bl†SòS*g~b0R

   return -1;

  }

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



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

Comments