//+------------------------------------------------------------------+ 
//|   TRO_OpenDiff                                                   | 
//|                                                                  | 
//|   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   = 500 ;
extern int myChartY   = 0 ;
extern int myCorner   = 0 ;
extern string myFont  = "Courier" ;
extern int myFontSize = 12 ;
 
extern int myPeriod = 0 ;

//extern int myRows     = 10 ;
//extern int myBars     = 240 ;
extern color colorHead  = Orange ;

extern string noteTimeNY   = " 14:00 server time = 05:00 mst" ; 
extern string noteTimeTY   = " 21:00 server time = 12:00 mst" ; 
extern string noteTimeAU   = " 23:00 server time = 14:00 mst" ; 
extern string noteTimeLD   = " 09:00 server time = 00:00 mst" ; 
extern string myHHMM1      = "14:00" ;  
extern string myMessage1   = "NY" ; 
extern string myHHMM2      = "21:00" ; 
extern string myMessage2   = "TY" ; 
extern string myHHMM3      = "23:00" ;  
extern string myMessage3   = "AU" ; 
extern string myHHMM4      = "09:00" ; 
extern string myMessage4   = "LD" ; 




double Hi, Li, C;

double prev_high, prev_low, prev_close, D, p, value ;
int h  ;
string lbl[15], lbl2[15];
int n, j, i, shift, stop, nHH, nLL  ;

color pColor = Blue, ColorOpen ;

string symbol, tChartPeriod, ShortName, sDiff , sDailyOpen, text ;  

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 , DailyOpen, Diff , WeeklyOpen, MonthlyOpen;   
 
 
int     ind_bufferOp[], Stats[11], StatsR[11], StatsG[11],scount, yy,mm,dd;

 
bool red0, red1, green0, green1 ;
      
      
//+------------------------------------------------------------------+

int init()
  {
   if(myPeriod == 0) { period = Period() ; } else { period = myPeriod ; }  
      
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   ShortName    =  "OpenDiff"+symbol+period  ;
   win          =  0 ;
   
 
   
   return(0);
  }
  
//+------------------------------------------------------------------+
int deinit()
  {
 
ObjectsDeleteAll();
 
   return(0);
  }
  
//+------------------------------------------------------------------+
int start()
{

n       = 10;
 
j       = 0 ;

text    = TimeToStr(TimeCurrent() ,TIME_SECONDS) ;

lbl[j]  = ShortName+j;
lbl2[j] = StringSubstr(text, 0, 5)  + " " + symbol + " Open +/-" ;
DoShow(j, colorHead);



j = j + 1 ;

DoDailyOpen(  myHHMM1  )  ; 
DoWork( period ) ;
lbl[j]  = ShortName+j;
lbl2[j] = myHHMM1 + " " + myMessage1 ;
DoShow(j, ColorOpen ); 
 

j = j + 1 ;

DoDailyOpen(  myHHMM2  )  ; 
DoWork( period ) ;
lbl[j]  = ShortName+j;
lbl2[j] = myHHMM2 + " " + myMessage2 ;
DoShow(j, ColorOpen ); 


j = j + 1 ;

DoDailyOpen(  myHHMM3  )  ; 
DoWork( period ) ;
lbl[j]  = ShortName+j;
lbl2[j] = myHHMM3 + " " + myMessage3 ;
DoShow(j, ColorOpen ); 
 
j = j + 1 ;

DoDailyOpen(  myHHMM4  )  ; 
DoWork( period ) ;
lbl[j]  = ShortName+j;
lbl2[j] = myHHMM4 + " " + myMessage4 ;
DoShow(j, ColorOpen ); 

j = j + 1 ;

DailyOpen  = iOpen(symbol,PERIOD_W1,0) ;
sDailyOpen = DoubleToStr(DailyOpen,digits) ;
lbl[j]  = ShortName+j;
lbl2[j] = "Weekly    " ;
DoWork( PERIOD_W1) ;
DoShow(j, ColorOpen ); 


j = j + 1 ;

 
DailyOpen  = iOpen(symbol,PERIOD_MN1,0) ;
sDailyOpen = DoubleToStr(DailyOpen,digits) ;
lbl[j]  = ShortName+j;
lbl2[j] = "Monthly   " ;
DoWork( PERIOD_MN1) ;
DoShow(j, ColorOpen ); 

//---------

WindowRedraw() ;   




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);

if(u > 0)
{

string glabel = "pr" + lbl[u] ;
ObjectCreate(glabel,23,0,Time[0],PRICE_CLOSE);
ObjectSet(glabel, OBJPROP_CORNER, myCorner );
ObjectSet(glabel,OBJPROP_XDISTANCE, 90+myChartX);
ObjectSet(glabel,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(glabel, sDailyOpen ,myFontSize,myFont,ColorOpen);
 
string rlabel = "od" + lbl[u] ;
ObjectCreate(rlabel,23,0,Time[0],PRICE_CLOSE);
ObjectSet(rlabel, OBJPROP_CORNER, myCorner );
ObjectSet(rlabel,OBJPROP_XDISTANCE, 150+myChartX);
ObjectSet(rlabel,OBJPROP_YDISTANCE,n+myChartY);
ObjectSetText(rlabel,sDiff,myFontSize,myFont,ColorOpen);

} 

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

//+------------------------------------------------------------------+
void DoWork( int wPeriod )
{
  

  
Close0    = iClose(symbol,0,0) ;
Diff      = ( Close0 - DailyOpen ) / point ;
sDiff     = DoubleToStr(Diff, 0) ;

   if(Close0 > DailyOpen ) { ColorOpen = Lime ;   } else {
   if(Close0 < DailyOpen ) { ColorOpen = Red ;    } else { 
                             ColorOpen = Yellow ; } } 

}

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

void DoDailyOpen( string doHHMM  ) 
{

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

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

int      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(symbol,0,cd);    
sDailyOpen = DoubleToStr(DailyOpen,digits) ;

}
 
//+------------------------------------------------------------------+
/*
Comment(

 
"WeeklyOpen " , DoubleToStr(WeeklyOpen,Digits) , "\n" ,  
"MonthlyOpen " , DoubleToStr(MonthlyOpen,Digits) , "\n" ,  
      

"") ; 

Comment(

"myHHMM1" , myHHMM1 , "\n" ,   
"Diff " , DoubleToStr(Diff,Digits) , "\n" ,  
"Close0 " , DoubleToStr(Close0,Digits) , "\n" ,   
"DailyOpen" , DoubleToStr(DailyOpen,Digits) , "\n" ,  
   

"") ; 



Comment(

"ind_bufferOp[0]" , ind_bufferOp[0] , "\n" ,   
"Open0 " , DoubleToStr(Open0,Digits) , "\n" ,  
"Close0 " , DoubleToStr(Close0,Digits) , "\n" ,   
"Open1 " , DoubleToStr(Open1,Digits) , "\n" ,  
"Close1 " , DoubleToStr(Close1,Digits) , "\n" ,   

"") ; 
*/