//+-------------------------------------------------------------------+
//|                                        Buy_Sell_Volumes_v1.0.mq4  |
//|                                            Copyright © 2013, Pip  |
//|                                    http://www.pricemasterpro.com  |
//| This indicator is for personal use only.                          |
//| Please respect the intellectual rights of the original programmer |
//| and the work of others that contributed to this indicator         |
//| under no circumstances is this indicator to be sold for money or  |
//| to be repackged into a commercial package or to be distributed as |
//| original work by anyone else.                                     |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, Pip; admin@pricemasterpro.com"
#property link      "http://www.pricemasterpro.com/"
//---- indicator settings
#property  indicator_separate_window
#property indicator_minimum 0
#property indicator_buffers 6
#property indicator_color1  White
#property indicator_color2  Red
#property indicator_color3  Green
#property indicator_color4  White
#property indicator_color5  Red
#property indicator_color6  Green
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  1
#property indicator_width4  1
#property indicator_width5  1
#property indicator_width6  1
//---- indicator buffers

extern double Smooth       = 5;
extern double VolumeSmooth = 1;
double ExtVolumesBufferHisto[];
double ExtVolumesBufferLine[];
double ExtVolumesUpBufferHisto[];
double ExtVolumesUpBufferLine[];
double ExtVolumesDownBufferHisto[];
double ExtVolumesDownBufferLine[];
 
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtVolumesBufferHisto);       
   SetIndexBuffer(1,ExtVolumesUpBufferHisto);
   SetIndexBuffer(2,ExtVolumesDownBufferHisto);
   SetIndexBuffer(3,ExtVolumesBufferLine);
   SetIndexBuffer(4,ExtVolumesUpBufferLine);       
   SetIndexBuffer(5,ExtVolumesDownBufferLine);
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   SetIndexStyle(3,DRAW_LINE);
   SetIndexStyle(4,DRAW_LINE);
   SetIndexStyle(5,DRAW_LINE);
//---- sets default precision format for indicators visualization
   IndicatorDigits(0);   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Buy_Sell Volume Pressure");
   SetIndexLabel(0,"Total Volume");      
   SetIndexLabel(1,"Buy Volume");
   SetIndexLabel(2,"Sell Volume");
//---- sets drawing line empty value
   SetIndexEmptyValue(1,0.0);
   SetIndexEmptyValue(3,0.0); 
   SetIndexEmptyValue(5,0.0);      
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Volumes                                                          |
//+------------------------------------------------------------------+
int start()
  {
   int    i,nLimit,nCountedBars;
//---- bars count that does not changed after last indicator launch.
   nCountedBars=IndicatorCounted();
//---- last counted bar will be recounted
   if(nCountedBars>0) nCountedBars--;
   nLimit=Bars-nCountedBars;
//----
   for(i=0; i<nLimit; i++)
     {
    double bullp   = MathAbs(iBullsPower(NULL, 0, 1, PRICE_CLOSE, i));
    double bearp   = MathAbs(iBearsPower(NULL, 0, 1, PRICE_CLOSE, i));
    double vol     = iVolume(Symbol(), 0, i);
    double Buyers  = iTema((bullp * vol) / MathMax((bullp + bearp),0.000001),Smooth,i,0);
    double Sellers = iTema((bearp * vol) / MathMax((bullp + bearp),0.000001),Smooth,i,1);
   
         ExtVolumesBufferHisto[i]=iTema(vol,VolumeSmooth,i,2);
         ExtVolumesBufferLine[i]=ExtVolumesBufferHisto[i];
         ExtVolumesUpBufferHisto[i]=Buyers;
         ExtVolumesUpBufferLine[i]=Buyers;
         ExtVolumesDownBufferHisto[i]=Sellers; 
         ExtVolumesDownBufferLine[i]=Sellers;       
     
     }        
//---- done
   return(0);
  }
//+------------------------------------------------------------------+

double workTema[][9];
#define _ema1 0
#define _ema2 1
#define _ema3 2

double iTema(double price, double period, int r, int instanceNo=0)
{
   if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3; r=Bars-r-1;

   //
   //
   //
   //
   //
      
   double alpha = 2.0 / (1.0+period);
          workTema[r][_ema1+instanceNo] = workTema[r-1][_ema1+instanceNo]+alpha*(price                        -workTema[r-1][_ema1+instanceNo]);
          workTema[r][_ema2+instanceNo] = workTema[r-1][_ema2+instanceNo]+alpha*(workTema[r][_ema1+instanceNo]-workTema[r-1][_ema2+instanceNo]);
          workTema[r][_ema3+instanceNo] = workTema[r-1][_ema3+instanceNo]+alpha*(workTema[r][_ema2+instanceNo]-workTema[r-1][_ema3+instanceNo]);
   return(workTema[r][_ema3+instanceNo]+3.0*(workTema[r][_ema1+instanceNo]-workTema[r][_ema2+instanceNo]));
}

