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

/*

RISK FACTORS:

PRICE VS DAILY OPEN 

PRICE VS HOURLY OPEN 

PRICE VS 1 HOUR VEGAS TUNNEL

PRICE VS DAILY DYNAMIC SUPPORT/RESISTANCE

PRICE VS HOURLY DYNAMIC SUPPORT/RESISTANCE

JJRVAT PRICE ANALYSIS

*/


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

#property  indicator_chart_window

extern bool sound.alert = false; 

extern int x.offset            = 50 ; 
extern int y.offset            = 100 ; 
extern int corner              = 1 ;

extern string myIndName        = "TRO_RISK2" ; 
extern bool   show.parms       = false ;
extern string myDayOpen        = "05:00" ; 
extern int    myTradeRisk      = 2 ;
extern int    myTrendRisk      = 2 ;
extern int    myBPBRisk        = 1 ;
extern int    myH1Risk         = 1 ;
extern int    myD1Risk         = 1 ;
extern int    myTunnelRisk     = 1 ;
extern int    myMACDRisk       = 1 ;

extern int    myResistanceTH   = 99 ;
extern int    mySupportTH      = 99 ;

extern string myFont       = "Courier" ;
extern int    myFontSize   = 12 ;
extern int    xStart       = 0 ;
extern int    xOffSet      = 0 ;


string   ToBuy = "", ToSell = "" ;
string   tObjName01 , tObjName02 , tObjName03, tObjName04 ;
string   tTrade = "" ,  tLastTrade = "" ;   
string   symbol, period, shortName, ArrowTrend ;

color    tColor    = White, ColorToBuy = White, ColorToSell = White;


int      TrendCount = 0 , Trend[] , OldTrend = 0, OldBars, LongRisk, ShortRisk ;
 
int      window; 
int      win ;  

string Arrow12 = "�";  // 12 oclock 225
string Arrow2  = "�";  //  2 oclock 228
string Arrow3  = "�";  //  3 oclock 224
string Arrow5  = "�";  //  5 oclock 230
string Arrow6  = "�";  //  6 oclock 226
string ArrowUp = "";
string ArrowDn = "";

double point  ;
double H1_Open, D1_Open ;


datetime PreviousBarTime ;

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

int cSR_Risk( string  SRPair, int SRPeriod  )
{
int hh, ll, fRisk;


double cc, xRES, xSUP, xRANGE, xRESpips, xSUPpips, xRet, xRetPct ;
		
    hh = Highest(SRPair,SRPeriod,MODE_HIGH,5,0);
    ll = Lowest(SRPair,SRPeriod,MODE_LOW,5,0); 

    cc = iClose(SRPair,SRPeriod,0) ;
        
    xRES = iHigh(SRPair,SRPeriod,hh) ;  
    xSUP = iLow(SRPair,SRPeriod,ll) ;
    

xRANGE = xRES - xSUP ;
    
xRESpips = (xRES - cc) / point ;  

xSUPpips = (cc - xSUP) / point ; 
    
xRet = cc - xSUP ;

xRetPct =  ( 100 * xRet ) / ( xRANGE );      

if(xRetPct >= 99){ fRisk = 1 ; } else {
if(xRetPct <= 01){ fRisk = -1 ;} else {
                   fRisk = 0 ; }}
                   

return( fRisk ) ;  
  
}

//+------------------------------------------------------------------+
int init()
  {
   symbol = Symbol();
   period = Period(); 
   point  = MarketInfo(symbol,MODE_POINT);  
   
   shortName = myIndName ;
   IndicatorShortName(shortName);   

   tObjName01 = myIndName+ symbol + period + "01";
   tObjName02 = myIndName+ symbol + period + "02";
   tObjName03 = myIndName+ symbol + period + "03";
   tObjName04 = myIndName+ symbol + period + "04";
   
   win = 0;         


  
   return(0);
  } 

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

int start()
  {

   int   yy = TimeYear(Time[0]);
   int   mm = TimeMonth(Time[0]);
   int   dd = TimeDay(Time[0]);
  
   string yymmddms = yy+"."+mm+"."+dd+ myDayOpen ;
         
   datetime var1 = StrToTime(yymmddms);
     
   int shift=iBarShift(NULL,0,var1,false);   


ShortRisk = 0 ;
LongRisk  = 0 ;

D1_Open = iOpen(NULL,PERIOD_D1,shift) ;

H1_Open = iOpen(NULL,PERIOD_H1,0) ;

if( Close[0] >= H1_Open ) { ShortRisk = ShortRisk + myH1Risk  ; } else { LongRisk  = LongRisk + myH1Risk  ; }
if( Close[0] >= D1_Open ) { ShortRisk = ShortRisk + myD1Risk ; }  else { LongRisk  = LongRisk + myD1Risk ; }


int BarShift = iBarShift(NULL,60,Time[0],true); 
   
double tunnel1 = iMA(NULL,60,144,0,1,0,BarShift);       
double tunnel2 = iMA(NULL,60,169,0,1,0,BarShift); 

if( Close[0] >= tunnel1 && Close[0] >= tunnel2 ) { ShortRisk = ShortRisk + myTunnelRisk ; }   
if( Close[0] < tunnel1  && Close[0] <  tunnel2 ) { LongRisk  = LongRisk  + myTunnelRisk ; }  


int H1_SR_Risk = cSR_Risk( symbol , 60 ) ;
int D1_SR_Risk = cSR_Risk( symbol , 1440 ) ;
                   

if( H1_SR_Risk == 1  ) { LongRisk  = LongRisk  + myH1Risk  ; }
if( H1_SR_Risk == -1 ) { ShortRisk = ShortRisk + myH1Risk  ; }
 
if( D1_SR_Risk == 1  ) { LongRisk  = LongRisk  + myD1Risk  ; }
if( D1_SR_Risk == -1 ) { ShortRisk = ShortRisk + myD1Risk  ; }


double cDiff = iMA(NULL,0,6,0,1,PRICE_CLOSE,0) - iMA(NULL,0,240,0,3,PRICE_CLOSE,0);
double pDiff = iMA(NULL,0,6,0,1,PRICE_CLOSE,1) - iMA(NULL,0,240,0,3,PRICE_CLOSE,1);

if( cDiff > pDiff ) { ShortRisk = ShortRisk + myMACDRisk ; } else {
if( cDiff < pDiff ) { LongRisk  = LongRisk  + myMACDRisk ; } }

if( cDiff > 0 ) { ShortRisk = ShortRisk + myMACDRisk ; } else {
if( cDiff < 0 ) { LongRisk  = LongRisk  + myMACDRisk ; } } 


if( LongRisk <= ShortRisk ) { ColorToBuy = Lime; ColorToSell = Red; ToBuy = "Long  Risk" ; ToSell = "Short Risk"; ArrowUp = Arrow12 ; ArrowDn = Arrow5 ; } 
else {  ColorToBuy = Red; ColorToSell = Lime; ToBuy = "Long  Risk" ; ToSell = "Short Risk"; ArrowUp = Arrow2 ; ArrowDn = Arrow6 ; } 




   ObjectCreate(tObjName03, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName03, ToBuy + " " + LongRisk , 8, myFont, ColorToBuy );  
   ObjectSet(tObjName03, OBJPROP_CORNER, corner);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 10+myFontSize+y.offset); //  

   ObjectCreate(tObjName04, OBJ_LABEL, win, 0, 0);// 
   ObjectSetText(tObjName04, ToSell  + " " + ShortRisk , 8, myFont, ColorToSell );  
   ObjectSet(tObjName04, OBJPROP_CORNER, corner);
   ObjectSet(tObjName04, OBJPROP_XDISTANCE, 0+x.offset); // 
   ObjectSet(tObjName04, OBJPROP_YDISTANCE, (5+myFontSize)*2+y.offset); // 




   WindowRedraw();    
   
   return(0);
  }  

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