//+------------------------------------------------------------------+ 
//|   $ROLLING_RANGE                                                 | 
//|                                                                  | 
//|   Copyright © 2008, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 43575, TUCSON, AZ 85733                                 |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|                                                                  |
//|   therumpldone@gmail.com                                         |  
//+------------------------------------------------------------------+ 

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

//---- indicator settings 
#property indicator_separate_window
#property indicator_buffers 0
//#property indicator_minimum 0
//#property indicator_maximum 1



//---- indicator parameters  
 

//extern int win = 1;
extern int price.x.offset= 0 ;//250
extern int price.y.offset= -160 ;//-160

extern bool show.arrow = true ;
extern bool show.value = true ;
//extern int  myDecimals = 2 ;

extern string myFont       = "Arial Bold" ;
extern int    myFontSize   = 20 ;
extern int    myArrowSize  = 40 ;
extern int    myDigitSize  = 20 ;
extern int    xStart       = -600 ;
extern int    xOffSet      = 100 ;

extern string Ind_Name_1 ="$ROLLING_RANGE";
extern string myName     ="$ROLLING_RANGE";
extern color  myIndColor = DarkGray ;

//extern double iParm1 = 0;
//extern double iParm2 = 0;

extern double iParm3 = -100;
extern double iParm4 = -100;
extern double iParm5 = -100;
extern double iParm6 = -100;
extern double iParm7 = -100;
extern double iParm8 = -100;
extern int    iMode  = 0;

extern int    myPeriod_1  = 5 ;
extern int    myPeriod_2  = 15 ;
extern int    myPeriod_3  = 20 ;
extern int    myPeriod_4  = 30 ;
extern int    myPeriod_5  = 45 ;
extern int    myPeriod_6  = 60 ;



extern bool   UseValue = false;
extern double N_value  = -999;
extern double NE_value = 2;
extern double E_value  = 3;
extern double SE_value = 1;
extern double S_value  = -999;

extern color N_color  = Green;
extern color NE_color = Green;
extern color E_color  = Yellow;
extern color SE_color = Red;
extern color S_color  = Red;

//---- buffers


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


string tObjName01   ;
string tObjName02   ;
string tObjName03   ;
string tObjName04 , tObjName05, IndHead ;

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

double mid      ; 
double mid1    ; 


double pCount   ;

int   HighDir, LowDir, MidDir, Mid1Dir, CloseDir, ClOpDir ;
color HighColor, LowColor, MidColor, Mid1Color, CloseColor ;

string PriceDir   ;

string tPeriod   ; 

double cVal, pVal ;

//+------------------------------------------------------------------+
int      totalLabels;
string   labelNames;
int      corner;
int      window; 
string   shortName ;
int      win ;
//+------------------------------------------------------------------+

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="MN1";
   }
   return(tfs);
}

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

color fColor( double i)
{
color rColor ;

    if ( i == 1 ) rColor = NE_color  ; else {
    if ( i == 0 ) rColor = E_color  ; else {     
    rColor = SE_color ; } } 

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

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

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


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


string fCompass( double i )
{
string rCompass ;


    if ( i == 1 ) rCompass = Arrow2  ; else {
    if ( i == 0 ) rCompass = Arrow3  ; else {      
    rCompass = Arrow5 ; } } 
    
return ( rCompass );
}


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

int init() 
  { 
   shortName  = MakeUniqueName("CI ","");
   labelNames = shortName;
   IndicatorShortName(shortName);
   totalLabels     = 0;


   return(0); 
  } 

int deinit() 
{ 
   while (totalLabels>0) { ObjectDelete(StringConcatenate(labelNames,totalLabels)); totalLabels--;}
  
DoDelObj( PERIOD_M1 ) ;
DoDelObj( PERIOD_M5 ) ;
DoDelObj( PERIOD_M15 ) ;  
DoDelObj( PERIOD_H1 ) ;
DoDelObj( PERIOD_H4 ) ;
DoDelObj( PERIOD_D1 ) ;
DoDelObj( PERIOD_W1 ) ;
DoDelObj( PERIOD_MN1 ) ;

   
}  

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

int xStart2 = xStart + xOffSet ;
int xStart3 = xStart + xOffSet*2 ;
int xStart4 = xStart + xOffSet*3 ;
int xStart5 = xStart + xOffSet*4 ;
int xStart6 = xStart + xOffSet*5 ;
int xStart7 = xStart + xOffSet*6 ;
int xStart8 = xStart + xOffSet*7 ;

win =    WindowFind(shortName);

bool sFirst = true ;

if ( sFirst ) {
sFirst = false ;
ObjectDelete(tObjName04);

ObjectsDeleteAll( win, 21);
ObjectsDeleteAll( win, 22);
ObjectsDeleteAll( win, 23);
}

/*

mladens way of deleting objects

for(int i = ObjectsTotal() - 1; i >= 0; i--)
{
    string label = ObjectName(i);
    if(StringSubstr(label, 0, 15) == "StochDivergence")  ObjectDelete(label);  
}

*/

if( iParm3 !=-100) { IndHead =  myName ; }
else { IndHead =  myName ; }

DoHead(IndHead );

DoWork( myPeriod_1 ) ;
DoShow( myPeriod_1, xStart ) ;

DoWork( myPeriod_2 ) ;
DoShow( myPeriod_2, xStart2 ) ;

DoWork( myPeriod_3 ) ;
DoShow( myPeriod_3, xStart3 ) ;

DoWork( myPeriod_4 ) ;
DoShow( myPeriod_4, xStart4 ) ;

DoWork( myPeriod_5 ) ;
DoShow( myPeriod_5, xStart5 ) ;

DoWork( myPeriod_6 ) ;
DoShow( myPeriod_6, xStart6 ) ;

WindowRedraw();    
 


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

void DoWork( int myPeriodx  )  
{

int myPeriod = 1 ;
/*
if ( Ind_Name_1=="tMid"  ) { cVal = (iHigh(NULL, myPeriod, 0 ) + iLow(NULL, myPeriod, 0 ) ) * 0.5  ; 
                             pVal = (iHigh(NULL, myPeriod, 1 ) + iLow(NULL, myPeriod, 1 ) ) * 0.5  ;  break;}
*/

while(true)
{

iParm3 = myPeriodx ;
if(iParm3==-100 ) {
               cVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iMode,0);
               pVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iMode,1);
               break ; }
                         
if(iParm3!=-100 && iParm4==-100 && iParm5==-100 && iParm6==-100 && iParm7==-100) {
               cVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iMode,0);
               pVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iMode,1);               
               break ; }
               
if(iParm3!=-100 && iParm4!=-100 && iParm5==-100 && iParm6==-100 && iParm7==-100) {
               cVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iMode,0);
               pVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iMode,1);             
               break ; }
            
if(iParm3!=-100 && iParm4!=-100 && iParm5!=-100 && iParm6==-100 && iParm7==-100) {
               cVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iParm5,iMode,0);
               pVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iParm5,iMode,1);
               break ; }                        
            
            
if(iParm3!=-100 && iParm4!=-100 && iParm5!=-100 && iParm6!=-100 && iParm7==-100) {
               cVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iParm5,iParm6,iMode,0);
               pVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iParm5,iParm6,iMode,1);
               break ; }                          
            
            
if(iParm3!=-100 && iParm4!=-100 && iParm5!=-100 && iParm6!=-100 && iParm7!=-100) {
               cVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iParm5,iParm6,iParm7,iMode,0);
               pVal = iCustom(NULL,myPeriod,Ind_Name_1,iParm3,iParm4,iParm5,iParm6,iParm7,iMode,1);
               break ; }
                       
break ;                                          
}

                                                                                                                     
pCount  = fDirection( cVal , myPeriodx  ) ;

CloseColor = fColor( pCount ) ;


PriceDir = fCompass( pCount ) ;


}

//+------------------------------------------------------------------+ 
void DoDelObj( int lPeriod  )  
{

tObjName01 = labelNames +lPeriod +  "XTN950" ;
tObjName02 = labelNames +lPeriod +  "XTN951" ;
tObjName03 = labelNames +lPeriod +  "XTN952" ;
//tObjName04 = labelNames +  "XIN900" ;
tObjName05 = labelNames +lPeriod +  "XTN955" ;

ObjectDelete(tObjName01);
ObjectDelete(tObjName02);
ObjectDelete(tObjName03);
ObjectDelete(tObjName04);
ObjectDelete(tObjName05);
}

//+------------------------------------------------------------------+ 
void DoShow( int lPeriod , int xInc)  
{

tObjName01 = labelNames +lPeriod +  "XTN950" ;
tObjName02 = labelNames +lPeriod +  "XTN951" ;
tObjName03 = labelNames +lPeriod +  "XTN952" ;
tObjName05 = labelNames +lPeriod +  "XTN955" ;


tPeriod  =  lPeriod  ;

   ObjectCreate(tObjName01, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName01, tPeriod, myFontSize, myFont, CloseColor );
   ObjectSet(tObjName01, OBJPROP_CORNER, 0);
   ObjectSet(tObjName01, OBJPROP_XDISTANCE,  xInc+980+price.x.offset); // 760
   ObjectSet(tObjName01, OBJPROP_YDISTANCE, 175+price.y.offset); // 160

int sInc = 0 ;

if(show.arrow){

sInc = 50 ;
   
   ObjectCreate(tObjName03, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, PriceDir , myArrowSize , "Wingdings" , CloseColor );
   ObjectSet(tObjName03, OBJPROP_CORNER, 0);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, xInc+980+price.x.offset); // 765
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 210+price.y.offset); 
   
} //if(show.arrow)


if(show.value){
    

    
   ObjectCreate(tObjName05, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName05, DoubleToStr(cVal,0) ,myDigitSize, myFont , CloseColor );
   ObjectSet(tObjName05, OBJPROP_CORNER, 0);
   ObjectSet(tObjName05, OBJPROP_XDISTANCE, xInc+980+price.x.offset); // 765
   ObjectSet(tObjName05, OBJPROP_YDISTANCE, sInc+210+price.y.offset); 
} //if(show.value)



}

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

void DoHead( string dHead  )  
{

tObjName04 = labelNames +  "XIN900" ;

   ObjectCreate(tObjName04, OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName04, dHead, myFontSize, myFont, myIndColor );
   ObjectSet(tObjName04, OBJPROP_CORNER, 0);
   ObjectSet(tObjName04, OBJPROP_XDISTANCE,  00+price.x.offset); // 760
   ObjectSet(tObjName04, OBJPROP_YDISTANCE, 210+price.y.offset); // 160
}

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


// string next() { totalLabels++; return(totalLabels); }  

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

 
string MakeUniqueName(string first, string rest)
{
   string result = first+(MathRand()%1001)+rest;

   while (WindowFind(result)> 0)
          result = first+(MathRand()%1001)+rest;
   return(result);
}