//+------------------------------------------------------------------+
//|                                                                  |
//|                               _TRO_jjrvat_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 Blue    
#property indicator_color2 DodgerBlue         
#property indicator_color3 DimGray      
#property indicator_color4 Maroon    
#property indicator_color5 Red         
     

//---- buffers


   double     ExtBuffer0[];   
   double     ExtBuffer1[];
   double     ExtBuffer2[];
   double     ExtBuffer3[]; 
   double     ExtBuffer4[];   

 

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


 double iParm3 = 6;
 double iParm4 = 0;
 double iParm5 = 3;
 double iParm6 = 0;
 double iParm7 = 0;
 double iParm8 = 0;


 string myPair    = "";
 int    myBars    = 100;

double open    ;  
double close    ; 
double close1   ;  
double high    ;    
double high1    ;   
double low     ;    
double low1     ; 

double mid      ; 
double mid1    ; 


int pCount, qCount   ;


color  MaxColor, MinColor, TriggerColor ;

string PriceDir   ;
string MaxDir, MinDir ;
string tPeriod   ; 

double cHiCl, cClLo, cHi,  cLo,  cX, cVal , cVal1 ;
double pHiCl, pClLo, pHi,  pLo,  pX, pVal , pVal1 ;
double MaxVal  ;
double MinVal  ;
double pMaxVal  ;
double pMinVal  ;

bool LgBias, ShBias  ;

string Trigger, tBias ;

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

int myPeriod = 0 ;



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

string Arrow12 = "ñ"; // 12 oclock
string Arrow2  = "ö";  // 2 oclock
string Arrow3  = "ð";  // 3 oclock
string Arrow5  = "ø";  // 5 oclock
string Arrow6  = "ò";  // 6 oclock

 color N_color  = Lime;
 color NE_color = LimeGreen;
 color E_color  = Gold;
 color SE_color = Orange;
 color S_color  = Red;

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

color fColor( double i)
{
color rColor ;

    if ( i ==  2 ) rColor = N_color  ; else {
    if ( i ==  1 ) rColor = NE_color  ; else {
    if ( i == -1 ) rColor = SE_color  ; else {
    if ( i == -2 ) rColor = S_color  ; else {     
                   rColor = E_color ; } } } }

return ( rColor );
}
//+------------------------------------------------------------------+

int fDirection( double i, double j )
{
int rDir ;

    if( i > j  )   rDir = 2;  else 
    if( i < j  )   rDir = -2; else     
                   rDir = 0; 
    
return ( rDir );
}


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


string fCompass( double i )
{
string rCompass ;

    if ( i == 2 ) rCompass = Arrow12  ; else {
    if ( i == -2 ) rCompass = Arrow6  ; else {      
    rCompass = Arrow3 ; } } 
    
return ( rCompass );
}


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

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);
 
   

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

string tPeriod = TimeFrameToString(myPeriod) ;

IndicatorShortName("TRO_jjrvat "+myPair+"("+tPeriod+")" );
    
return(0);
}

//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
  {
  
   int counted_bars=IndicatorCounted();
   
   if(counted_bars<0) return(-1);
   
   if(counted_bars>0) counted_bars--;
   
   int bars=Bars-counted_bars;

int    i ;
double xVal, xVal1, xVal2 ;

//---- main calculation loop

while(bars>=0)
{          
       
LgBias = false; 
ShBias = false; 

Trigger = "Wait" ;

TriggerColor = Yellow ;

pMaxVal = 0 ;
pMinVal = 999999999 ;

MaxVal  = 0 ;
MinVal  = 999999999 ;

xVal  = iMA(NULL,myPeriod, 240 , 0 ,MODE_LWMA , PRICE_CLOSE , bars ) ;  

low   = iLow(NULL,myPeriod,bars) ;  
high  = iHigh(NULL,myPeriod,bars) ;  
close = iClose(NULL,myPeriod,bars) ; 

if(close > xVal) { LgBias = true ; tBias = "L" ; } else { ShBias = true ; tBias = "S" ;}

for(i = 1; i < myBars   ; i++ )
{
   xVal  = iMA(NULL,myPeriod, iParm3, iParm4, iParm5, iParm6, i ) ;  
   xVal1 = iMA(NULL,myPeriod, iParm3, iParm4, iParm5, iParm6, i+1 ) ;  
   xVal2 = iMA(NULL,myPeriod, iParm3, iParm4, iParm5, iParm6, i+2 ) ;  

if( MaxVal == 0 ) { if( xVal < xVal1 && xVal1 > xVal2) { MaxVal  = xVal1 ; } } 
else if( pMaxVal == 0 ) { if( xVal < xVal1 && xVal1 > xVal2) { pMaxVal = xVal1 ; } } 
   
if( MinVal == 999999999 ) { if( xVal > xVal1 && xVal1 < xVal2) { MinVal  = xVal1 ; } }  
else if( pMinVal == 999999999 ) { if( xVal > xVal1 && xVal1 < xVal2) { pMinVal = xVal1 ; } }    
   
 



cVal = iMA(NULL,myPeriod, 6 , 0 , MODE_LWMA, PRICE_CLOSE, bars ) ;   


                                                                                                                  
pCount   = fDirection( MaxVal , pMaxVal) ;

MaxColor = fColor( pCount ) ;

MaxDir   = fCompass( pCount ) ;

qCount   = fDirection( MinVal , pMinVal) ;

MinColor = fColor( qCount ) ;

MinDir   = fCompass( qCount ) ;


      
if (LgBias && qCount ==  2 && close > cVal && low  < cVal && close > MinVal) { PlotHist(4,0,0,0,0,bars) ; break; }
if (ShBias && pCount == -2 && close < cVal && high > cVal && close < MaxVal) { PlotHist(0,0,0,0,4,bars) ; break; }
                                                                               PlotHist(0,0,4,0,0,bars) ; break; 


} // for i=
       
bars--; } // for bars=
 
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;
 
}

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