
#property copyright "Copyright @ 2014"
#property link      "email: none@none.com" 

#property indicator_chart_window

#include <stdlib.mqh>

/*
Main_Lines(1.28000)             Settings:
Main_Lines_Max_TF
Main_Lines_Color
Main_Lines_Style_01234
Main_Lines_Width_012345
Half_Lines(1.28500)                Settings:
Half_Lines_Max_TF
Half_Lines_Color
Half_Lines_Style_01234
Half_Lines_Width_012345
Quarter_Lines(1.28250)            Settings:
Quarter_Lines_Max_TF
Quarter_Lines_Color
Quarter_Lines_Style_01234
Quarter_Lines_Width_012345
Quarter_Lines_Max_TF

*/
extern bool   Liness_On                       = true;
extern  bool  Show_Half_Lines               = true;
extern bool   Show_Quarter_Lines            = true;
extern string Main_Lines=" Main lines(1.28000) Settings:";
extern int    Main_Lines_Max_TF           = 10080;
extern color  Main_Lines_Color               = C'220,210,255';
extern int    Main_Lines_Style_01234         = 0;
extern int    Main_Lines_Width_12345         = 1;
extern string Half_Lines=" Half_Lines(1.28500) Settings:";
extern int    Half_Lines_Max_TF            = 240;
extern color  Half_Lines_Color                = C'202,220,240';
extern int    Half_Lines_Style_01234          = 0;
extern int    Half_Lines_Width_12345          = 1;
extern string Quarter_Lines=" Quarter_Lines(1.28250) Settings:";
extern int    Quarter_Lines_Max_TF         = 60;
extern color  Quarter_Lines_Color             = C'210,240,210';
extern int    Quarter_Lines_Style_01234       = 0;
extern int    Quarter_Lines_Width_12345       = 1;

//+-------------------------------------------------------------------------------------------+
//| Indicator Initialization                                                                  |                                                        
//+-------------------------------------------------------------------------------------------+      
int init()
  {
//With the first DLL call below, the program will exit (and stop) automatically after one alert. 
  // if(!IsDllsAllowed())
  //   {
  //    Alert(Symbol()," ",Period(),": Access Panel: Allow DLL Imports"); return(false);
  //   }
//Determine the current chart scale (chart scale number should be 0-5)
   Chart_Scale=ChartScaleGet();

//Reset to Signal State of Non-deinitialization 
   Deinitialized=false;

//Panel - determine display of Market Price            
   if(Digits==5 || Digits==3) {Factor=10;}
   else {Factor=1;} //cater for 5 digits 

                    //Panel and Liness - Check for unconventional Point digits number. 
   Poin2=Point;
   if(Point==0.00001) {Poin=0.0001;} //5 digits     
   else
     {
      if(Point==0.001) {Poin=0.01;} //3 digits
      else {Poin=Point;}
     } //Normal 4 digits       
   if(StringSubstr(Symbol(),0,6)=="XAUUSD") {Poin=0.1;}
   else
     {
      if(StringSubstr(Symbol(),0,6)=="EURTRY") {Poin=0.001;}
      else
        {
         if(StringSubstr(Symbol(),0,6)=="USDTRY") {Poin=0.001;}
         else
           {
            if(StringSubstr(Symbol(),0,6)=="USDMXN") {Poin=0.001;}
            else {if(StringSubstr(Symbol(),0,6)=="USDCZK") {Poin=0.001;} }
           }
        }
     }

//Liness - set ranges for chart TFs 
   if(Period()==1) {NumberOfRanges=4;}
   else
     {
      if(Period()==5) {NumberOfRanges=7;}
      else
        {
         if(Period()==15) {NumberOfRanges=15;}
         else
           {
            if(Period()==30) {NumberOfRanges=15;}
            else
              {
               if(Period()==60) {NumberOfRanges=20;}
               else
                 {
                  if(Period()==240) {NumberOfRanges=30;}
                  else
                    {
                     if(Period()==1440) {NumberOfRanges=60;}
                     else
                       {
                        if(Period()==10080) {NumberOfRanges=70;}
                        else {if(Period()==43200) {NumberOfRanges=60;} }
                       }
                    }
                 }
              }
           }
        }
     }

   return(0);
  }
//+-------------------------------------------------------------------------------------------+
//| Indicator De-initialization                                                               |                                                        
//+-------------------------------------------------------------------------------------------+       
int deinit()
  {
   obj_total=ObjectsTotal();
   for(k=obj_total; k>=0; k--)
     {
      name=ObjectName(k);
      if(StringSubstr(name,0,5)=="[C1P1" || StringSubstr(name,1,5)=="[C1P1") {ObjectDelete(name);}
     }
//Comment("");
   return(0);
  }
//+-------------------------------------------------------------------------------------------+
//| Indicator Start                                                                           |                                                        
//+-------------------------------------------------------------------------------------------+         
int start()
  {
//If Indicator is "Off" deinitialize only once, not every tick-------------------------------  
   if(!Indicator_On)
     {
      if(!Deinitialized) {deinit(); Deinitialized=true;}
      return(0);
     }

//If indicator is "ON" delete all objects----------------------------------------------------
   obj_total=ObjectsTotal();
   for(k=obj_total; k>=0; k--)
     {
      name=ObjectName(k);
      if(StringSubstr(name,0,5)=="[C1P1" || StringSubstr(name,1,5)=="[C1P1") {ObjectDelete(name);}
     }

//Liness-------------------------------------------------------------------------------------
   if(Liness_On && Period()<=Main_Lines_Max_TF)
     {
      if(Period()>240 && Liness_Display_Number==2) {Liness_Display_Number=3;}
      //define needed variables
      ssp1=Bid/Poin;  //bid w/o the decimal 
      v1=ssp1%100; //last two significant digits   
      BL=v1; //last two significant digits of starting/reference bid line  

             //calculate line levels bottom to top
      for(j=-(100-(100-BL))-(100*NumberOfRanges); j<=(100-BL)+(100*NumberOfRanges); j++)
        {
         ssp=ssp1+j;
         v1=ssp%100;
         if(v1==u1)
           {
            linelevel=ssp*Poin;
            DrawLines("LVL",Liness_Display_Number,linelevel,Main_Lines_Color,
                      Main_Lines_Style_01234,Main_Lines_Width_12345);
           }
         else
           {
            if((Show_Half_Lines && Period()<=Half_Lines_Max_TF) && (v1==u2))
              {
               linelevel=ssp*Poin;
               DrawLines("LVL",Liness_Display_Number,linelevel,Half_Lines_Color,
                         Half_Lines_Style_01234,Half_Lines_Width_12345);
              }
            else
              {
               if((Show_Quarter_Lines && Period()<=Quarter_Lines_Max_TF) && ((v1==u3) || (v1==u4)))
                 {
                  linelevel=ssp*Poin;
                  DrawLines("LVL",Liness_Display_Number,linelevel,Quarter_Lines_Color,
                            Quarter_Lines_Style_01234,Quarter_Lines_Width_12345);
                 }
              }
           }
        }
     }//End Liness         


//End of program computations---------------------------------------------------------------        
   return(0);
  }
//+-------------------------------------------------------------------------------------------+
//| Subroutine To name and draw Daily Pivot and Range HL lines and line labels                |                                                                                  |
//+-------------------------------------------------------------------------------------------+
void DrawLines(string text,int Line_Selection_Number,double Lines,color Clr,
               int style,int thickness)
  {
//Lines====================================================================================== 
   if(Line_Selection_Number<1 || Line_Selection_Number>3) {Line_Selection_Number=2;}

//Name lines: lines with "a" make them subordinate to lines with "b" or "c".  Lines with 
//"b" are subordinate to lines with "c".  Lines with "c" always on top. 
   if(StringSubstr(text,0,2)=="LV") //Liness lines
     {
      line="a[C1P1 Draw] "+text+" Line "+DoubleToStr(Lines,Digits);
     }
   else if(StringSubstr(text,0,1)=="R")//Range lines
     {
      line="c[C1P1 Draw] "+text+" Line "+DoubleToStr(Lines,Digits);
     }
   else //all pivots lines
     {
      line="b[C1P1 Draw] "+text+" Line "+DoubleToStr(Lines,Digits);
     }

//Define variables  
   a=style; b=thickness; c=1; if(a==0)c=b;
   int Shift;

//Where to Start lines
//start chart left
   if(Line_Selection_Number==1)
     {startline=WindowFirstVisibleBar();}
//start at day separator 
   else if(Line_Selection_Number==2 || Line_Selection_Number==3)
     {
      Shift=0;  if(Broker_GMT_Offset==0 && DayOfWeek()==0) {Shift=1;}
      startline=iTime(NULL,PERIOD_D1,Shift);
     }

//Where to Stop Lines   
//stop at chart right border
   if(Line_Selection_Number==1)
     {stopline=iTime(NULL,PERIOD_D1,0)+18748800; R2=true;}
//stop at end of current session
   else
     {
      if(Line_Selection_Number==2)
        {stopline=iTime(NULL,PERIOD_D1,0)+86400; R2=false;}
      //stop seven TFs right of current bar
      else {if(Line_Selection_Number==3) {stopline=startline+7*(Period()*60); R2=false;} }
     }

//Draw lines   
   ObjectCreate(line,OBJ_TREND,0,startline,Lines,stopline,Lines);
   ObjectSet(line,OBJPROP_STYLE,a);
   ObjectSet(line,OBJPROP_COLOR,Clr);
   ObjectSet(line,OBJPROP_WIDTH,c);
   if(Subordinate_Lines || StringSubstr(text,0,4)=="LVL") ObjectSet(line,OBJPROP_BACK,true);
   else ObjectSet(line,OBJPROP_BACK,false);
   ObjectSet(line,OBJPROP_RAY,R2);

//Labels=====================================================================================
//Exit if label not to be shown 
   if(StringSubstr(text,0,2)=="LV") {return;}
   else
     {
      if((!Pivot_PVT_Label_On) && (StringSubstr(text,0,2)=="PV")) {return;}
      else
        {
         if((!Pivot_SRM_Labels_On) && ((StringSubstr(text,1,1)=="R") || 
            (StringSubstr(text,1,1)=="S") || (StringSubstr(text,0,1)=="M"))) {return;}
         else
           {
            if((!Range_Labels_On) && ((StringSubstr(text,0,2)=="RD")
               || (StringSubstr(text,0,2)=="RW"))) {return;}
            else
              {
               //Name label  
               if(StringSubstr(text,0,2)=="LV") //Liness lines
                 {
                  label="a[C1P1 Draw] "+text+" Label "+DoubleToStr(Lines,Digits);
                 }
               else if(StringSubstr(text,0,1)=="R")//Range lines
                 {
                  label="c[C1P1 Draw] "+text+" Label "+DoubleToStr(Lines,Digits);
                 }
               else //all pivots lines
                 {
                  label="b[C1P1 Draw] "+text+" Label "+DoubleToStr(Lines,Digits);
                 }

               //Pivots: Remove main pivot number
               if(StringSubstr(text,0,1)=="P")
                 {
                  text=StringSubstr(text,0,3);
                 }

              

              
                  text="  "+text+" ";
                

               //Where to put labels
               if(Line_Selection_Number==1)
                 {
                  //start at mid-chart
                  spc=""; //00 
                  startlabel=Time[WindowFirstVisibleBar()/2];
                 }
               else
                 {
                  if(Line_Selection_Number==2)
                    {
                     Shift=0;  if(Broker_GMT_Offset==0 && DayOfWeek()==0) {Shift=1;}
                     if(Time[WindowFirstVisibleBar()]<iTime(NULL,PERIOD_D1,0))
                       {
                        //start at day separator if it is in chart window
                        spc="        "; //08                           
                        startlabel=iTime(NULL,PERIOD_D1,Shift);
                       }
                     else
                       {
                        //start at mid-chart
                        spc=""; //00 
                        startlabel=Time[WindowFirstVisibleBar()/2];
                       }
                    }
                  else
                    {
                     if(Line_Selection_Number==3)
                       {
                        //start at current candle + Scale Shift to right 
                        spc="         "; //09                   
                        startlabel=Time[0]+T4;
                       }
                    }
                 }

               //Draw labels                              
               ObjectCreate(label,OBJ_TEXT,0,startlabel,Lines);
               ObjectSetText(label,spc+text,FontSize,FontStyle,Labels_Color);
               if(Subordinate_Labels) ObjectSet(label,OBJPROP_BACK,true);
               else ObjectSet(label,OBJPROP_BACK,false);
              }
           }
        }
     }
  }
//+-------------------------------------------------------------------------------------------+
//| Subroutine to convert time to a string for time labels                                    |
//+-------------------------------------------------------------------------------------------+
//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Set up to get the chart scale number                                         |
//+-------------------------------------------------------------------------------------------+
void OnChartEvent(const int id,const long &lparam,const double &dparam,const string &sparam)
  {
   Chart_Scale=ChartScaleGet();
   init();
  }
//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Get the chart scale number                                                   |
//+-------------------------------------------------------------------------------------------+
int ChartScaleGet()
  {
   long result=-1;
   ChartGetInteger(0,CHART_SCALE,0,result);
   return((int)result);
  }

//+-------------------------------------------------------------------------------------------+
//| Indicator End                                                                             |                                                        
//+-------------------------------------------------------------------------------------------+      



//See function documentation at http://msdn.microsoft.com/en-us/library/ms725473(VS.85).aspx

//Global External Inputs-----------------------------------------------------------------------
bool   Indicator_On                          = true;
string Preface                   = "Get Broker_GMT_Offset w/Clock & live feed.";
int    Broker_GMT_Offset                     = 0;
bool   Apply_it_now_because_feed_is_not_live = false;
string End_Preface                   = "";
string _                               = "";
string Part_1                          = "Indicator Master Controls:";
bool   Show_Clock_in_Panel             = false;
bool   Bid_Line_On                     = false;

bool   Pivots_PVT_On                   = false;
int    PVT_Max_TF                    = 60;
bool   Pivots_SR_On                    = false;
int    Show_Liness_Thru_2345         = 5;
bool   Show_MidPivots                = false;
int    SR_Max_TF                     = 60;
bool   Pivots_Fibonacci                = false;
bool   PivotsTz_On                     = false;
bool   Use_Preferred_Offset          = false;
int    Alternative_GMT_Offset        = 0;
bool   Range_Day_On                    = false;
int    Day_Min_TF                    = 1;
int    Day_Max_TF                    = 240;
bool   Range_Week_On                   = false;
int    Week_Min_TF                   = 15;
int    Week_Max_TF                   = 240;
bool   Separators_On                   = false;
int    Prior_Days_To_Show            = 5;
int    Separators_Max_TF             = 60;
bool   vLines_On                       = false;
int    Current_Session_Max_TF        = 60;
int    Prior_Sessions_To_Show        = 0;
int    Prior_Sessions_Max_TF         = 60;
bool   Current_Session_Ao            = false;
bool   Current_Session_Fo            = false;
string TF_Choices                      = "1-5-15-30-60-240-1440-10080-43200";

string Part_2                          = "Panel Settings:";
bool   Show_Chart_ID_and_TF            = true;
bool   Show_Forex_Fractional_Pip       = true;
int    Shift_Fractional_Pip          = 0;
int    Digits_To_Show_In_Spread        = 2;
int    Days_In_Range_Day_Avg           = 15;
int    Weeks_In_Week_ATR               = 13;
bool   Show_Swaps_vs_Candle_Time       = true;
color  Panel_Background_Color          = C'025,025,025';
color  Symbol_And_TF_Color             = C'208,208,208';
color  Spread_Color                    = C'120,120,120';
color  PipVal_Color                    = C'120,120,120';
color  Range_Color                     = C'120,120,120';
color  Swaps_Time_Color                = C'120,120,120';
color  Bid_UP_Color                    = C'046,188,046';
color  Bid_DN_Color                    = C'234,000,000';
color  Bid_Last_Digit_Normal           = C'100,100,100';
color  Bid_Last_Digit_Small            = C'160,160,160';
color  sonicS                          = C'004,162,255';
color  sonicO                          = C'000,115,183';
color  sonicN                          = C'000,075,121';
color  sonicI                          = C'138,000,000';
color  sonicC                          = C'176,000,000';
color  sonicR                          = C'221,000,000';
color  smiley1                         = C'255,255,100';
color  smiley2                         = White;
color  Smiley_Back_with_Clock          = C'025,025,025';
color  Smiley_Back_without_Clock       = C'025,025,025';
color  ID_TF_Background_for_Smiley     = C'025,025,025';

string Part_3                          = "Bid Line Settings:";
color  Bid_Ray_Color                   = C'100,100,100';
int    Bid_Ray_LineStyle_01234         = 0;
int    Bid_Ray_Thickness               = 1;
color  Bid_Dot_Up_Color                = Lime;
color  Bid_Dot_Dn_Color                = Red;
int    Bid_Dot_Size                    = 6;

string Part_4                          = "Horizontal Lines Position Settings:";
string note_4_0                        = "Choose line display number.";
string note_4_1                        = "1 = Fullscreen";
string note_4_2                        = "2 = Session";
int    Liness_Display_Number           = 1;
int    Pivots_Display_Number           = 2;
int    Ranges_Display_Number           = 2;
bool   Subordinate_Lines               = true;

string Part_5                          = "Horizontal Lines Labels Settings:";
bool   Pivot_PVT_Label_On              = true;
bool   Pivot_SRM_Labels_On             = true;
bool   Range_Labels_On                 = true;
color  Labels_Color                    = DarkGray;
string FontStyle                       = "Verdana";
int    FontSize                        = 7;
bool   Subordinate_Labels              = true;


string Part_7                          = "Pivots Settings:";
color  PVT1_Back_Color                 = CLR_NONE;
int    PVT1_Style_01234                = 0;
int    PVT1_Width_12345                = 3;
color  PVT2_Top_Color                  = C'230,216,000';
int    PVT2_Style_01234                = 0;
int    PVT2_Width_12345                = 1;
color  R_Pivot_Color                   = C'179,000,000';
int    R_Style_01234                   = 2;
int    R_Width_12345                   = 1;
color  S_Pivot_Color                   = C'090,075,173';
int    S_Style_01234                   = 2;
int    S_Width_12345                   = 1;
color  MidPivots_Color                 = C'085,085,000';
int    mP_Style_01234                  = 2;
int    mP_Width_12345                  = 1;





//Buffers, Constants and Variables-------------------------------------------------------------
bool          Deinitialized;

//Deinit
int           obj_total,k;
string        name;

//Panel
color         Static_Price_Color,Static_Bid_Color,Bid_Dot_Color,Static_Bid_Dot_Color;
double        Poin,Poin2,New_Price,Old_Price,level,Spread,ARg,RangeAchieved,dRG,dRange,wRange;
double        pipValue;
int           Chart_Scale,modifier,Color,Factor,G,h,i,l,m,s,t,LP,LS,T1,T2;

string        ID,Price,C,tab,tab1,timeleft;

//Liness
color         linecolor;
double        linelevel,linestyle,linewidth,BL;
int           j,Line1_Color,Line2_Color,Line3_Color;
int           u1=00,u2=50,u3=25,u4=75;
int           v1,ssp,ssp1,NumberOfRanges;
string        linename;

//Pivots
datetime      pvtDT;
double        p,d,q,r1,r2,r3,r4,r5,s1,s2,s3,s4,s5;
double        today_high,today_low,yesterday_high,yesterday_open,
yesterday_low,yesterday_close;
int           Pdisplay,idxfirstbaryesterday,idxlastbaryesterday,idxfirstbartoday,idxbar,
tzdiff,tzdiffsec,barsperday,dayofweektoday,dayofweektofind,Pivots_GMT_Offset;
string        prefix;

//Range Day
datetime      d1,d2;
double        dRangeHigh,dRangeLow,HiToday,LoToday,dRH,dRL;
int           ii,iii,x,xx,r,dtf,Hbarshift,Lbarshift,rdc,rwc,Rdisplay,RDshift;

//Range Week
datetime      w1,w2;
double        HiWeek,LoWeek;
double        wRangeAvg,wRangeHigh,wRangeLow,wRH,wRL;
int           wtf,wHbarshift,wLbarshift,RWdisplay,RWshift;

//Horizontal Lines Placement
datetime      startline,stopline;
int           a,b,c,R2,T,T4;
string        line,dt;

//Horizontal Labels Placement
datetime      startlabel;
string        label,spc;

//vLines & Labels
bool          vLabels_Chart_Top=true;
datetime      shift,T3;
double        top,bottom,scale,YadjustTop,YadjustBot,vLabels_Dist_Above_Border,time2,time4;
int           bps,MF,time3;
int           vLabels_Dist_To_Border=1;
string        vline,vlabel;
string        vLabels_Asian_Open       = "      Ao ";
string        vLabels_Berlin_Open      = "     F  ";
string        vLabels_London_Open      = "      Lo";
string        vLabels_NewYork_Open     = "     Ny";
string        vLabels_London_Close     = "      Lc ";
string        Label_Day_Separator      = "day";



//Clock - Normal hrs 8-17 (9 hrs), Sydney 9-18 (9 hrs) and Tokyo 9-18 (9 hrs)   
int           LocalOpenHour        =  8;
int           LocalCloseHour       = 17;
int           SydneyLocalOpenHour  =  9;
int           SydneyLocalCloseHour = 18;
int           TokyoLocalOpenHour   =  9;
int           TokyoLocalCloseHour  = 18;