//+------------------------------------------------------------------+
//|                                             BullsBearsVolume.mq5 |
//|                                                           Shovel |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Shovel"
#property link      "http://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1  clrGreen
#property indicator_width1  2 
#property indicator_color2  clrRed
#property indicator_width2  2 
#property strict


//--- input parameters
input int                  InpPeriod     = 13;               // Period
input bool                 UseRealVolume = false;            // Use real volume?
input double               delta         = 0.00001;          // delta (flat level)

double    ExtBullsBearsBuffer[],hup[],hdn[],valc[],ExtVolumesBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0,hup,INDICATOR_DATA); SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(1,hdn,INDICATOR_DATA); SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(2,ExtBullsBearsBuffer);
   SetIndexBuffer(3,valc);
   SetIndexBuffer(4,ExtVolumesBuffer);

   IndicatorSetString(INDICATOR_SHORTNAME,"BullsBearsVolume("+(string)InpPeriod+"  delta: "+(string)delta+") ");
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&     real_volume[],
                 const int&      spread[])

{
   double point=SymbolInfoDouble(_Symbol,SYMBOL_POINT);
   int i=rates_total-prev_calculated+1; if (i>=rates_total) i=rates_total-1; 
   for (; i>=0 && !_StopFlag; i--)
   {
      double _bull = high[i]-iMA(NULL,0,InpPeriod,0,MODE_EMA,PRICE_CLOSE,i);
      double _bear =  low[i]-iMA(NULL,0,InpPeriod,0,MODE_EMA,PRICE_CLOSE,i);

      //--- fill indicators buffer
      if(_bull>0 && _bear>0)      ExtBullsBearsBuffer[i] = _bull-_bear;
      else if(_bull<0 && _bear<0) ExtBullsBearsBuffer[i] = -(fabs(_bear)-fabs(_bull));
      else                        ExtBullsBearsBuffer[i] = _bull+_bear;

      double volume = (UseRealVolume) ? (double)real_volume[i] : (double)tick_volume[i];

      //--- add volume to indicators buffer
      ExtBullsBearsBuffer[i]=ExtBullsBearsBuffer[i]*(double)volume*point;

      //--- remove noise 
      if(fabs(ExtBullsBearsBuffer[i])<delta) ExtBullsBearsBuffer[i]=0.0;

      //--- fill indicators color buffer
      hup[i] = EMPTY_VALUE;
      hdn[i] = EMPTY_VALUE;
      if(fabs(_bull)>fabs(_bear)) hup[i] = ExtBullsBearsBuffer[i];  // set color Green
      else                        hdn[i] = ExtBullsBearsBuffer[i];  // set color Red
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
