//+------------------------------------------------------------------+ 
//|  _TRO_RANGE_InfoH1                                               | 
//|                                                                  | 
//|   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 4 
#property  indicator_color1  Red 
#property  indicator_color2  Green 
#property  indicator_color3  Orange 


//---- indicator parameters  
//extern int   Threshold       = 20; 

extern int win = 1;
extern int price.x.offset= 250;//250
extern int price.y.offset= -160;//-190
extern bool show.info=true;

extern int   MaxBars         = 960;
extern int   MA_Period       = 200;




extern color CurrentBarColor = LightGray ; 
extern color ThresholdColor  = LimeGreen; 
extern bool  ShowLabel       = false; 



//---- indicator buffers 
double     RANGEBuffer[]; 
double     RANGEBuffer1[]; 
double     RANGEBuffer2[]; 
double     TempBuffer[]; 

double     MaxRange = 0 ; 
double     MinRange = 0 ; 

 int   Chart_Period    = PERIOD_H1;


//---- buffers
double low[];
double high[];

int uPeriod ;

//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
  { 

//---- drawing settings 
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2); 
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,2); 
   SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2); 
   SetIndexStyle(3,DRAW_NONE);    

//---- indicator buffers mapping 

   SetIndexBuffer(0,RANGEBuffer); 
   SetIndexBuffer(1,RANGEBuffer1);  
   SetIndexBuffer(2,RANGEBuffer2); 
   SetIndexBuffer(3,TempBuffer); // non visualizzato 
    
//---- name for DataWindow and indicator subwindow label 
   IndicatorShortName("TRO RANGE INFO H1"); 
//   SetIndexLabel(0,"TRO ABOVE"); 
//   SetIndexLabel(1,"TRO BELOW"); 
//   SetIndexLabel(2,"TRO MOVING AVERAGE"); 
    
//---- initialization done 
   return(0); 
  } 

int deinit() 
{ 
   ObjectDelete("CurrentBarValue"); 
   ObjectDelete("LabelCurrentBarValue");  
   ObjectDelete("Threshold"); 
   
   
   ObjectDelete("RNG530");
   ObjectDelete("RNG529");
   ObjectDelete("RNG529B");
   ObjectDelete("RNG531");
   ObjectDelete("RNG532");
   ObjectDelete("RNG534");   
}  

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

    MaxRange = 0 ; 
    MinRange = 999999 ; 

   double HighVal,LowVal; 
   int limit; 
   int counted_bars=IndicatorCounted(); 
    
//---- last counted bar will be recounted 
   if(counted_bars>0) counted_bars--; 
   if(counted_bars<0) return(-1); 
   
//   limit=Bars-counted_bars; 
  
   limit = MaxBars ;  

//---- RANGE counted in the 1-st buffer 
   for(int i=0; i<limit; i++) 
   { 
   

 
     HighVal  = iHigh(NULL,Chart_Period,i);        
     LowVal   = iLow(NULL,Chart_Period,i); 

      
     TempBuffer[i] = (HighVal-LowVal)/Point; 

//     if( TempBuffer[i] > MaxRange ) {MaxRange = TempBuffer[i] ; }

//     if( TempBuffer[i] < MinRange ) {MinRange = TempBuffer[i] ; }  
 
//     if( TempBuffer[i] > high[i+1] ) {high[i] = TempBuffer[i] ; } else { high[i] = high[i+1] ; }

//     if( TempBuffer[i] < low[i+1] ) { low[i] = TempBuffer[i] ; } else { low[i] = low[i+1] ; }
  
 
       
     /* 
     // Draw Threshold line  
     ObjectDelete("Threshold"); 
     ObjectCreate("Threshold", OBJ_HLINE, win_idx, Time[0], Threshold); 
     ObjectSet("Threshold", OBJPROP_COLOR, ThresholdColor); 
     ObjectSet("Threshold", OBJPROP_STYLE, STYLE_SOLID); 
     ObjectSet("Threshold", OBJPROP_WIDTH, 1); 
     ObjectSet("Threshold", OBJPROP_BACK, true); 
      
     // Label for Threshold 
     if ( ShowLabel == true) 
     { 
      ObjectDelete("LabelThreshold"); 
      ObjectCreate("LabelThreshold", OBJ_TEXT, win_idx, Time[0], Threshold); 
      ObjectSet("LabelThreshold", OBJPROP_COLOR, ThresholdColor); 
      ObjectSetText("LabelThreshold", "Threshold",8); 
      ObjectSet("LabelThreshold", OBJPROP_ANGLE, 0); 
     } 
     */ 
   } 

   // Calculate MA of Histograms 
   for(int j=0; j<limit; j++) 
   { 
     RANGEBuffer2[j]  = iMAOnArray(TempBuffer,0,MA_Period,0,MODE_SMA,j); 

     if( TempBuffer[j] > MaxRange ) {MaxRange = TempBuffer[j] ; }
     if( TempBuffer[j] < MinRange ) {MinRange = TempBuffer[j] ; }  

   } 
  
  
    
   // Paint Histograms 
   for ( i=0; i<limit; i++) 
   { 
    if ( TempBuffer[i] >= RANGEBuffer2[i]) { RANGEBuffer1[i] = TempBuffer[i]; } 
    else { RANGEBuffer[i] = TempBuffer[i]; }    
   }  
   
   // Draw Current Bar line value 
   int win_idx=WindowFind("TRO PLOT ACCUMULATOR"); 
   
   double oRange = (iHigh(NULL,Chart_Period,0)-iLow(NULL,Chart_Period,0))/Point ;
   
   
/*    
   ObjectDelete("CurrentBarValue"); 
   ObjectCreate("CurrentBarValue", OBJ_HLINE, win_idx, Time[0], (iHigh(NULL,0,0)-iLow(NULL,0,0))/Point); 
   ObjectSet("CurrentBarValue", OBJPROP_COLOR, CurrentBarColor); 
   ObjectSet("CurrentBarValue", OBJPROP_STYLE, STYLE_SOLID); 
   ObjectSet("CurrentBarValue", OBJPROP_WIDTH, 1); 
   ObjectSet("CurrentBarValue", OBJPROP_BACK, true); 
*/     
   // Label per Current Bar Line 
   if ( ShowLabel == true ) 
   { 
   
   
   
     ObjectDelete("LabelCurrentBarValue"); 
     ObjectCreate("LabelCurrentBarValue", OBJ_TEXT, win_idx, Time[0], oRange ); 
     ObjectSet("LabelCurrentBarValue", OBJPROP_COLOR, CurrentBarColor); 
     ObjectSetText("LabelCurrentBarValue", "Current Bar value",8); 
     ObjectSet("LabelCurrentBarValue", OBJPROP_ANGLE, 0);      
   }    
//---- done 


 
//+------------------------------------------------------------------+ 
if (show.info) {    

string tPeriod = " "+TimeFrameToString( Chart_Period ) ;

if ( oRange > RANGEBuffer2[0] ) { color RangeColor = ThresholdColor ; } else { RangeColor = CurrentBarColor   ; }

   ObjectCreate("RNG530", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG530","RANGE", 30, "Arial Bold", Gray);
   ObjectSet("RNG530", OBJPROP_CORNER, 0);
   ObjectSet("RNG530", OBJPROP_XDISTANCE, 760+price.x.offset);
   ObjectSet("RNG530", OBJPROP_YDISTANCE, 160+price.y.offset);
 
   ObjectCreate("RNG529", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG529",DoubleToStr(MaxRange,0), 15, "Arial Bold", Lime);
   ObjectSet("RNG529", OBJPROP_CORNER, 0);
   ObjectSet("RNG529", OBJPROP_XDISTANCE, 770+price.x.offset);
   ObjectSet("RNG529", OBJPROP_YDISTANCE, 197+price.y.offset);
   
   ObjectCreate("RNG529b", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG529b",DoubleToStr( MinRange ,0), 15, "Arial Bold", Crimson);
   ObjectSet("RNG529b", OBJPROP_CORNER, 0);
   ObjectSet("RNG529b", OBJPROP_XDISTANCE, 770+price.x.offset);
   ObjectSet("RNG529b", OBJPROP_YDISTANCE, 260+price.y.offset);                 
                     
   ObjectCreate("RNG531", OBJ_LABEL, win, 0, 0);//HiLow LABEL
   ObjectSetText("RNG531",DoubleToStr(oRange,0), 40, "Arial Bold", RangeColor );
   ObjectSet("RNG531", OBJPROP_CORNER, 0);
   ObjectSet("RNG531", OBJPROP_XDISTANCE, 800+price.x.offset); // 765
   ObjectSet("RNG531", OBJPROP_YDISTANCE, 210+price.y.offset); 
           
 
   ObjectCreate("RNG532", OBJ_LABEL, win, 0, 0);
   ObjectSetText("RNG532",DoubleToStr(RANGEBuffer2[0],2), 15, "Arial Bold", Orange);
   ObjectSet("RNG532", OBJPROP_CORNER, 0);
   ObjectSet("RNG532", OBJPROP_XDISTANCE, 860+price.x.offset);
   ObjectSet("RNG532", OBJPROP_YDISTANCE, 197+price.y.offset);
                 
   ObjectCreate("RNG534", OBJ_LABEL, win, 0, 0);
   ObjectSetText("RNG534",DoubleToStr( MaxBars ,0), 15, "Arial Bold", Silver);
   ObjectSet("RNG534", OBJPROP_CORNER, 0);
   ObjectSet("RNG534", OBJPROP_XDISTANCE, 860+price.x.offset);
   ObjectSet("RNG534", OBJPROP_YDISTANCE, 260+price.y.offset);    
    
/*   
   ObjectCreate("RNG534a", OBJ_LABEL, win, 0, 0);
   ObjectSetText("RNG534a",DoubleToStr(s,Digits-4), 15, "Arial Bold", Yellow);
   ObjectSet("RNG534a", OBJPROP_CORNER, 0);
   ObjectSet("RNG534a", OBJPROP_XDISTANCE, 900+price.x.offset);
   ObjectSet("RNG534a", OBJPROP_YDISTANCE, 280+price.y.offset);
   
   ObjectCreate("RNG534b", OBJ_LABEL, win, 0, 0);
   ObjectSetText("RNG534b",DoubleToStr(m,Digits-4)+" : "+DoubleToStr(s,Digits-4), 15, "Arial Bold", Yellow);
   ObjectSet("RNG534b", OBJPROP_CORNER, 0);
   ObjectSet("RNG534b", OBJPROP_XDISTANCE, 870+price.x.offset);
   ObjectSet("RNG534b", OBJPROP_YDISTANCE, 280+price.y.offset); 
   
   ObjectCreate("t634b", OBJ_LABEL, win, 0, 0);
   ObjectSetText("t634b","Time Left", 15, "Arial Bold", Gray);
   ObjectSet("t634b", OBJPROP_CORNER, 0);
   ObjectSet("t634b", OBJPROP_XDISTANCE, 765+price.x.offset);
   ObjectSet("t634b", OBJPROP_YDISTANCE, 280+price.y.offset);   
*/
//+------------------------------------------------------------------+  
   WindowRedraw();    
} 
   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="MN1";
   }
   return(tfs);
}