//+------------------------------------------------------------------+
//|                                                 VolumeRSI_v1.mq4 |
//|                           Copyright © 2007, TrendLaboratory Ltd. |
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |
//|                                   E-mail: igorad2003@yahoo.co.uk |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, TrendLaboratory Ltd."
#property link      "http://finance.groups.yahoo.com/group/TrendLaboratory"

#property indicator_separate_window
#property indicator_buffers   1
#property indicator_color1    LightBlue
#property indicator_width1    2 
#property indicator_level1    50
//---- input parameters
extern int       Price     =  0; //Applied Price(0-Close;1-Open;2-High;3-Low;4-Median;5-Typical;6-Weighted)
extern int       Length    = 14; // Period of evaluation
extern int       Smooth    =  3; // Period of smoothing
extern int       MA_Mode   =  2; // Mode of MA
//---- buffers
double SmRSI[];
double vRSI[];
double vBulls[];
double vBears[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,SmRSI);
   SetIndexBuffer(1,vRSI);
   SetIndexBuffer(2,vBulls);
   SetIndexBuffer(3,vBears);
   
//---- name for DataWindow and indicator subwindow label
   string short_name="VolumeRSI("+Price+","+Length+","+Smooth+","+MA_Mode+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"VolumeRSI");

//----
   SetIndexDrawBegin(0,Length+Smooth);
     
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int      shift, limit, counted_bars=IndicatorCounted();
   double   Price1, Price2, Bulls, Bears, AvgBulls, AvgBears;
//---- 
   if ( counted_bars < 0 ) return(-1);
   if ( counted_bars ==0 ) limit=Bars-1;
   
   if ( counted_bars < 1 ) 
   for(int i=1;i<Length+Smooth-1;i++) 
   {vRSI[Bars-i]=0; SmRSI[Bars-i]=0; vBulls[Bars-i]=0; vBears[Bars-i]=0;}     
      
   if(counted_bars>0) limit=Bars-counted_bars;
   limit--;
   
   for( shift=limit; shift>=0; shift--)
   {
   Price1 = iMA(NULL,0,1,0,0,Price,shift);
   Price2 = iMA(NULL,0,1,0,0,Price,shift+1); 
      
   Bulls = 0.5*(MathAbs(Price1-Price2)+(Price1-Price2));
   Bears = 0.5*(MathAbs(Price1-Price2)-(Price1-Price2));
  
   if(Bulls > 0) {vBulls[shift] = Volume[shift]; vBears[shift] = 0;}
   else
   if(Bears > 0) {vBears[shift] = Volume[shift]; vBulls[shift] = 0;}
   else
   if(Bears == Bulls) {vBears[shift]=0; vBulls[shift] = 0;}    
   }
      
   for( shift=limit; shift>=0; shift--)
   {
   AvgBulls=iMAOnArray(vBulls,0,Length,0,MA_Mode,shift);
   AvgBears=iMAOnArray(vBears,0,Length,0,MA_Mode,shift);
   
   if (AvgBulls+AvgBears != 0) vRSI[shift] = 1.0/(1.0+AvgBears/AvgBulls);
   else if (AvgBulls+AvgBears==0) vRSI[shift] = 0;        
   }
   for( shift=limit; shift>=0; shift--)
   SmRSI[shift] = 100*iMAOnArray(vRSI,0,Smooth,0,MA_Mode,shift);
//----
   return(0);
  }
//+------------------------------------------------------------------+