//+------------------------------------------------------------------+
//|                                                   VWAP_Close.mq4 |
//|                                                              STS |
//|                                                                  |
//+------------------------------------------------------------------+

#property link      ""

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Indigo
#property indicator_width1 3
#property indicator_width2 3
//---- input parameters
extern int period=12;
extern int shift=0;
extern bool label_name=false;
extern bool label_price=false;
extern string label_gap = "----- ";
extern int label_font_size=10;
extern color label_color=clrIndigo;
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexShift(0,shift);
   if(label_name||label_price){TextCreate(NULL,"vwap label",0,0,0,"Waiting","Arial",label_font_size,label_color,0,ANCHOR_LEFT);}
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete(NULL,"vwap label");
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int i,bar;
   bar = Bars-counted_bars;
   
   for (i=0;i<=bar;i++){
      double sum1,sum2;
      int ntmp;
      sum1=0;
      sum2=0;
      for (ntmp=0;ntmp<=period;ntmp++){
         sum1=sum1+Close[i+ntmp]*Volume[i+ntmp];
         sum2=sum2+Volume[i+ntmp];
      }
      
      if(sum2 > 0){
       ExtMapBuffer1[i]=sum1/sum2;
      string a = IntegerToString(period)+" VWAP ";
      string b = DoubleToString(ExtMapBuffer1[0],Digits());
      string c = "";
      if(label_name&&!label_price){c=a;}
      if(label_name&&label_price){c=a+b;}
      if(!label_name&&label_price){c=b;}
      if(label_name||label_price){      
            ObjectSetInteger(NULL,"vwap label",OBJPROP_TIME,Time[0]);
            ObjectSetDouble(NULL,"vwap label",OBJPROP_PRICE,ExtMapBuffer1[0]);
            ObjectSetString(NULL,"vwap label",OBJPROP_TEXT,label_gap+c);}
      }
      
   
   }
   

//----
   return(0);
  }
//+------------------------------------------------------------------+
bool TextCreate(long              chart_ID=0,               // chart's ID
                string            name="Text",              // object name
                int               sub_window=0,             // subwindow index
                datetime          time=0,                   // anchor point time
                double            price=0,                  // anchor point price
                string            text="Text",              // the text itself
                string            font="Arial",             // font
                int               font_size=10,             // font size
                color             clr=clrRed,               // color
                double            angle=0.0,                // text slope
                ENUM_ANCHOR_POINT anchor=ANCHOR_LEFT_UPPER, // anchor type
                bool              back=false,               // in the background
                bool              selectable=false,          //object can be selected  
                bool              selection=false,          // higmaxight to move
                bool              hidden=false,             // hidden in the object list
                long              z_order=0)                // priority for mouse click
{
      ResetLastError();
      ObjectCreate(chart_ID, name, OBJ_TEXT,sub_window, time, price);
      ObjectSetText(name, text,font_size,font,clr);
      ObjectSet(name, OBJPROP_ANGLE, angle);
      ObjectSet(name, OBJPROP_ANCHOR, anchor);
      ObjectSet(name, OBJPROP_BACK, back);
      ObjectSet(name, OBJPROP_SELECTABLE, selectable);
      ObjectSet(name, OBJPROP_SELECTED,selection);
      ObjectSet(name, OBJPROP_HIDDEN, hidden);
      ObjectSet(name, OBJPROP_ZORDER, z_order);
      return(true);    
}