//+------------------------------------------------------------------+
//|                  TRO_BIAS_History                                |
//|                                                                  |
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpledone@gmail.com                                        |  
//+------------------------------------------------------------------+ 

#property  copyright "Copyright © 2008, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

#property indicator_separate_window
#property indicator_buffers 5
#property indicator_color1 Lime    
#property indicator_color2 DarkGreen         
#property indicator_color3 Yellow      
#property indicator_color4 Maroon    
#property indicator_color5 Red        
    

//---- parameters
 
extern int  BiasPeriod  = 60 ;


//---- buffers

   double     ExtBuffer0[];   
   double     ExtBuffer1[];
   double     ExtBuffer2[];
   double     ExtBuffer3[]; 
   double     ExtBuffer4[];   
 
//+------------------------------------------------------------------+

int   i, y , z, shift,limit  ;
int myPeriod , digits;

double Bias_Open, Bias_Close, Bias_High, Bias_Low, Bias_Diff, Bias_Mid, Bias_Pivot ;

string BiasMsg, tBiasPeroid, myPair ;

//+------------------------------------------------------------------+

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_MN1: tfs="MN"; break;
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
   }
   return(tfs);
} 

//+------------------------------------------------------------------+

int init()
{
   SetIndexBuffer(0,ExtBuffer0);
   SetIndexBuffer(1,ExtBuffer1);
   SetIndexBuffer(2,ExtBuffer2);
   SetIndexBuffer(3,ExtBuffer3);
   SetIndexBuffer(4,ExtBuffer4);
 
   
   SetIndexStyle(0,DRAW_HISTOGRAM, EMPTY, 4, indicator_color1);
   SetIndexStyle(1,DRAW_HISTOGRAM, EMPTY, 4, indicator_color2);
   SetIndexStyle(2,DRAW_HISTOGRAM, EMPTY, 4, indicator_color3);
   SetIndexStyle(3,DRAW_HISTOGRAM, EMPTY, 4, indicator_color4);
   SetIndexStyle(4,DRAW_HISTOGRAM, EMPTY, 4, indicator_color5);
 

myPair = Symbol();
   
if( myPeriod == 0) myPeriod = Period() ;    


string tPeriod = TimeFrameToString(BiasPeriod) ;

IndicatorShortName("BiasHist "+myPair+"("+tPeriod+")" );
    
  digits = Digits ;
  
      
return(0);
}

//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
  {


   int counted_bars=IndicatorCounted();

   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(int i=0; i<limit; i++)

   {
 
      y = iBarShift(NULL,BiasPeriod,Time[i]);
      z = iBarShift(NULL,BiasPeriod,Time[i+1]);
       
      Bias_Close   = iClose(NULL,0,i) ;            // close of the chart period
      Bias_Open    = iOpen(NULL,BiasPeriod,y) ;    // open of the Bias period  
      Bias_High    = iHigh(NULL,BiasPeriod,z) ;    // high of the Bias period + 1
      Bias_Low     = iLow(NULL,BiasPeriod,z) ;     // low of the Bias period + 1
  
      Bias_Diff    = Bias_Close - Bias_Open ;
 
      Bias_Mid     = ( Bias_High + Bias_Low ) * 0.50 ;
      Bias_Mid     = NormalizeDouble( Bias_Mid , digits ) ;
 
      Bias_Pivot   = ( Bias_High + Bias_Low + Bias_Close ) * 0.33333 ; 
      Bias_Pivot   = NormalizeDouble( Bias_Pivot , digits ) ; 
 
   while(true)
   {
      
   if (Bias_Diff > 0 && Bias_Close > Bias_Mid ) { PlotHist(4,0,0,0,0,i) ; break; } // strong up
   if (Bias_Diff > 0 && Bias_Close < Bias_Mid ) { PlotHist(0,4,0,0,0,i) ; break; } // weak up
   if (Bias_Diff < 0 && Bias_Close > Bias_Mid ) { PlotHist(0,0,0,4,0,i) ; break; } // weak down
   if (Bias_Diff < 0 && Bias_Close < Bias_Mid ) { PlotHist(0,0,0,0,4,i) ; break; } // strong down
                                                  PlotHist(0,0,4,0,0,i) ; break;   // no bias

   } // while 
       
   } // for     
 
return(0);

}

//+------------------------------------------------------------------+
      
 
void PlotHist(int a,int b,int c,int d,int e, int bars)
{

ExtBuffer0[bars]=a;    
ExtBuffer1[bars]=b;
ExtBuffer2[bars]=c;
ExtBuffer3[bars]=d;
ExtBuffer4[bars]=e;
 
}
 


//+------------------------------------------------------------------+

/*

   int limit;
   int counted_bars=IndicatorCounted();

   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;

   for(int i=0; i<limit; i++)

{
 
 Bias_Close   = iClose(NULL,BiasPeriod,i) ; 
 Bias_Open    = iOpen(NULL,BiasPeriod,i) ;  
 Bias_High    = iHigh(NULL,BiasPeriod,i+1) ; 
 Bias_Low     = iLow(NULL,BiasPeriod,i+1) ;
  
 Bias_Diff    = Bias_Close - Bias_Open ;
 
 Bias_Mid     = ( Bias_High + Bias_Low ) * 0.50 ;
 Bias_Mid     = NormalizeDouble( Bias_Mid , digits ) ;
 
 Bias_Pivot   = ( Bias_High + Bias_Low + Bias_Close ) * 0.33333 ; 
 Bias_Pivot   = NormalizeDouble( Bias_Pivot , digits ) ; 
 

while(true)
{
      
  if (Bias_Diff > 0 && Bias_Close > Bias_Mid ) { ExtBuffer0[i]= 2 ; break; } // strong up
  if (Bias_Diff > 0 && Bias_Close < Bias_Mid ) { ExtBuffer0[i]= 1 ; break; } // weak up
  if (Bias_Diff < 0 && Bias_Close > Bias_Mid ) { ExtBuffer0[i]= -1 ; break; } // weak down
  if (Bias_Diff < 0 && Bias_Close < Bias_Mid ) { ExtBuffer0[i]= -2 ; break; } // strong down
                                                 ExtBuffer0[i]= 0 ; break;   // no bias

} // while 
       
 
        }
 
return(0);

}




  if (Bias_Diff > 0 && Bias_Close > Bias_Mid ) { ExtBuffer0[i]= 2 ; break; } // strong up
  if (Bias_Diff > 0 && Bias_Close < Bias_Mid ) { ExtBuffer0[i]= 1 ; break; } // weak up
  if (Bias_Diff < 0 && Bias_Close > Bias_Mid ) { ExtBuffer0[i]= -1 ; break; } // weak down
  if (Bias_Diff < 0 && Bias_Close < Bias_Mid ) { ExtBuffer0[i]= -2 ; break; } // strong down
                                                 ExtBuffer0[i]= 0 ; break;   // no bias

*/