#property copyright "www.forex-station.com"
#property link      "www.forex-station.com"

#property indicator_separate_window 
#property indicator_buffers 2
#property indicator_plots   1
#property indicator_label1  "Obv slope"
#property indicator_type1   DRAW_COLOR_HISTOGRAM
#property indicator_color1  clrDimGray,clrOrange,clrLime
#property indicator_width1  3
#property indicator_minimum 0
#property indicator_maximum 1

//
//
//

input ENUM_APPLIED_VOLUME inpVolumeType = VOLUME_TICK; // Volumes

double val[],valc[];
struct sGloStruct { double _vol; }; sGloStruct glo;

//------------------------------------------------------------------------------------------------
//
//------------------------------------------------------------------------------------------------

int OnInit()
{
   SetIndexBuffer(0,val  ,INDICATOR_DATA);
   SetIndexBuffer(1,valc ,INDICATOR_COLOR_INDEX);
  
   IndicatorSetString(INDICATOR_SHORTNAME,"OBV");            
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 &volume[],
                const int &spread[])
{   
   int _limit = (prev_calculated>0) ? prev_calculated-1 : 0;

   //
   //
   //
   
   struct sWorkStruct { double obv; };   
   static sWorkStruct wrk[];
   static int         wrkSize = -1;
                  if (wrkSize<=rates_total) wrkSize = ArrayResize(wrk,rates_total+500,2000);

   
   //
   //
   //

   for (int i=_limit; i<rates_total && !_StopFlag; i++)
   {
      glo._vol   = (inpVolumeType==VOLUME_TICK) ? (double)tick_volume[i] : volume[i];
      wrk[i].obv = (i>0) ? (close[i]>close[i-1]) ? wrk[i-1].obv+glo._vol :  (close[i]<close[i-1]) ? wrk[i-1].obv-glo._vol : wrk[i-1].obv : 0;
      val[i]     = 1;
      valc[i]    = (i>0) ? (wrk[i].obv>wrk[i-1].obv) ? 1 : (wrk[i].obv<wrk[i-1].obv) ? 2 : valc[i-1] : 0;
   }
return(rates_total);
}