//+------------------------------------------------------------------+
//|   TRO_TREND_METER                                                |
//|                                                                  |
//|   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_chart_window


extern int        myChartY    = 400 ;
extern bool       show.gauge  = true ;
extern bool       show.trend  = true ;
extern bool       show.open   = true ;

extern string     myFont      = "Courier" ; // "Verdana"
extern int        myFontSize  = 12 ; //9
extern color myColorH = LightSalmon ;
extern color myColor1 = LightSalmon ;
extern color myColor2 = LightSalmon ;
extern color myColor3 = LightSalmon ;
extern color myColor4 = LightSalmon ;
extern color myColor5 = Yellow ;
extern color myColor6 = Orange ;
extern color myColor7 = Blue ;

extern color myColorOpen = Yellow ;
extern color myColorUp   = Aqua ;
extern color myColorDn   = Magenta ;

extern string noteTimeNY   = " 14:00 server time = 05:00 mst" ; 
extern string noteTimeAU   = " 23:00 server time = 14:00 mst" ; 
extern string noteTimeLD   = " 09:00 server time = 00:00 mst" ; 
extern string myHHMM       = "14:00" ; // 05:00 am pst = 14:00 server time 


int ChartPeriod ;
int    digits, digits2;

color HLColor, CLColor ;
color color_X01 ;
color color_X02 ;
color color_X03 ;
color color_X04 ;
color color_X05 ;
color color_X06 ;
color color_X07 ;


double X01,X02,X03,X04,X05,X06, X07, point , DailyOpen;

string dHead01, dHead02;
string dLabel01, dValue01;
string dLabel02, dValue02;
string dLabel03, dValue03;
string dLabel04, dValue04;
string dLabel05, dValue05;
string dLabel06, dValue06;
string dLabel07, dValue07;

string symbol, tChartPeriod, sX02 ;  

int      cd ;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
  
   ChartPeriod = Period();
   

   tChartPeriod =  TimeFrameToString(ChartPeriod) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;   
   point        =  Point ;
   
   if(digits == 5 || digits == 3) { digits2 = 0 ; point = point * 10 ; } else { digits2 = 0 ; }

if( show.trend ) {
       
    ObjectCreate("HLtoC",OBJ_TREND,0,0,0,0,0);
    ObjectSet("HLtoC",OBJPROP_COLOR,Yellow); 
    ObjectSet("HLtoC",OBJPROP_RAY,false);    
    ObjectSet("HLtoC",OBJPROP_WIDTH,2); 
         
    ObjectCreate("HtoL",OBJ_TREND,0,0,0,0,0);     
    ObjectSet("HtoL",OBJPROP_RAY,false);     
    ObjectSet("HtoL",OBJPROP_WIDTH,2);    
} 

 dHead01="trnh01"+ tChartPeriod ;  dHead02="trn202"+ tChartPeriod ;     
    
 dLabel01="trn01"+ tChartPeriod ;  dValue01="trnv01"+ tChartPeriod ; 
 dLabel02="trn02"+ tChartPeriod ;  dValue02="trnv02"+ tChartPeriod ; 
 dLabel03="trn03"+ tChartPeriod ;  dValue03="trnv03"+ tChartPeriod ; 
 dLabel04="trn04"+ tChartPeriod ;  dValue04="trnv04"+ tChartPeriod ; 
 dLabel05="trn05"+ tChartPeriod ;  dValue05="trnv05"+ tChartPeriod ; 
 dLabel06="trn06"+ tChartPeriod ;  dValue06="trnv06"+ tChartPeriod ; 
 dLabel07="trn07"+ tChartPeriod ;  dValue07="trnv07"+ tChartPeriod ; 
      
if(  show.open ) 
{  
    ObjectCreate("OpenL",OBJ_TREND,0,0,0,0,0);
    ObjectSet("OpenL",OBJPROP_COLOR,myColorOpen); 
    ObjectSet("OpenL",OBJPROP_WIDTH,2); 
}
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
   ObjectsDeleteAll(0);
   
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {

if( ChartPeriod >= PERIOD_D1 ) { Comment ("***error - must be less than PERIOD_D1" ) ; return (-1) ; } 

   double low,OP,CL,HI,LO;
   int rt1, i;
   
   double MD, PM, L1,xLow,H1,high,xHigh, cVal, eVal ;
   int D0,D1,st1, tt;
   
   
   DoDailyOpen(symbol) ;
   
 
    
   int shift=cd;   
   
   int hh = Highest(NULL,0,MODE_HIGH,shift+1,0);
   int ll = Lowest(NULL,0,MODE_LOW,shift+1,0); 
      
   double xRES = iHigh(NULL,0,hh) ;  
   double xSUP = iLow(NULL,0,ll) ;      

    OP = iOpen(NULL,ChartPeriod ,shift) ; 
    CL = iClose(NULL,ChartPeriod ,0) ; 
    HI = iHigh(NULL,ChartPeriod ,0) ; 
    LO = iLow(NULL,ChartPeriod ,0) ; 
 
if( ll < hh ) { tt = ll ; cVal = xSUP ; eVal = LO ; HLColor = myColorDn  ; color_X01 = myColorUp ; color_X02 = myColorUp ; } 
else          { tt = hh ; cVal = xRES ; eVal = HI ; HLColor = myColorUp ; color_X01 = myColorDn  ; color_X02 = myColorDn ; } 
 

if( show.trend ) {

    ObjectCreate("HLtoC",OBJ_TREND,0,0,0,0,0);
    ObjectSet("HLtoC",OBJPROP_COLOR,Orange); 
    ObjectSet("HLtoC",OBJPROP_RAY,false);    
    ObjectSet("HLtoC",OBJPROP_WIDTH,2); 
         
    ObjectCreate("HtoL",OBJ_TREND,0,0,0,0,0);     
    ObjectSet("HtoL",OBJPROP_RAY,false);     
    ObjectSet("HtoL",OBJPROP_WIDTH,2); 

   ObjectMove("HtoL",0,Time[ll],xSUP); // ("HtoL",1,Time[st1],s1);
   ObjectMove("HtoL",1,Time[hh],xRES); //  ("HtoL",0,Time[st2],s2)

   ObjectSet("HtoL",OBJPROP_COLOR,HLColor);  
    
   ObjectMove("HLtoC",0,Time[tt],cVal);
   ObjectMove("HLtoC",1,Time[0],eVal);
}      
  
X01    = MathAbs(CL - cVal) / point ;
X03    = xRES ;
X04    = xSUP ;
X05    = OP ;
X06    = (xRES - CL) / point ;
X07    = (CL - xSUP) / point ;

sX02   = TimeToStr(Time[tt]) ;       
       
if(show.gauge) { DoGauge();  }     
   
   
if(  show.open ) 
{  
    ObjectCreate("OpenL",OBJ_TREND,0,0,0,0,0);
    ObjectSet("OpenL",OBJPROP_COLOR,myColorOpen); 
    ObjectSet("OpenL",OBJPROP_WIDTH,2); 
    ObjectSet("HLtoC",OBJPROP_RAY,false);  
    ObjectMove("OpenL",0,Time[shift],OP);
    ObjectMove("OpenL",1,Time[0],OP);
}
 
 
           
return(0);
}

//+------------------------------------------------------------------+
 
void DoGauge()
{ 

string StrH   = "Trend Meter   " ;
string StrX01 = "Pips    " ; 
string StrX02 = "Time   " ; 
string StrX03 = "High    " ; 
string StrX04 = "Low     " ; 
string StrX05 = "Open    " ; 
string StrX06 = "Dist HI " ; 
string StrX07 = "Dist LO " ; 


   int WindowToUse;
   int Corner_line, Xdist_line;
   int Corner_text, Corner_vSRFP, Xdist_text, Xdist_vSRFP;
   int Ydist_line5;
   int Corner_pivots, Xdist_pivots, Ydist_pivots;
   int Ydist_X01, Ydist_X02, Ydist_X03, Ydist_X04, Ydist_X05, Ydist_X06, Ydist_X07, Ydist_X08, Ydist_X09, Ydist_X10 ;
  
   int YdistInc ;
   
    WindowToUse =  0;
    Corner_line = 1;
    Corner_text = 1; Corner_vSRFP = 1;
    Xdist_line = 21; Xdist_text = 93; Xdist_vSRFP = 23;
    Corner_pivots = 1; Xdist_pivots = 21; 
    
    Ydist_pivots = myChartY;  
    Ydist_line5 = Ydist_pivots + 11; YdistInc = 15 ;
    
    Ydist_X01 = Ydist_pivots + 25; 
    Ydist_X02 = Ydist_X01 + YdistInc; 
    Ydist_X03 = Ydist_X01 + YdistInc*2; 
    Ydist_X04 = Ydist_X01 + YdistInc*3; 
    Ydist_X05 = Ydist_X01 + YdistInc*4;
    Ydist_X06 = Ydist_X01 + YdistInc*5;    
    Ydist_X07 = Ydist_X01 + YdistInc*6;
    Ydist_X08 = Ydist_X01 + YdistInc*7;    
    Ydist_X09 = Ydist_X01 + YdistInc*8;    
    Ydist_X10 = Ydist_X01 + YdistInc*9;    

//+------------------------------------------------------------------+ 
   ObjectCreate(dHead01, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dHead01,StrH,myFontSize, myFont, myColorH);
   ObjectSet(dHead01, OBJPROP_CORNER, Corner_pivots);
   ObjectSet(dHead01, OBJPROP_XDISTANCE, Xdist_pivots);
   ObjectSet(dHead01, OBJPROP_YDISTANCE, Ydist_pivots);
   
   ObjectCreate(dHead02, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dHead02,"----------------",myFontSize, myFont, myColorH);
   ObjectSet(dHead02, OBJPROP_CORNER, Corner_line);
   ObjectSet(dHead02, OBJPROP_XDISTANCE, Xdist_line);
   ObjectSet(dHead02, OBJPROP_YDISTANCE, Ydist_line5);

//+------------------------------------------------------------------+    
   ObjectCreate(dLabel01, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel01,StrX01,myFontSize, myFont, color_X01 );
   ObjectSet(dLabel01, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel01, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel01, OBJPROP_YDISTANCE, Ydist_X01);
   
   ObjectCreate(dValue01, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue01," "+DoubleToStr(X01,digits2),myFontSize, myFont, color_X01);
   ObjectSet(dValue01, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue01, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue01, OBJPROP_YDISTANCE, Ydist_X01);

//+------------------------------------------------------------------+    
/* 
   ObjectCreate(dLabel02, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel02,StrX02,myFontSize, myFont, myColor2 );
   ObjectSet(dLabel02, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel02, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel02, OBJPROP_YDISTANCE, Ydist_X02);
*/
  
   ObjectCreate(dValue02, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue02," "+ sX02 ,myFontSize, myFont, color_X02);
   ObjectSet(dValue02, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue02, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue02, OBJPROP_YDISTANCE, Ydist_X02);

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

   ObjectCreate(dLabel03, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel03,StrX03,myFontSize, myFont, myColor3 );
   ObjectSet(dLabel03, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel03, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel03, OBJPROP_YDISTANCE, Ydist_X03);

  
   ObjectCreate(dValue03, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue03," "+DoubleToStr(X03,digits) ,myFontSize, myFont, myColor3);
   ObjectSet(dValue03, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue03, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue03, OBJPROP_YDISTANCE, Ydist_X03);

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

   ObjectCreate(dLabel04, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel04,StrX04,myFontSize, myFont, myColor4 );
   ObjectSet(dLabel04, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel04, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel04, OBJPROP_YDISTANCE, Ydist_X04);

  
   ObjectCreate(dValue04, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue04," "+DoubleToStr(X04,digits) ,myFontSize, myFont, myColor4);
   ObjectSet(dValue04, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue04, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue04, OBJPROP_YDISTANCE, Ydist_X04);

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

   ObjectCreate(dLabel05, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel05,StrX05,myFontSize, myFont, myColor5 );
   ObjectSet(dLabel05, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel05, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel05, OBJPROP_YDISTANCE, Ydist_X05);

  
   ObjectCreate(dValue05, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue05," "+DoubleToStr(X05,digits) ,myFontSize, myFont, myColor5);
   ObjectSet(dValue05, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue05, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue05, OBJPROP_YDISTANCE, Ydist_X05);
 
//+------------------------------------------------------------------+ 


   ObjectCreate(dLabel06, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel06,StrX06,myFontSize, myFont, myColor6 );
   ObjectSet(dLabel06, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel06, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel06, OBJPROP_YDISTANCE, Ydist_X06);

  
   ObjectCreate(dValue06, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue06," "+DoubleToStr(X06,digits2) ,myFontSize, myFont, myColor6);
   ObjectSet(dValue06, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue06, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue06, OBJPROP_YDISTANCE, Ydist_X06);
 
//+------------------------------------------------------------------+ 

   ObjectCreate(dLabel07, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dLabel07,StrX07,myFontSize, myFont, myColor7 );
   ObjectSet(dLabel07, OBJPROP_CORNER, Corner_text);
   ObjectSet(dLabel07, OBJPROP_XDISTANCE, Xdist_text);
   ObjectSet(dLabel07, OBJPROP_YDISTANCE, Ydist_X07);

  
   ObjectCreate(dValue07, OBJ_LABEL, WindowToUse, 0, 0);
   ObjectSetText(dValue07," "+DoubleToStr(X07,digits2) ,myFontSize, myFont, myColor7);
   ObjectSet(dValue07, OBJPROP_CORNER, Corner_vSRFP);
   ObjectSet(dValue07, OBJPROP_XDISTANCE, Xdist_vSRFP);
   ObjectSet(dValue07, OBJPROP_YDISTANCE, Ydist_X07);
 
//
} // DoGauge
  
//+------------------------------------------------------------------+ 

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

 

void DoDailyOpen(string doSymbol) 
{

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

//datetime r=(TimeCurrent()/day)*day;
//datetime r=(var1/day)*day;

         cd=iBarShift(NULL,0,r,FALSE);
datetime r2=r-day;


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

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


DailyOpen = iOpen(doSymbol,0,cd);    


}


//+------------------------------------------------------------------+ 
/*

   int hh2 = Highest(NULL,0,MODE_HIGH,2,0);
   int ll2 = Lowest(NULL,0,MODE_LOW,2,0);  
        
Comment(
"hh2 " , hh2 , "\n" , 
"ll2 " , ll2 , "\n" , 
"cd " , cd , "\n" , 
"pair " , symbol , "\n" , 
"DailyOpen " , DoubleToStr(DailyOpen,Digits) , "\n" ,
"hh " , hh , "\n" , 
"ll " , ll , "\n" , 
"tt " , tt , "\n" , 
"") ; 
 
 high = -1 ;
 low = 999999999 ;
  
 
 D0 = TimeDay( Time[0] ) ; 
 
 CL = iClose(NULL,ChartPeriod ,0) ; 
   
 for( i=0; i<=1440; i++)
 {
 if( TimeDay( Time[i] ) != D0 ) { break ; }
 
 xHigh = iHigh(NULL,ChartPeriod,i) ;
 xLow = iLow(NULL,ChartPeriod,i) ;
 
 if ( xHigh > high ) { high  = xHigh ; rt1 = i ; }
 if ( xLow < low  ) { low   = xLow ; st1 = i ; }  
 
 } // for(i=0...
  
      

*/



