// implemented by Daniel Fernandez, Asirikuy.com 2011

#property indicator_separate_window
#property indicator_minimum -20
#property indicator_maximum 120
#property indicator_buffers 3
#property indicator_color1 LightSeaGreen
#property indicator_color2 Crimson
#property indicator_color3 DarkGreen

extern int lookback_period = 45;
extern int power_period = 10 ;

//---- buffers
double power[];
double bearbuffer[];
double bullbuffer[];


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   string short_name;
//---- 2 additional buffers are used for counting.
   IndicatorBuffers(3);
//---- indicator lines
   SetIndexStyle(0,DRAW_LINE, 1, 2);
   SetIndexBuffer(0, power);
   SetIndexStyle(1,DRAW_LINE, 1, 2);
   SetIndexBuffer(1, bearbuffer);
   SetIndexStyle(2,DRAW_LINE, 1, 2);
   SetIndexBuffer(2, bullbuffer);
   SetLevelValue (0, 0);  
   SetLevelValue (1, 50);  
   SetLevelValue (2, 100);  

   return(0);
  }
//+------------------------------------------------------------------+
//| Net Power                                                      |
//+------------------------------------------------------------------+
int start()
  {
   int    i,k;
   
   int counted_bars=IndicatorCounted();
//----
   if(Bars<= power_period) return(0);
//---- initial zero
   if(counted_bars<1)
      for(i=1;i<=power_period;i++) power[Bars-i]=0.0;
//----
   i=Bars-counted_bars-1;
 
 while (i >= 0) 
 
 {
 
 Comment("Implemented by Daniel Fernandez, Asirikuy.com 2011");
 
   int bearcount = 0 ;
	int bullcount = 0 ;
	
	k = 0 ;
	
	while (k <= lookback_period - 1 ){
	
	double bear = iBearsPower(NULL,0,power_period, PRICE_CLOSE, k+i) ;
	double bull = iBullsPower(NULL,0,power_period, PRICE_CLOSE, k+i) ;
	
	if (bear < 0 )
	bearcount++ ;
	
	if (bull > 0 )
	bullcount++ ;
	
	k++ ;
	
	}
	
	power[i] = MathAbs(bullcount-bearcount)*100/lookback_period  ;
	bearbuffer[i] = bearcount*100/lookback_period  ;
	bullbuffer[i] = bullcount*100/lookback_period  ;
   
 i-- ;
 
 }
 
   return(0);
}
//+------------------------------------------------------------------+