//+------------------------------------------------------------------+
//|                                               BullAndBear_v1.mq4 |
//|                                  Copyright © 2006, Forex-TSD.com |
//|                         Written by IgorAD,igorad2003@yahoo.co.uk |   
//|            http://finance.groups.yahoo.com/group/TrendLaboratory |                                      
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 2

#property indicator_color1 SkyBlue
#property indicator_width1 2
#property indicator_color2 Tomato
#property indicator_width2 0
#property indicator_style2 2

//---- input parameters
extern int     Length         = 20;  // Period of BullAndBear
extern int     Smooth         = 5;   // Period of Smoothing MA  
extern int     Signal         = 5;   // Period of Signal MA 
//---- indicator buffers
double BullnBear[];
double Sig[];
double Bulls[];
double Bears[];
double AvgBulls[];
double AvgBears[];
double Diff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
  int init()
  {
   IndicatorBuffers(7);
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0,BullnBear);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,Sig);
   SetIndexBuffer(2,Bulls);
   SetIndexBuffer(3,Bears);
   SetIndexBuffer(4,AvgBulls);
   SetIndexBuffer(5,AvgBears);
   SetIndexBuffer(6,Diff); 
   string short_name;
//---- indicator line
   
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));
//---- name for DataWindow and indicator subwindow label
   short_name="BullAndBear("+Length+","+Smooth+","+Signal+")";
   IndicatorShortName(short_name);
   SetIndexLabel(0,"BullAndBear");
   SetIndexLabel(1,"Signal");
//----
    
   SetIndexDrawBegin(0,3*(Length+Smooth+Signal));
   SetIndexDrawBegin(1,3*(Length+Smooth+Signal));
//----
 
   return(0);
  }

//+------------------------------------------------------------------+
//| BullAndBear_v1                                                   |
//+------------------------------------------------------------------+
int start()
{
   int    i,shift, counted_bars=IndicatorCounted(),limit;
   double price;      
   if ( counted_bars > 0 )  limit=Bars-counted_bars;
   if ( counted_bars < 0 )  return(0);
   if ( counted_bars ==0 )  limit=Bars-1; 
   if ( counted_bars < 1 ) 
   
   for(i=1;i<Length+Smooth+Signal;i++) 
   {
   BullnBear[Bars-i]=(Close[Bars-i]-Open[Bars-i])/Point;    
   Sig[Bars-i]=(Close[Bars-i]-Open[Bars-i])/Point;  
   }
   
   for(shift=limit;shift>=0;shift--) 
   {	
   
      if (Close[shift] < Open[shift])
      {
         if (Close[shift+1] < Open[shift])
         Bulls[shift] = MathMax(High[shift] - Close[shift+1], Close[shift] - Low[shift]);
         else
         Bulls[shift] = MathMax(High[shift] - Open[shift], Close[shift] - Low[shift]);
      }
      else
      if( Close[shift] > Open[shift] )
      { 
         if (Close[shift+1] > Open[shift])
         Bulls[shift] = High[shift] - Low[shift];
         else
         Bulls[shift] = MathMax(Open[shift] - Close[shift+1], High[shift] - Low[shift]);
      }
      else
      if( High[shift] - Close[shift] > Close[shift] - Low[shift] ) 
      {
         if (Close[shift+1] < Open[shift])
         Bulls[shift] = MathMax( High[shift] -  Close[shift+1],Close[shift] - Low[shift]);
         else
         Bulls[shift] = High[shift] - Open[shift];
      }
      else
      if( High[shift] - Close[shift] < Close[shift] - Low[shift])
      {
         if (Close[shift+1] > Open[shift])
         Bulls[shift] = High[shift] - Low[shift];
         else
         Bulls[shift]= MathMax(Open[shift] - Close[shift+1],High[shift] - Low[shift]);
      }
      else
      if (Close[shift+1] > Open[shift])
      Bulls[shift] = MathMax(High[shift] - Open[shift],Close[shift] - Low[shift]);
      else
      if (Close[shift+1] < Open[shift])
      Bulls[shift] = MathMax( Open[shift]- Close[shift+1], High[shift] - Low[shift]);
      else
      Bulls[shift] = High[shift] - Low[shift];
   
      
      
      if(Close[shift] < Open[shift])
      {
         if(Close[shift+1]>Open[shift])
         Bears[shift] = MathMax(Close[shift+1]- Open[shift],High[shift] - Low[shift]);
         else
         Bears[shift] = High[shift] - Low[shift];
      }   
      else
      if(Close[shift] > Open[shift])
      {
         if (Close[shift+1] > Open[shift])
         Bears[shift] = MathMax( Close[shift+1] - Low[shift],High[shift]- Close[shift]);
         else
         Bears[shift] = MathMax(Open[shift]- Low[shift],High[shift]- Close[shift]);
      }
      else
      if (High[shift] - Close[shift] > Close[shift] - Low[shift])
      {
         if (Close[shift+1] > Open[shift]) 
         Bears[shift] = MathMax(Close[shift+1]-Open[shift],High[shift] - Low[shift]);
         else
         Bears[shift] = High[shift] - Low[shift];
      }
      else
      if (High[shift] - Close[shift] < Close[shift] - Low[shift])
      {
         if (Close[shift+1]>Open[shift])
         Bears[shift] = MathMax(Close[shift+1]-Low[shift],High[shift]-Close[shift]);
         else
         Bears[shift] = Open[shift] - Low[shift];
      }  
      else
      if (Close[shift+1] > Open[shift])
      Bears[shift] = MathMax(Close[shift+1]-Open[shift],High[shift]-Low[shift]);
      else
      if (Close[shift+1]<Open[shift])
      Bears[shift] = MathMax(Open[shift] - Low[shift], High[shift] - Close[shift]);
      else
      Bears[shift] = High[shift] - Low[shift];
   }      
      
   for(shift=limit;shift>=0;shift--)
   { 
   AvgBulls[shift] = iMAOnArray(Bulls,0,Length,0,1,shift)/Point;  
   AvgBears[shift] = iMAOnArray(Bears,0,Length,0,1,shift)/Point;
   Diff[shift] = AvgBulls[shift] - AvgBears[shift];
   }
    
   for(shift=limit;shift>=0;shift--)
   BullnBear[shift] = iMAOnArray(Diff,0,Smooth,0,1,shift);   

   for(shift=limit;shift>=0;shift--)
   Sig[shift] = iMAOnArray(BullnBear,0,Signal,0,1,shift);  
//----
	return(0);	
}

