Weekly_Pivots_TZ

Download script
Author:
Weekly_Pivots_TZ

This script is a specialized charting tool designed for traders who use Pivot Points—specific price levels that indicate potential support and resistance. The primary strength of this specific tool is its ability to handle complex time zone conversions, allowing it to calculate levels based on a specific geographic market (like London or New York) regardless of where your personal trading server is located.

Core Logic and Functionality

1. Time Zone Synchronization The script allows you to define two different time zones: your "Local" time (where your platform currently sits) and your "Destination" time (the market you actually want to analyze). By inputting the difference between these zones, the script can accurately identify when a new trading week begins in that specific market. This ensures that the high, low, and closing prices used for calculations are accurate to the intended session.

2. Data Collection Every weekend (specifically on Sundays and Mondays), the script performs a "look-back" operation. It automatically scans the price history to identify:

  • The highest and lowest prices reached during the previous week.
  • The closing price of the previous week.
  • The opening price of the current week.

3. Automated Level Calculations Using the data collected above, the script mathematically generates several types of trading levels on your chart:

  • Standard Pivots: It calculates a central Pivot Point, along with three Resistance levels (R1, R2, R3) and three Support levels (S1, S2, S3). These represent key areas where price might stall or reverse.
  • Fibonacci Levels: The script identifies the "range" of the previous week and projects Fibonacci retracement levels (such as 38.2% and 61.8%) based on that range.
  • Camarilla Breakouts: It calculates specialized Camarilla levels, which are often used by day traders to identify potential breakout prices for the current session.
  • Midpoint Levels: For more granular analysis, it can draw "mid-pivots," which are the halfway points between the standard support and resistance lines.

4. Visual Organization The script handles all the "heavy lifting" of drawing these lines. It automatically places vertical bars to mark the start and end of trading weeks and draws horizontal lines for every calculated level. Each line is accompanied by a text label (e.g., "R1," "S2," or "Pivot") so you can identify them instantly.

5. On-Screen Dashboard To provide a quick summary, the script generates a text comment in the corner of your chart. This displays real-time data including the current range in pips for both the previous and current weeks, as well as a summary of the highs, lows, and previous close.

Summary of User Experience

For the user, this means you don't have to manually calculate complex formulas or worry about time zone offsets. You simply set your desired market zones once, and the script automatically draws a comprehensive "map" of key price levels every week, helping you visualize where the market might face pressure or find support.

Miscellaneous
It issuies visual alerts to the screen
11 Views
0 Downloads
0 Favorites
Weekly_Pivots_TZ
//+------------------------------------------------------------------+
//|                                             Weekly_Pivots_TZ.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_chart_window

/* Introduction:
   TZ = Time Zone
   Calculation of pivot and similar levels based on TZ.
   If you want to modify the colours, please scroll down to line
   200 and below (where it says "Calculate Levels") and change
   the colors.  Valid color names can be obtained by placing
   the curor on a color name (e.g. somewhere in the word "DarkGray"
   and pressing F1).
   
   TZ Inputs:

   LocalTZ: TZ for which MT4 shows your local time, e.g. 1 or 2 for Europe
            (GMT+1 or GMT+2 (daylight savings time). Use zero for no
            adjustment. The MetaQuotes demo server uses GMT +2.
                  
   DestinationTZ:  TZ for the session from which to calculate the levels
            (e.g. 1 or 2 for the European session (without or with daylight
            savings time). Use zero for GMT
           
   Example: If your MT server is living in the EST (Eastern Standard Time, 
            GMT-5) zone and want to calculate the levels for the London trading
            session (European time in summer GMT+1), then enter -5 for 
            LocalTZ, 1 for DestinationTZ. 
            If you want to know whether your servers are GMT time Google "GMT
            time now" and compare that time to your active MetaTrader charts
            The number of hours that your charts are ahead or behind is your
            local time setting(for example the demo server from MetaQuotes always
            lives in CDT(+2) or CET (+1), no matter what the clock on your wall
            says.
            If in doubt, leave everything to zero.
*/

extern int LocalTZ= 0;
extern int DestinationTZ= -0;

extern int LineType= 1;
extern int LineWidth= 0;

extern int VLineStyle= 0;
extern int VLineThickness= 3;

extern bool ShowComment = true;
extern bool ShowHighLowOpen = true;
extern bool ShowSweetSpots = false;
extern bool ShowPivots = true;
extern bool ShowMidPitvot = false;
extern bool ShowFibos= true;
extern bool ShowCamarilla = false;
extern bool ShowLevelPrices = true;

extern int BarForLabels= 0;     // number of bars from right, where lines labels will be shown

extern bool DebugLogger = false;

int TradingHoursFrom= 0;
int TradingHoursTo= 24;
int PIPplaces; //decimal PIPplaces for symbol's price       



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
	deinit();
	if (Ask>10) PIPplaces=2; else PIPplaces=4;
   Print("Period= ", Period());
   return(0);
}

int deinit()
{
   int total_object_count= ObjectsTotal();
   string pivotname;
   
   for (int i= total_object_count; i>=0; i--) {
      string name= ObjectName(i);
    
      if (StringSubstr(name,0,7)=="[PIVOT]") 
         ObjectDelete(name);
   }
   	pivotname=Symbol()+"st";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"p";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"r1";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"r2";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"r3";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"s1";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"s2";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"s3";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"yh";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"to";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"yl";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"ds1";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"ds2";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"flm618";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"flm382";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"flp382";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"flp5";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"fhm382";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"fhp382";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"fhp618";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"h3";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"h4";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"l3";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"l4";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"mr3";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"mr2";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"mr1";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"ms1";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"ms2";
   	GlobalVariableDel(pivotname);
   	pivotname=Symbol()+"ms3";
   	GlobalVariableDel(pivotname);

   
   return(0);
}
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   static datetime timesincelastupdate= 0;
   static datetime mostrecenttimeframe= 0;
   
   datetime start_of_week= 0,
            start_of_last_week= 0;

   double this_weeks_high= 0,
            this_weeks_low= 0,
            this_weeks_open= 0,
            last_weeks_high= 0,
            last_weeks_open= 0,
            last_weeks_low= 0,
            last_weeks_close= 0;

   int gotofirstbarofthisweek= 0,
       gotofirstbaroflastweek= 0,
       gotolastbaroflastweek= 0;

   
   // Will not update these too often   
   if (TimeCurrent()-timesincelastupdate<60 && Period()==mostrecenttimeframe)
      return (0);
      
   mostrecenttimeframe= Period();
   timesincelastupdate= TimeCurrent();
   
   //---- period greater than weekly charts
   if(Period() > 10080) {
      Alert("Error - Chart period is greater than 1 week.");
      return(-1); // then exit
   }
if (DayOfWeek()==0|DayOfWeek()==1)
   {if (DebugLogger) {
      Print("Local time current bar:", TimeToStr(Time[0]));
      Print("Dest  time current bar: ", TimeToStr(Time[0]- (LocalTZ - DestinationTZ)*3600), ", TZDifEntered= ", LocalTZ - DestinationTZ);
   }}

if (DayOfWeek()==0|DayOfWeek()==1)
   {   string pivotname; double pivotalvalue;

   // Determine which hour bars make this week and last week
   ComputeDayIndices(LocalTZ, DestinationTZ, gotofirstbarofthisweek, gotofirstbaroflastweek, gotolastbaroflastweek);

   start_of_week= Time[gotofirstbarofthisweek];  // datetime (x-value) for labels on horizontal bars
   pivotname=Symbol()+"st";
   pivotalvalue=start_of_week;
   GlobalVariableSet(pivotname,pivotalvalue);
   start_of_last_week= Time[gotofirstbaroflastweek];  // datetime (x-value) for labels on horizontal bars

   

   // 
   // walk forward through last week's start and collect high/lows within the same week
   //
   last_weeks_high= -99999;  // not high enough to remain alltime high
   last_weeks_low=  +99999;  // not low enough to remain alltime low
   
   for (int gotobar=gotofirstbaroflastweek; gotobar>=gotolastbaroflastweek; gotobar--) {

      if (last_weeks_open==0)  // grab first value for open
         last_weeks_open= Open[gotobar];                      
      
      last_weeks_high= MathMax(High[gotobar], last_weeks_high);
      last_weeks_low= MathMin(Low[gotobar], last_weeks_low);
      
      // overwrite close in loop until we leave with the last iteration's value
      last_weeks_close= Close[gotobar];
   }

   

   // 
   // walk forward through this week and collect high/lows within the same week
   //
   this_weeks_open= Open[gotofirstbarofthisweek];  // should be open of this week start trading hour

   this_weeks_high= -99999; // not high enough to remain alltime high
   this_weeks_low=  +99999; // not low enough to remain alltime low
   for (int j= gotofirstbarofthisweek; j>=0; j--) {
      this_weeks_high= MathMax(this_weeks_high, High[j]);
      this_weeks_low= MathMin(this_weeks_low, Low[j]);
   }
      
   
   
   // draw the vertical bars that marks the time span
   double level= (last_weeks_high + last_weeks_low + last_weeks_close + this_weeks_open) / 4;
   SetTimeLine("LastWeeksStart", "LastWeek", gotofirstbaroflastweek, RoyalBlue, level - 4*Point);
   SetTimeLine("LastWeeksEnd", "ThisWeek", gotofirstbarofthisweek, RoyalBlue, level - 4*Point);
   
   
   
   if (DebugLogger) 
      Print("Timezoned values: lwo= ", last_weeks_open, ", lwc=", last_weeks_close, ", lwhigh= ", last_weeks_high, ", lwlow== ", last_weeks_low, ", tweo= ", this_weeks_open);

}
   //
   //---- Calculate Levels
   //
if (DayOfWeek()==0|DayOfWeek()==1)
{
   double p, q, d, r1,r2,r3, s1,s2,s3;
   
   d = (this_weeks_high - this_weeks_low);
   q = (last_weeks_high - last_weeks_low);
   p = (last_weeks_high + last_weeks_low + last_weeks_close + this_weeks_open)/4;
   p=NormalizeDouble(p,PIPplaces);
   pivotname=Symbol()+"p";
   pivotalvalue=p;
   GlobalVariableSet(pivotname,pivotalvalue);
   
   r1 = (2*p)-last_weeks_low;
   r1=NormalizeDouble(r1,PIPplaces);
   pivotname=Symbol()+"r1";
   pivotalvalue=r1;
   GlobalVariableSet(pivotname,pivotalvalue);
   r2 = p+(last_weeks_high - last_weeks_low);              //	r2 = p-s1+r1;
   r2=NormalizeDouble(r2,PIPplaces);
   pivotname=Symbol()+"r2";
   pivotalvalue=r2;
   GlobalVariableSet(pivotname,pivotalvalue);
	r3 = (2*p)+(last_weeks_high-(2*last_weeks_low));
   r3=NormalizeDouble(r3,PIPplaces);
   pivotname=Symbol()+"r3";
   pivotalvalue=r3;
   GlobalVariableSet(pivotname,pivotalvalue);
   s1 = (2*p)-last_weeks_high;
   s1=NormalizeDouble(s1,PIPplaces);
   pivotname=Symbol()+"s1";
   pivotalvalue=s1;
   GlobalVariableSet(pivotname,pivotalvalue);
   s2 = p-(last_weeks_high - last_weeks_low);              //	s2 = p-r1+s1;
   s2=NormalizeDouble(s2,PIPplaces);
   pivotname=Symbol()+"s2";
   pivotalvalue=s2;
   GlobalVariableSet(pivotname,pivotalvalue);
	s3 = (2*p)-((2* last_weeks_high)-last_weeks_low);
   s3=NormalizeDouble(s3,PIPplaces);
   pivotname=Symbol()+"s3";
   pivotalvalue=s3;
   GlobalVariableSet(pivotname,pivotalvalue);


   //---- High/Low, Open
   if (ShowHighLowOpen) {
      EstablishLevel("LW\'s High", last_weeks_high,  MediumBlue, VLineStyle, VLineThickness, start_of_last_week);
      EstablishLevel("TW\'s Open", this_weeks_open,      MediumBlue, LineType, LineWidth, start_of_week);
      EstablishLevel("LW\'s Low", last_weeks_low,    MediumBlue, VLineStyle, VLineThickness, start_of_last_week);

   	pivotname=Symbol()+"lwhigh";
   	pivotalvalue=last_weeks_high;
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"tweo";
   	pivotalvalue=this_weeks_open;
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"lwlow";
   	pivotalvalue=last_weeks_low;
   	GlobalVariableSet(pivotname,pivotalvalue);
   }


   //---- High/Low, Open
   if (ShowSweetSpots) {
      int ssp1, ssp2;
      double ds1, ds2;
      
      ssp1= Bid / Point;
      ssp1= ssp1 - ssp1%50;
      ssp2= ssp1 + 50;
      
      ds1= ssp1*Point;
      ds2= ssp2*Point;
      
      EstablishLevel(DoubleToStr(ds1,PIPplaces), ds1,  Maroon, LineType, LineWidth, Time[10]);
      EstablishLevel(DoubleToStr(ds2,PIPplaces), ds2,  Maroon, LineType, LineWidth, Time[10]);

   	pivotname=Symbol()+"ds1";
   	pivotalvalue=ds1;
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"ds2";
   	pivotalvalue=ds2;
   	GlobalVariableSet(pivotname,pivotalvalue);
   }

   //---- Pivot Lines
   if (ShowPivots==true) {
      EstablishLevel("R1", r1,      Red, VLineStyle, VLineThickness, start_of_week);
      EstablishLevel("R2", r2,      Red, VLineStyle, VLineThickness, start_of_week);
      EstablishLevel("R3", r3,      Red, VLineStyle, VLineThickness, start_of_week);
      
      EstablishLevel("Pivot", p,    Black, VLineStyle, VLineThickness, start_of_week);

      EstablishLevel("S1", s1,      Green, VLineStyle, VLineThickness, start_of_week);
      EstablishLevel("S2", s2,      Green, VLineStyle, VLineThickness, start_of_week);
      EstablishLevel("S3", s3,      Green, VLineStyle, VLineThickness, start_of_week);
   }
   
   //---- Fibos of yesterday's range
   if (ShowFibos) {
      // .618, .5 and .382
      EstablishLevel("Low - 61.8%", last_weeks_low - q*0.618,      DarkGoldenrod, LineType, LineWidth, start_of_week);
      EstablishLevel("Low - 38.2%", last_weeks_low - q*0.382,      DarkOrange, LineType, LineWidth, start_of_week);
      EstablishLevel("Low + 38.2%", last_weeks_low + q*0.382,      DarkOrange, LineType, LineWidth, start_of_week);
      EstablishLevel("LowHigh 50%", last_weeks_low + q*0.5,        Orange, VLineStyle, LineWidth, start_of_week);
      EstablishLevel("High - 38.2%", last_weeks_high - q*0.382,    DarkOrange, LineType, LineWidth, start_of_week);
      EstablishLevel("High + 38.2%", last_weeks_high + q*0.382,    DarkOrange, LineType, LineWidth, start_of_week);
      EstablishLevel("High + 61.8%", last_weeks_high +  q*0.618,   DarkGoldenrod, LineType, LineWidth, start_of_week);

   	pivotname=Symbol()+"flm618";
   	pivotalvalue=last_weeks_low - q*0.618;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"flm382";
   	pivotalvalue=last_weeks_low - q*0.382;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"flp382";
   	pivotalvalue=last_weeks_low + q*0.382;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"flp5";
   	pivotalvalue=last_weeks_low + q*0.5;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"fhm382";
   	pivotalvalue=last_weeks_high - q*0.382;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"fhp382";
   	pivotalvalue=last_weeks_high + q*0.382;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"fhp618";
   	pivotalvalue=last_weeks_high + q*0.618;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);

   }


   //----- Camarilla Lines
   if (ShowCamarilla==true) {
      
      double h4,h3,l4,l3;
	   h4 = (q*0.55)+last_weeks_close;
	   h3 = (q*0.27)+last_weeks_close;
	   l3 = last_weeks_close-(q*0.27);	
	   l4 = last_weeks_close-(q*0.55);	
	   
      EstablishLevel("H3", h3,   Khaki, LineType, LineWidth, start_of_week);
      EstablishLevel("H4", h4,   Khaki, LineType, LineWidth, start_of_week);
      EstablishLevel("L3", l3,   Khaki, LineType, LineWidth, start_of_week);
      EstablishLevel("L4", l4,   Khaki, LineType, LineWidth, start_of_week);

   	pivotname=Symbol()+"h3";
   	pivotalvalue=h3;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"h4";
   	pivotalvalue=h4;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"l3";
   	pivotalvalue=l3;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"l4";
   	pivotalvalue=l4;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   }


   //------ Midpoints Pivots 
   if (ShowMidPitvot==true) {
      // mid levels between pivots
      EstablishLevel("MR3", (r2+r3)/2,    Red, LineType, LineWidth, start_of_week);
      EstablishLevel("MR2", (r1+r2)/2,    Red, LineType, LineWidth, start_of_week);
      EstablishLevel("MR1", (p+r1)/2,     Red, LineType, LineWidth, start_of_week);
      EstablishLevel("MS1", (p+s1)/2,     Green, LineType, LineWidth, start_of_week);
      EstablishLevel("MS2", (s1+s2)/2,    Green, LineType, LineWidth, start_of_week);
      EstablishLevel("MS3", (s2+s3)/2,    Green, LineType, LineWidth, start_of_week);

   	pivotname=Symbol()+"mr3";
   	pivotalvalue=(r2+r3)/2;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"mr2";
   	pivotalvalue=(r1+r2)/2;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"mr1";
   	pivotalvalue=(p+r1)/2;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"ms1";
   	pivotalvalue=(p+s1)/2;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"ms2";
   	pivotalvalue=(p+s2)/2;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   	pivotname=Symbol()+"ms3";
   	pivotalvalue=(p+s3)/2;
   	pivotalvalue=NormalizeDouble(pivotalvalue,PIPplaces);
   	GlobalVariableSet(pivotname,pivotalvalue);
   }


   //------ Comment for upper left corner
   if (ShowComment) {
      string comment= ""; 
      
      comment= comment + "-- The Latham Group ---\n";
      comment= comment + "Range: Yesterday "+DoubleToStr(MathRound(q/Point),0)   +" PIPplaces, Today "+DoubleToStr(MathRound(d/Point),0)+" PIPplaces" + "\n";
      comment= comment + "Highs: Yesterday "+DoubleToStr(last_weeks_high,PIPplaces)  +", Today "+DoubleToStr(this_weeks_high,PIPplaces) +"\n";
      comment= comment + "Lows:  Yesterday "+DoubleToStr(last_weeks_low,PIPplaces)   +", Today "+DoubleToStr(this_weeks_low,PIPplaces)  +"\n";
      comment= comment + "Close: Yesterday "+DoubleToStr(last_weeks_close,PIPplaces) + "\n";
   // comment= comment + "Pivot: " + DoubleToStr(p,PIPplaces) + ", S1/2/3: " + DoubleToStr(s1,PIPplaces) + "/" + DoubleToStr(s2,PIPplaces) + "/" + DoubleToStr(s3,PIPplaces) + "\n" ;
   // comment= comment + "Fibos: " + DoubleToStr(last_weeks_low + q*0.382, PIPplaces) + ", " + DoubleToStr(last_weeks_high - q*0.382,PIPplaces) + "\n";
      
      Comment(comment); 
   }

   return(0);
}
}

 
//+------------------------------------------------------------------+
//| Compute index of first/last bar of last week and this week       |
//+------------------------------------------------------------------+

void ComputeDayIndices(int TZLocal, int TZDest, int &gotofirstbarofthisweek, int &gotofirstbaroflastweek, int &gotolastbaroflastweek)
{     
if (DayOfWeek()==0|DayOfWeek()==1)
   {int TZDifEntered= TZLocal - TZDest,
       TZdifSeconds= TZDifEntered*3600,
       weekminutes= 7 * 24 * 60,
       barsperweek= weekminutes/Period();
   
   int dayofweektoday= TimeDayOfWeek(Time[0] - TZdifSeconds),  // what day is today in the dest timezone?
       FindOneWeekBack= -1;
}
   //
   // due to gaps in the data, and shift of time around weekends (due 
   // to time zone) it is not as easy as to just look back for a bar 
   // with 00:00 time
   //
   
   gotofirstbarofthisweek= 0;
   gotofirstbaroflastweek= 0;
   gotolastbaroflastweek= 0;
       
   switch (dayofweektoday) {
      case 6: // sat
      case 5: // sun
      case 4: // mon
      case 3: // sun
      case 2: // mon

            FindOneWeekBack= 0|1; // last week in terms of trading was Monday or Sunday that the pivots were being calculated upon.
            break;
   }
if (DayOfWeek()==0|DayOfWeek()==1)
   {if (DebugLogger) {
      Print("Dayofweektoday= ", dayofweektoday);
      Print("Dayofweekyesterday= ", FindOneWeekBack);
   }
   }
       
       
   // search  backwards for the last occrrence (backwards) of the week this week (this week's first bar)
if (DayOfWeek()==0|DayOfWeek()==1)
   {for (int i=1; i<=barsperweek+1; i++) {
      datetime timet= Time[i] - TZdifSeconds;
      if (TimeDayOfWeek(timet)!=dayofweektoday) {
         gotofirstbarofthisweek= i-1;
         break;
      }
   }}
   

   // search  backwards for the first occrrence (backwards) of the weekday we are looking for (lastweek's last bar)
if (DayOfWeek()==0|DayOfWeek()==1)
   {for (int j= 0; j<=2*barsperweek+1; j++) {
      datetime timey= Time[i+j] - TZdifSeconds;
      if (TimeDayOfWeek(timey)==FindOneWeekBack) {  // ignore saturdays (a Sa may happen due to TZ conversion)
         gotolastbaroflastweek= i+j;
         break;
      }
   }}


   // search  backwards for the first occurrence of this day before yesterday (to determine this week's first bar)
if (DayOfWeek()==0|DayOfWeek()==1)
   {for (j= 1; j<=barsperweek; j++) {
      datetime timey2= Time[gotolastbaroflastweek+j] - TZdifSeconds;
      if (TimeDayOfWeek(timey2)!=FindOneWeekBack) {  // ignore saturdays (a Sa may happen due to TZ conversion)
         gotofirstbaroflastweek= gotolastbaroflastweek+j-1;
         break;
      }
   }}

if (DayOfWeek()==0|DayOfWeek()==1)
   {if (DebugLogger) {
      Print("Dest time zone\'s current day starts:", TimeToStr(Time[gotofirstbarofthisweek]), 
                                                      " (local time), gotobar= ", gotofirstbarofthisweek);

      Print("Dest time zone\'s previous day starts:", TimeToStr(Time[gotofirstbaroflastweek]), 
                                                      " (local time), gotobar= ", gotofirstbaroflastweek);
      Print("Dest time zone\'s previous day ends:", TimeToStr(Time[gotolastbaroflastweek]), 
                                                      " (local time), gotobar= ", gotolastbaroflastweek);
   }
   }
}


//+------------------------------------------------------------------+
//| Helper                                                           |
//+------------------------------------------------------------------+
void EstablishLevel(string text, double level, color col1, int LineType, int thickness, datetime start_of_week)
{
   int PIPplaces= PIPplaces;
   string labelname= "[PIVOT] " + text + " Label",
          linename= "[PIVOT] " + text + " Line",
          pricelabel; 

   // create or move the horizontal line   
   if (ObjectFind(linename) != 0) {
      ObjectCreate(linename, OBJ_TREND, 0, start_of_week, level, Time[0],level);
      ObjectSet(linename, OBJPROP_STYLE, LineType);
      ObjectSet(linename, OBJPROP_COLOR, col1);
      ObjectSet(linename, OBJPROP_WIDTH, thickness);
   }
   else {
      ObjectMove(linename, 1, Time[0],level);
      ObjectMove(linename, 0, start_of_week, level);
   }
   

   // put a label on the line   
   if (ObjectFind(labelname) != 0) {
      ObjectCreate(labelname, OBJ_TEXT, 0, MathMin(Time[BarForLabels], start_of_week + 2*Period()*60), level);
   }
   else {
      ObjectMove(labelname, 0, MathMin(Time[BarForLabels], start_of_week+2*Period()*60), level);
   }

   pricelabel= " " + text;
   if (ShowLevelPrices && StrToInteger(text)==0) 
      pricelabel= pricelabel + ": "+DoubleToStr(level, PIPplaces);
   
   ObjectSetText(labelname, pricelabel, 8, "Arial", DarkSlateBlue);
}
      

//+------------------------------------------------------------------+
//| Helper                                                           |
//+------------------------------------------------------------------+
void SetTimeLine(string objname, string text, int goto, color col1, double vleveltext) 
{
   string name= "[PIVOT] " + objname;
   int x= Time[goto];

   if (ObjectFind(name) != 0) 
      ObjectCreate(name, OBJ_TREND, 0, x, 0, x, 100);
   else {
      ObjectMove(name, 0, x, 0);
      ObjectMove(name, 1, x, 100);
   }
   
   ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
   ObjectSet(name, OBJPROP_COLOR, DarkGray);
   
   if (ObjectFind(name + " Label") != 0) 
      ObjectCreate(name + " Label", OBJ_TEXT, 0, x, vleveltext);
   else
      ObjectMove(name + " Label", 0, x, vleveltext);
            
   ObjectSetText(name + " Label", text, 8, "Arial", col1);
}

Comments