//+------------------------------------------------------------------+
//|   TRO_SR_RR_X                                                    |
//|                                                                  | 
//|   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 1
#property  indicator_color1  Silver

//---- input parameters 


bool UseBias     = true ;

extern int  myBiasPeroid      = 1440 ;
extern string myOpenHHMM      = "14:00"; // 05:00 am pst = 14:00 server time 
extern int       myBars       = 21 ;


 int       myShift      = 1 ;
 int       myRR         = 1.5 ;


double     SignalBuffer[];

 double    iLevel1=0.24; 
 double    iLevel2=0.382; 
 double    iLevel3=0.5; 
 double    iLevel4=0.618; 
 double    iLevel5=0.76; 



//---- buffers

double    myUpper      = 0 ; 
double    myLower      = 0 ;
double    myWindow     = 0 ;

int i;
  
double R1, R2, R3 ;  

double X05 ;
double X06 ;   
double X07 ;
double X08, XRN ; 
double xRet ;
double xRetPct;    
  
string symbol,tChartPeriod ; 


double range  ;
double DynR; 
double DynS;  
datetime StartTime;
int digits,period ;

double Entry, work, goShort = 0, goLong = 0, reward = 0, risk = 0, ratio = 0, pHigh, pLow, pClose, LgTrigger, ShTrigger, pivot ;
double hipv, pvlo, pvpct, fibpct, ShortPL, LongPL, point, close, MaxRatio = 0, MaxReward = 0,MaxRisk = 0;

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

string BiasMsg, tBiasPeroid ;

bool long = false , short = false ;

bool Bias_Long = false , Bias_Short = false ;


bool OKtoLong   = false ;
bool OKtoShort  = false ; 

bool xAutoCalc = true;
int nHH, nLL; 

 
color  ColorShortPL = Yellow, ColorLongPL = Yellow, ColorRatio = Yellow, ColorBias = Yellow ;

double xfb1; 
double xfb2; 
double xfb3; 
double xfb4; 
double xfb5; 

      
datetime Trigger ;      

//+------------------------------------------------------------------+
   
int init()
  {

  symbol       =  Symbol() ;
  period       =  Period() ;
  tChartPeriod =  TimeFrameToString(period ) ;

  
  tBiasPeroid = TimeFrameToString(period) ;
 
   SetIndexStyle(0,DRAW_HISTOGRAM); 
   SetIndexBuffer(0,SignalBuffer);


    
   return(0);
  }

//+------------------------------------------------------------------+
int deinit()
  {

       
   return(0);
  }

//+------------------------------------------------------------------+
int start()
{


  
/*   
Reward = Target Point - Entry Point = 1.9869-1.9782 = 87 pips

Risk= Entry Point - Stop Loss = 1.9782 - 1.9747 = 35pips

Risk to Reward Ratio = 87 / 35 = 1:2.5

*/


 
 close    =  iClose(symbol,period,0) ; 
 
 nHH      =  Highest(symbol,period,MODE_HIGH,myBars,myShift); 
 nLL      =  Lowest(symbol,period,MODE_LOW,myBars,myShift); 
 
 myUpper      = iHigh(symbol,period,nHH); 
 myLower      = iLow(symbol,period,nLL); 
  
 

 if( nHH < nLL ) { pClose = iClose(symbol,period,nHH); pHigh  = iHigh(symbol,period,nHH); pLow = iLow(symbol,period,nHH);} 
 else            { pClose = iClose(symbol,period,nLL); pHigh  = iHigh(symbol,period,nLL); pLow = iLow(symbol,period,nLL);}  
 
 
      
   pivot  = ( pHigh + pLow + pClose ) / 3 ;
   pivot  =  NormalizeDouble(pivot,4) ;
   hipv   = myUpper - pivot ;
   pvlo   = pivot - myLower ;
 
   work   = MathMin(hipv, pvlo)  ;
 
   goLong  = myLower + work ;
   goLong  = NormalizeDouble(goLong, 4) ;
   
   goShort = myUpper - work ;    
   goShort = NormalizeDouble(goShort, 4) ; 
 
/*        
   reward   = myUpper - goLong ;
   risk     = goLong - myLower ;
   
   if(risk != 0) { ratio = reward / risk ; } else { ratio = -999 ; } 
   
   range    = myUpper -  myLower ;
   
   if(range != 0) { pvpct = work / ( range ) ; } else { pvpct = -999 ; } 
 


   if(ratio  > myRR   )   { ColorRatio = Green ; } else  { ColorRatio = Red ; }
   
   if(ratio  > MaxRatio)  { MaxRatio  = ratio ; }
   
   if(reward > MaxReward) { MaxReward = reward ; }
   
   if(risk   > MaxRisk)   { MaxRisk   = risk ; }
 
 */      
   
//------------   

int   yy = TimeYear(Time[0]);
int   mm = TimeMonth(Time[0]);
int   dd = TimeDay(Time[0]);  
   
string  yymmddms = yy+"."+mm+"."+dd+" " + myOpenHHMM ;
         
datetime var1 = StrToTime(yymmddms);  
  
  
datetime day  = 24*60*60;
datetime   r  = var1 ;


int   cd = iBarShift(symbol,period,r,FALSE);
 
datetime r2=r-day;


if(TimeDayOfWeek(r2)==0) r2=r2-day-day;
int cd2=iBarShift(symbol,period,r2,FALSE);

if( var1 > Time[0] ) { r = r2 ;cd = cd2 ;} // if date/time is greater than current date/time go back 1 day


//------------
OKtoLong   = false ;
OKtoShort  = false ; 

Bias_Long  = false ; 
Bias_Short = false ;

 Bias_Close   = iClose(symbol,myBiasPeroid,0) ; 

 Bias_High    = iHigh(symbol,myBiasPeroid,1) ; 
 Bias_Low     = iLow(symbol,myBiasPeroid,1) ;
 

 if( myBiasPeroid == 1440 ) {Bias_Open = iOpen(symbol,period,cd) ; } else {Bias_Open = iOpen(NULL,myBiasPeroid,0) ; } 

  
 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 ) ; 
 
    
  
   if( Bias_Diff > 0 && Bias_Close > Bias_Mid ) { Bias_Long  = true ; SignalBuffer[0] = 2;  BiasMsg = "UP STRONG" ; ColorBias = Green   ; } else 
   if( Bias_Diff > 0 && Bias_Close < Bias_Mid ) { Bias_Long  = true ; SignalBuffer[0] = 1;  BiasMsg = "Up Weak" ;   ColorBias = Green   ; } else    
   if( Bias_Diff < 0 && Bias_Close < Bias_Mid  ){ Bias_Short = true ; SignalBuffer[0] = -2; BiasMsg = "DN STRONG" ; ColorBias = Red     ; } else 
   if( Bias_Diff < 0 && Bias_Close > Bias_Mid  ){ Bias_Short = true ; SignalBuffer[0] = -1; BiasMsg = "Dn Weak" ;   ColorBias = Red ; } else    
                                                {                     SignalBuffer[0] = 0;  BiasMsg = "No Bias" ;   ColorBias = Gray; }
                                                
   if( (UseBias && Bias_Long)  || !UseBias ) { OKtoLong  = true ; }                                                                                 
   if( (UseBias && Bias_Short) || !UseBias ) { OKtoShort = true ; }  
                                                   
  
         
   LongPL   = 0 ;
   ShortPL  = 0 ;   
   

        
   if( OKtoLong && MarketInfo(symbol,MODE_ASK)  == goLong  && goLong  > iLow(symbol,period,0) ) 
   { 
      long  = true; 
      short = false ; 
            
      if( Trigger != Time[0] )
      { 
         Trigger = Time[0] ;  
         SignalBuffer[0] = 3; 
      }
   }
        
 
  if( OKtoShort && MarketInfo(symbol,MODE_BID) == goShort && goShort < iHigh(symbol,period,0) )
  {  
      long = false; 
      short = true ;      
      
      if( Trigger != Time[0]  )
      {         
         Trigger = Time[0] ; 
         SignalBuffer[0] = -3; 
      }
  }     


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

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

/*
  
Comment(

BiasMsg, "\n",
"OKtoShort = ",OKtoShort, "\n",
"OKtoLong = ",OKtoLong, "\n",

"symbol = ",symbol, "\n",
"period = ",period, "\n",
"myBiasPeroid = ",myBiasPeroid, "\n",
"SignalBuffer[0] = ",SignalBuffer[0], "\n",


"myUpper = ",DoubleToStr(myUpper,4), "\n",
"goShort = ",DoubleToStr(goShort,4), "\n",
"goLong = ",DoubleToStr(goLong,4), "\n",
"myLower = ",DoubleToStr(myLower,4), "\n",

"MarketInfo(symbol,MODE_ASK) = ",DoubleToStr(MarketInfo(symbol,MODE_ASK),4), "\n",
"MarketInfo(symbol,MODE_BID) = ",DoubleToStr(MarketInfo(symbol,MODE_BID),4), "\n",

"iHigh(symbol,period,0) = ",DoubleToStr(iHigh(symbol,period,0),4), "\n",
"iLow(symbol,period,0) = ",DoubleToStr(iLow(symbol,period,0),4), "\n",

"Bias_Open = ",Bias_Open, "\n", 

"") ;   
        
   
Comment(
"SignalBuffer[0] = ", DoubleToStr( SignalBuffer[0], 4), "\n",

"") ; 

Comment(
"goLong = ", DoubleToStr( goLong, Digits), "\n",
"goShort = ", DoubleToStr( goShort, Digits), "\n",
"close = ", DoubleToStr( close, Digits), "\n",
"LongPL = ", DoubleToStr( LongPL, Digits), "\n",
"ShortPL = ", DoubleToStr( ShortPL, Digits), "\n",
"") ;



Comment(
"myUpper = ", DoubleToStr( myUpper, Digits), "\n",
"myLower = ", DoubleToStr( myLower, Digits), "\n",
"reward = ", DoubleToStr( reward, Digits), "\n",
"risk = ", DoubleToStr( risk, Digits), "\n",
"ratio = ", DoubleToStr( ratio, Digits), "\n",
"pivot = ", DoubleToStr( pivot, Digits), "\n",
"") ; 


      
*/