//|                     Exponential Weighted Volume (EWV) Histogram |



#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_style1 DRAW_HISTOGRAM
#property indicator_style2 DRAW_HISTOGRAM
#property indicator_width1 2
#property indicator_width2 2

//--- Declare indicator buffers
double EWVBufferBuy[];    // Buying volume
double EWVBufferSell[];   // Selling volume

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+
int OnInit()
{
   // Set up indicator buffers
   SetIndexBuffer(0, EWVBufferBuy);
   SetIndexBuffer(1, EWVBufferSell);

   SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexStyle(1, DRAW_HISTOGRAM);

   SetIndexLabel(0, "Buying Volume");
   SetIndexLabel(1, "Selling Volume");

   // Return initialization status
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   int limit = Bars - IndicatorCounted();

   for(int i = limit; i >= 0; i--)
   {
      // Calculate EWV logic
      if(i == 0) // First bar initialization
      {
         EWVBufferBuy[i] = Volume[i];
         EWVBufferSell[i] = 0;
      }
      else
      {
         // Weighted logic calculation using 0.5 smoothing
         double currentEWV = (0.5 * Volume[i]) + (0.5 * EWVBufferBuy[i + 1]);

         // Compare close prices to determine buying/selling pressure
         if(Close[i] > Close[i + 1])
         {
            EWVBufferBuy[i] = currentEWV;
            EWVBufferSell[i] = 0;
         }
         else
         {
            EWVBufferSell[i] = currentEWV;
            EWVBufferBuy[i] = 0;
         }
      }
   }

   return(0);
}
