//+------------------------------------------------------------------+
//|                                                    DASOBV_v1.mq4 |
//|                                                       Akif TOKUZ |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Akif TOKUZ"
#property link      ""

#property indicator_separate_window
#property indicator_buffers 1
#property indicator_color1 Yellow
#property indicator_width1  1 


//---- indicator buffers
double OBV[];

//---- buffers
double AvgPrice[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

   IndicatorBuffers(2);
   
   SetIndexStyle(0,DRAW_LINE);
   
   SetIndexBuffer(0,OBV);
   SetIndexBuffer(1,AvgPrice);   
    
   string short_name="MIDAS_OBV";
   IndicatorShortName(short_name);
   
   SetIndexLabel(0,"MIDAS_OBV");
   SetIndexLabel(1,"AvgPrice");
   
   
   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }


//+------------------------------------------------------------------+
//| Check if we reached the input date                               |
//+------------------------------------------------------------------+  
int   sign(double yesterday, double today)
{
   if (today>yesterday)   return(1);
   else if  (today<yesterday)   return(-1);
   else if (today==yesterday) return (0);
   return (0);
}


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
  int i;
  int counted_bars=IndicatorCounted();
  
  //---- check for possible errors
  if(counted_bars<0) return(-1);
   
   for (i=Bars-counted_bars-1; i>=0; i--)
   {
      AvgPrice[i]=(High[i]+Low[i])/2;
      if (i==Bars-counted_bars-1)
      {
         OBV[i]=Volume[i];
      }else
      {
         OBV[i]=OBV[i+1]+sign(AvgPrice[i+1],AvgPrice[i])*Volume[i];
      }
   }
    
   
  //---- done
  return(0);
   }
//+------------------------------------------------------------------+