//+------------------------------------------------------------------+ 
//|   TRO_MARKET                                                     | 
//|                                                                  | 
//|   Copyright © 2008, AVERY T. HORTON, JR. AKA TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
 

#property indicator_chart_window

extern int myChartX   = 400 ;
extern int myChartY   = 0 ;
extern int myCorner   = 0 ;
extern string myFont  = "Courier" ;
extern int myFontSize = 12 ;

extern int       MAPeriod         = 21;
extern string    notetype         = "0=SMA,1=EMA,2=SMMA,3=LWMA" ;
extern int       MAType           = 1;
extern string    noteprice        = "0=CLOSE,1=OPEN,2=HIGH,3=LOW,4=MEDIAN,5=PP,6=WEIGHT" ;
extern int       MAPrice          = PRICE_CLOSE ;

extern int Line_Style   = STYLE_DOT ;
extern int Line_Width   = 1 ;
extern color HighColor  = Red ;
extern color LowColor   = Blue ;



double Hi, Li, C;

double prev_high, prev_low, prev_close, D, p, value ;
int h  ;
string lbl[15], lbl2[15];
int n ;

color pColor = Blue, ColorMarket = Yellow  ;

string symbol, tChartPeriod, ShortName, tMarket, LabelTime ;  

int    digits, period, win ; 
double    H1L0, L1H0 ;

double point,  open, high, low, close, mid, range, pClose, pOpen, pHigh, pLow, pMid, pRange, cRange  ;  
double xMax = - 1,  xMin = 9999999999999, xDiff, xClose, xHigh, xLow, xRunUp, xRunDn, xPain ;
double High0, Low0, Open0,  Close0, Open1,High1, Low1, Close1   ;  

double     ind_bufferOp[];
double     xTrendOp[];
double     xCountOp;

double     ind_bufferHi[];
double     xTrendHi[];
double     xCountHi; 

double     ind_bufferLo[];
double     xTrendLo[];
double     xCountLo; 

double     ind_bufferCl[];
double     xTrendCl[];
double     xCountCl; 

int nHH, nLL, nXA, nXB, i, j, min, max, start, end , xBars ; 

double    myUpper      = 0 ; 
double    myLower      = 0 ;
double    MA_Cur, MA_Prev, cClose; 
   
bool xUp, xDn, bull, bear, ranging ;

   
string Obj0001,Obj0002,Obj0003,Obj0004,Obj0005,Obj0006,Obj0007,Obj0008,Obj0009,Obj0010,Obj0011;  


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

int init()
  {
   period       = Period() ;   
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    = "tstats"+symbol+period ;
   win          = 0 ;
   
   LabelTime    = symbol + tChartPeriod + period + MAPeriod + MAType ;  
   
   Obj0001 = "BKOUT_0001" + LabelTime ; 
   Obj0002 = "BKOUT_0002" + LabelTime ; 
   Obj0003 = "BKOUT_0003" + LabelTime ; 
   Obj0004 = "BKOUT_0004" + LabelTime ;    
        
   return(0);
  }
  
//+------------------------------------------------------------------+
int deinit()
{
   if(ObjectFind(Obj0001) > -1 ) ObjectDelete(Obj0001);  
   if(ObjectFind(Obj0002) > -1 ) ObjectDelete(Obj0002); 
   if(ObjectFind(Obj0003) > -1 ) ObjectDelete(Obj0003);  
   if(ObjectFind(Obj0004) > -1 ) ObjectDelete(Obj0004); 
     
   for(i=0;i<20;i++)
   {
      if(ObjectFind(lbl[i])!=-1) { ObjectDelete(lbl[i]); }  
   }
   
   return(0);
}
  
//+------------------------------------------------------------------+
int start()
{

deinit() ;

n        = 10;
 

 
 xUp      = false ; 
 xDn      = false ;   
 bull     = false ;
 bear     = false ;
 ranging  = false ; 



  
   for( i = 0 ; i <= 1000 ; i++ )
   {
       MA_Cur  = iMA(NULL,period,MAPeriod,0,MAType,MAPrice,i);
       MA_Prev = iMA(NULL,period,MAPeriod,0,MAType,MAPrice,i+1);
       cClose  = iClose(NULL,period,i) ;
       pClose  = iClose(NULL,period,i+1) ;
       
      if( !xUp && cClose > MA_Cur && pClose < MA_Prev ) { xUp = true ; nXA = i ; }
      if( !xDn && cClose < MA_Cur && pClose > MA_Prev ) { xDn = true ; nXB = i ; }   
      
      if( xUp && xDn ) { break ; }  
   }
   
 min      =  MathMin( nXA, nXB ) ;
 max      =  MathMax( nXA, nXB ) ; 
 
 xBars    =  max - min + 1; 
   
 nHH      =  Highest(NULL,period,MODE_HIGH,xBars,min); 
 nLL      =  Lowest(NULL,period,MODE_LOW,xBars,min); 
 
 
   
 close  = iClose(NULL,period,0) ;
 high   = iHigh(NULL,period,nHH) ;
 low    = iLow(NULL,period,nLL) ;
   
 if( close <= high && close >= low ) { ColorMarket = Blue  ; ranging = true ; tMarket = "Ranging "+ max  ; } else
 if( close >  high                 ) { ColorMarket = Green ; bull    = true ; tMarket = "Bull   " ; } else
 if( close <  low                  ) { ColorMarket = Red   ; bear    = true ; tMarket = "Bear   " ; }  
 
 
 

 
j       = 0 ;
lbl[j]  = ShortName+j;
lbl2[j] = tChartPeriod + " TRO MARKET";
DoShow(j, SteelBlue);



j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = tMarket;
//DoColor( range, pRange ) ; 
DoShow(j, ColorMarket); 

if( ranging )
{

j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Top      " + DoubleToStr(high,digits);
//DoColor( close, pOpen ) ; 
DoShow(j, Orange); 



j       = j + 1 ;
lbl[j]  = ShortName+j;
lbl2[j] = "Bottom   "  + DoubleToStr(low,digits);
//DoColor( close , open ) ; 
DoShow(j, Orange);  
 
}



ObjectCreate(Obj0001, OBJ_HLINE,0, CurTime(),high);
ObjectSet(Obj0001,OBJPROP_COLOR,HighColor);
ObjectSet(Obj0001,OBJPROP_STYLE,Line_Style);
ObjectSet(Obj0001,OBJPROP_WIDTH,Line_Width);

ObjectCreate(Obj0002, OBJ_HLINE,0, CurTime(),low);
ObjectSet(Obj0002,OBJPROP_COLOR,LowColor);
ObjectSet(Obj0002,OBJPROP_STYLE,Line_Style);
ObjectSet(Obj0002,OBJPROP_WIDTH,Line_Width);


if( ranging )
{
   if(ObjectFind(Obj0003)==-1) {ObjectCreate  (Obj0003,OBJ_VLINE,0,Time[nHH],0); }
   else                        {ObjectSet     (Obj0003,OBJPROP_TIME1,Time[nHH]); }

   if(ObjectFind(Obj0004)==-1) {ObjectCreate  (Obj0004,OBJ_VLINE,0,Time[nLL],0); }
   else                        {ObjectSet     (Obj0004,OBJPROP_TIME1,Time[nLL]); }
}



   return(0);
  }

//+------------------------------------------------------------------+
  
void DoShow( int u, color dsColor )
{
ObjectCreate(lbl[u],23,0,Time[0],PRICE_CLOSE);
ObjectSet(lbl[u], OBJPROP_CORNER, myCorner );
ObjectSet(lbl[u],OBJPROP_XDISTANCE, myChartX);
ObjectSet(lbl[u],OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(lbl[u],lbl2[u],myFontSize,myFont,dsColor);
n = n+20;

   return(0);
  }

//+------------------------------------------------------------------+
  
void DoColor( double c1, double c2 )
{

if(c1 > c2 ) { pColor = Lime ; } else {
if(c1 < c2 ) { pColor = Red ; } else { pColor = Yellow ; } } 

}  
  
//+------------------------------------------------------------------+
void drawLabel(string name,double lvl,color Color)
{
    if(ObjectFind(name) != 0)
    {
        ObjectCreate(name, OBJ_TEXT, 0, Time[10], lvl);
        ObjectSetText(name, name, myFontSize, myFont , EMPTY);
        ObjectSet(name, OBJPROP_COLOR, Color);
    }
    else
    {
        ObjectMove(name, 0, Time[10], lvl);
    }
}

//+------------------------------------------------------------------+
void drawLine(double lvl,string name, color Col,int type)
{
         if(ObjectFind(name) != 0)
         {
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            if(type == 1)
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            else
            ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
            
            ObjectSet(name, OBJPROP_COLOR, Col);
            ObjectSet(name,OBJPROP_WIDTH,1);
            
         }
         else
         {
            ObjectDelete(name);
            ObjectCreate(name, OBJ_HLINE, 0, Time[0], lvl,Time[0],lvl);
            
            if(type == 1)
            ObjectSet(name, OBJPROP_STYLE, STYLE_SOLID);
            else
            ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
            
            ObjectSet(name, OBJPROP_COLOR, Col);        
            ObjectSet(name,OBJPROP_WIDTH,1);
          
         }
}

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

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(
"high= ", DoubleToStr(high,digits), "\n",
"low= ", DoubleToStr(low,digits), "\n",


""); 


Comment(
"HH= ", DoubleToStr(HH,digits), "\n",
"myBars= ", DoubleToStr(myBars,digits), "\n",
"rHH= ", DoubleToStr(rHH,digits), "\n",

""); 


*/