//+------------------------------------------------------------------+
//| Simple Daily VWAP with Bands for MQL4                            |
//+------------------------------------------------------------------+
#property copyright "Grok Assisted"
#property link      ""
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_plots   3

// Plots (defaults, overridden by inputs)
#property indicator_label1  "VWAP"
#property indicator_type1   DRAW_LINE

#property indicator_label2  "VWAP + SD"
#property indicator_type2   DRAW_LINE

#property indicator_label3  "VWAP - SD"
#property indicator_type3   DRAW_LINE

// Inputs for customization
input color VWAPColor       = Blue;      // Color for VWAP line
input int   VWAPStyle       = STYLE_SOLID;  // Style for VWAP: 0=Solid, 1=Dash, 2=Dot, 3=DashDot, 4=DashDotDot
input int   VWAPWidth       = 2;         // Width for VWAP line (1-5)

input color UpperBandColor  = Green;     // Color for Upper Band
input int   UpperBandStyle  = STYLE_DASH;  // Style for Upper Band
input int   UpperBandWidth  = 1;         // Width for Upper Band

input color LowerBandColor  = Red;       // Color for Lower Band
input int   LowerBandStyle  = STYLE_DASH;  // Style for Lower Band
input int   LowerBandWidth  = 1;         // Width for Lower Band

input int   BandDeviations  = 1;         // Standard deviations for bands (0 to disable)
input bool  UseTickVolume   = true;      // Use tick volume (true) or real if available

// Buffers
double VWAPBuffer[];
double UpperBand[];
double LowerBand[];

// Global vars
double cumPV = 0.0;  // Cumulative Price * Volume
double cumV  = 0.0;  // Cumulative Volume
double sumSq = 0.0;  // For variance calc
datetime lastDay = 0;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
   SetIndexBuffer(0, VWAPBuffer);
   SetIndexBuffer(1, UpperBand);
   SetIndexBuffer(2, LowerBand);
   
   // Apply input styles
   SetIndexStyle(0, DRAW_LINE, VWAPStyle, VWAPWidth, VWAPColor);
   SetIndexStyle(1, DRAW_LINE, UpperBandStyle, UpperBandWidth, UpperBandColor);
   SetIndexStyle(2, DRAW_LINE, LowerBandStyle, LowerBandWidth, LowerBandColor);
   
   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 limit = rates_total - prev_calculated;
   if(prev_calculated == 0) limit = rates_total - 1;  // Full recalc on init

   for(int i = limit; i >= 0; i--)
     {
      // Reset on new day (using midnight check)
      datetime currentDay = time[i] - (time[i] % 86400);  // Midnight timestamp
      if(lastDay == 0 || currentDay > lastDay)
        {
         cumPV = 0.0;
         cumV = 0.0;
         sumSq = 0.0;
         lastDay = currentDay;
        }

      // Typical price (HLC/3)
      double typPrice = (high[i] + low[i] + close[i]) / 3.0;

      // Volume: tick or real
      double vol = UseTickVolume ? (double)tick_volume[i] : (double)volume[i];
      if(vol == 0) vol = 1;  // Avoid div by zero

      // Accumulate
      cumPV += typPrice * vol;
      cumV += vol;

      // VWAP
      VWAPBuffer[i] = cumPV / cumV;

      // For bands: Incremental variance
      if(BandDeviations > 0)
        {
         sumSq += MathPow(typPrice - VWAPBuffer[i], 2) * vol;
         double variance = sumSq / cumV;
         double sd = MathSqrt(variance) * BandDeviations;
         UpperBand[i] = VWAPBuffer[i] + sd;
         LowerBand[i] = VWAPBuffer[i] - sd;
        }
      else
        {
         UpperBand[i] = EMPTY_VALUE;
         LowerBand[i] = EMPTY_VALUE;
        }
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+
