//+------------------------------------------------------------------+ 
//|   TRO_RULE_OF_THUMB_TRIGGER                                      | 
//|                                                                  | 
//|   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_chart_window 
 
extern bool Show.Rules   = true ;
extern bool Show.Lines   = true ; 
 
//extern bool Show.Message = true ; 

extern int win = 0;
extern int price.x.offset= 500 ; 
extern int price.y.offset= 300 ; 


extern int   myFontSize = 20;

extern int myBars        = 10 ;
extern int myThreshold   = 3 ;
extern int myPipTrigger  = 4 ;

extern string Buy_Message  = "BUY ON GREEN CANDLE" ;
extern string Wait_Message = "WAIT " ;
extern string Sell_Message = "SELL ON RED CANDLE" ;
extern string Rdy_Message  = "GET READY " ;

extern color Buy_color = Lime;
extern color Wait_color = Yellow;
extern color Sell_color = Red;
extern color Rdy_color = Orange;
 
extern color ColorRes = Red ;
extern color ColorSup = Blue ; 
extern int       mySRBars       = 3 ;
extern int       mySRShift      = 1 ; 
//---- input parameters 

extern int Symbol_1_Kod=217; //140
extern int Symbol_2_Kod=218; // 141
extern int Symbol_3_Kod=142;


//---- buffers 
double FP_BuferUp[];
double FP_BuferDn[]; 
double High0, High1, High2 ;
double Low0, Low1, Low2 ;
double H1Diff, Diff0, Diff1, Diff2 ;
double H1Close, Close0, Close1, Close2 ;
double H1Open, Open0, Open1, Open2 ;
 

 
string symbol, tChartPeriod,  tShortName ;  
int    digits, period  ; 

bool Trigger1,  Trigger2,  Trigger3 ;

int OldBars = -1 ;
 

color tColor = Yellow, ClOpColor ;
string tMessage , oldmsg = "", tMessageB, tMessageS ; 


int nHH, nLL; 

double    point, Pct, range, myUpper, myLower   ;


string tRes0 = "hhRes_0" ;
string tSup0 = "hhSup_0" ;
datetime StartTime;

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

int init() 
  { 
  
    
   period       = Period() ;     
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   
   point        = Point ;

   if(digits == 5 || digits == 3) {  point = point * 10 ; } 
   
   tShortName = "ROT"+ symbol + tChartPeriod  ;

 
   SetIndexStyle(0,DRAW_ARROW,0,1); 
   SetIndexArrow(0,Symbol_1_Kod); 
   SetIndexBuffer(0,FP_BuferUp); 
   SetIndexEmptyValue(0,0.0); 
   
   SetIndexStyle(1,DRAW_ARROW,0,1); 
   SetIndexArrow(1,Symbol_2_Kod); 
   SetIndexBuffer(1,FP_BuferDn); 
   SetIndexEmptyValue(1,0.0); 
 
   tRes0 = tShortName + "Res_0" ;
   tSup0 = tShortName + "Sup_0" ;
   
   return(0); 
  } 
//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
  { 

   ObjectDelete("TRO330");
   ObjectDelete("TRO329");
   ObjectDelete("TRO329B");
   ObjectDelete("TRO331");
   ObjectDelete("TRO332");
   ObjectDelete("TRO334");   
   
   ObjectDelete("TRO330");
   ObjectDelete("TRO329");
   ObjectDelete("TRO329B");
   ObjectDelete("TRO331");
   ObjectDelete("TRO332");
   ObjectDelete("TRO334");  

   ObjectDelete(tRes0);           
   ObjectDelete(tSup0);
   
   ObjectsDeleteAll( 0, OBJ_FIBO ) ;

   return(0); 
  } 


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

int start() 
  { 
  
   int limit,i,counted_bars=IndicatorCounted();
//----
   limit=Bars-counted_bars;

   H1Diff = iClose(NULL,60,0) - iOpen(NULL,60,0) ;

  for(i = 0 ; i <= 0; i++) 
  { 
    High0 = iHigh(NULL,period,i) ;
    Low0  = iLow(NULL,period,i) ;
    Diff0 = iClose(NULL,period,i) - iOpen(NULL,period,i) ;
    Diff1 = iClose(NULL,period,i+1) - iOpen(NULL,period,i+1) ;  
    Diff2 = iClose(NULL,period,i+2) - iOpen(NULL,period,i+2) ;
        
    if( Diff0 < 0 && Diff1 < 0 && Diff2 < 0  ) {FP_BuferUp[i] = Low0 ; } else  {FP_BuferUp[i] = 0 ; }
    if( Diff0 >= 0 && Diff1 >= 0 && Diff2 >= 0  ) {FP_BuferDn[i] = High0 ; } else  {FP_BuferDn[i] = 0 ; }
      
  } 
   
   
   tMessageB = "" ;
   tMessageS = "" ;
      
   Close0 = iClose(NULL,period,0) ;
   Close1 = iClose(NULL,period,1) ;
   Close2 = iClose(NULL,period,2) ;   
   
   Open0  = iOpen(NULL,period,0) ;
   Open1  = iOpen(NULL,period,1) ;
   Open2  = iOpen(NULL,period,2) ;
      
   if( Open0 < Close1 ) { tMessageB = "!!" ;  }  
   if( Open0 > Close1 ) { tMessageS = "!!" ;  }  
      
 nHH      =  Highest(NULL,0,MODE_HIGH,mySRBars,mySRShift); 
 nLL      =  Lowest(NULL,0,MODE_LOW,mySRBars,mySRShift); 
 
 myUpper      = High[nHH]; 
 myLower      = Low[nLL];  
 
 range    = (myUpper - myLower)  ; 
  
 if( range != 0) { Pct = 100 * ( Close[0]  - myLower ) / range ; } else {Pct = 0 ;}  
 
   while(true)
   { 
      if(FP_BuferUp[1] != 0 && H1Diff > 0 ) { ClOpColor = Buy_color ;  tMessage = Buy_Message  + tMessageB ; break ; }           
      if(FP_BuferDn[1] != 0 && H1Diff < 0 ) { ClOpColor = Sell_color ; tMessage = Sell_Message + tMessageS ; break ; }    
      
      ClOpColor = Wait_color ; tMessage = Wait_Message + " " + DoubleToStr(Pct,0) + "%" + " " + DoubleToStr(range/point,0) ;  break ;
   } 
 
if( Close0 >= myUpper && Close0 > Open0 && Close1 > Open1 ) { ClOpColor = Buy_color  ; tMessage = "BreakOut UP" ; } 
if( Close0 <= myLower && Close0 < Open0 && Close1 < Open1 ) { ClOpColor = Sell_color ; tMessage = "BreakOut DOWN" ; } 
   
if(Show.Rules) {DoShowRules() ; }       
if(Show.Lines) {DoShowLines() ; }   

//+------------------------------------------------------------------+  
                    
   ObjectCreate("TRO331", OBJ_LABEL, win, 0, 0);
   ObjectSetText("TRO331",tMessage, myFontSize , "Arial Bold", ClOpColor );
   ObjectSet("TRO331", OBJPROP_CORNER, 0);
   ObjectSet("TRO331", OBJPROP_XDISTANCE, price.x.offset); 
   ObjectSet("TRO331", OBJPROP_YDISTANCE, price.y.offset); 


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

        
   return(0);
}

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

string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      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;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}

//+------------------------------------------------------------------+  
void DoShowRules()
{

Comment( 

" ", "\n", 
"RULES OF THUMB", "\n", 

" ", "\n", 
"ALWAYS TRADE WITH H1 CANDLE COLOR", "\n",     

" ", "\n", 
"TRADING WITH D1 CANDLE COLOR SAFER THAN TRADING AGAINST", "\n",     

" ", "\n",    
"ONLY ENTER REVERSAL TRADES ON BAR 4 OR MORE OF A RUN ", "\n",  
 
" ", "\n", 
"ONLY ENTER BREAK TRADES ON BAR 2 OF A RUN WHEN PRICE IS AT ", "\n", 
"THE HIGHEST HIGH OR LOWEST LOW OF THE PREVIOUS 3 BARS", "\n",   

" ", "\n", 
"ALWAYS CHECK THE NEWS EVERY HOUR BEFORE TRADING", "\n",  
 
"") ; 
}


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


void DoShowLines()
{

   StartTime = Time[3] ;

   range = myUpper  - myLower  ;
   
	if(ObjectFind(tRes0) == -1)
		ObjectCreate(tRes0,OBJ_FIBO,0,StartTime,myUpper,StartTime,myUpper);
	else
	{
		ObjectSet(tRes0,OBJPROP_TIME2, StartTime);
		ObjectSet(tRes0,OBJPROP_TIME1, StartTime);
		ObjectSet(tRes0,OBJPROP_PRICE1,myUpper);
		ObjectSet(tRes0,OBJPROP_PRICE2,myUpper);
	}
   ObjectSet(tRes0,OBJPROP_LEVELCOLOR,ColorRes);
   ObjectSet(tRes0,OBJPROP_FIBOLEVELS,1);
 //  ObjectSet(tRes0,OBJPROP_FIRSTLEVEL+0,0.0);	
   ObjectSetFiboDescription(tRes0,0, DoubleToStr(myUpper,digits)+ " Resistance  "); 
//   ObjectSet(tRes0,OBJPROP_RAY,true);
//   ObjectSet(tRes0,OBJPROP_BACK,true);


		if(ObjectFind(tSup0) == -1)
			ObjectCreate(tSup0,OBJ_FIBO,0,StartTime,myLower,StartTime,myLower);
		else
		{
			ObjectSet(tSup0,OBJPROP_TIME2, StartTime);
			ObjectSet(tSup0,OBJPROP_TIME1, StartTime);
			ObjectSet(tSup0,OBJPROP_PRICE1,myLower);
			ObjectSet(tSup0,OBJPROP_PRICE2,myLower);
		}
   	ObjectSet(tSup0,OBJPROP_LEVELCOLOR,ColorSup); 
   	ObjectSet(tSup0,OBJPROP_FIBOLEVELS,1);
   
//   	ObjectSet(tSup0,OBJPROP_FIRSTLEVEL+0,0.000);	
   	ObjectSetFiboDescription(tSup0,0,DoubleToStr(myLower,digits)+ " Support  "); 

//   	ObjectSet(tSup0,OBJPROP_RAY,true);
//   	ObjectSet(tSup0,OBJPROP_BACK,true);

}


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


/*
if( Close0 >= myUpper && Close0 > Open0 && Close1 > Open1 && Close2 < Open2 ) {tMessage = "BreakOut UP" ; } 
if( Close0 <= myLower && Close0 < Open0 && Close1 < Open1 && Close2 > Open2 ) {tMessage = "BreakOut DOWN" ; } 

Comment( 
"high0 = ", DoubleToStr(high0 , digits), "\n",        
"high1 = ", DoubleToStr(high1 , digits), "\n",  
"high2 = ", DoubleToStr(high2 , digits), "\n",  
"Low0 = ", DoubleToStr(Low0 , digits), "\n",        
"Low1 = ", DoubleToStr(Low1 , digits), "\n",  
"Low2 = ", DoubleToStr(Low2 , digits), "\n",  
"") ; 

*/