Levels Tokyo London New York Markets For ThinkOrSwim

SimpleScript

New member
The idea is to drawing the box around the overnight session, and another box to highlight the Europe or London session, to allow easy visual identification/exchange attribution for any identified support/resistance areas. Want this to work on any Intraday aggregation.

did a bunch of poking around tonite, and what Im thinking might work is to

1) input the start and stop times for the sessions (these below are just examples)

input EUOpen = 0100;
input RTHOpen = 0900;
input RTHClose = 1600;
input AsiaOpen = 1800;

2) use conditional logic to then allow addcloud to paint the boxes to the high and lo of those periods

am I on the right track here?
is there a better way to get RTH/ETH/exchange market hours?
do I need to use secondstilltime? to set the desired range?
what's the best way to capture the max hi and low for that specified time period? use High or HighestAll functions?
TIA
 
Solution
I would like to get someone's input in making TOS upper indicator for ES futures that marks:
1. Previous week high and low (shown as WL, WH)
2. London Range 2am to 5am (for 24 hr only; EST; shown as LRH, LRL)
3. Tokyo Range 8pm to midnight (for 24 hr only; EST; shown as TRH, TRL)
4. New york morning seeion 7am to 10am (for 24 hr only; EST; shown as NYH, NYL)
5. Prior 4 weeks opening gap - friday close and monday open (shown as WOG1, WOG2, WOG3, WOG4)
5. New day opening gap - price difference between 5pm and 6pm (for 24 hr only; EST; shown as DOGH, DOGL)

Thanks

this can be a starting point.
it is set up to pick 1 of 3 preset markets, US, UK, HK . a period of time within a day, that can span over midnight...
here is my version of a study, to specify a repeating time period, that i made a few months ago.
this doesn't use highestall(), as it can cause a script to become complex, and slow down TOS.

find the high and low, over some specified time period, every 24 hours
# hilolines_07a
http://tos.mx/ggTL45x
# 2020-07-17
# halcyonguy
# time periods work over midnight
# can choose wicks or body
# draw shading, bounded by start/stop times and highest/lowest levels
# enter start and end times, (24 hour EST), for a period < 24 hours
# can choose to show several parameters , labels, lines, shading
# pick /es for testing. trades most of the day

ulgOaLg.jpg


Code:
# hilolines_07

# abandon ver06
# start with a copy of ver05, not 05b. this works over midnight
#  add , wicks or body


# hilolines_06
#  removed too much in v05b. doesn't work over midnight
#   wicks or body


# hilolines_05
# 2020-07-17
# halcyonguy
# draw shading, bounded by start/stop times and highest/lowest levels
#  enter start and end times, (24 hour EST), for a period < 24 hours
#  can choose to show several parameters , labels, lines, shading
#   pick  /es  for testing.  trades most of day

declare hide_on_daily;

input candle_levels = {default "wick" , "body" };

input market = { default "U.S." , "EU" , "Asian" , "-"};

input start1_est = 0930;
input end1_est = 1600;
def start1 = start1_EST;
def end1 = end1_est;

input show_start_end_vertical_lines = yes;
def ssevl = show_start_end_vertical_lines;
input show_midnight_vertical_line = yes;
def smvl = show_midnight_vertical_line;
input show_current_period_labels = yes;
def scpl = show_current_period_labels;

def na = Double.NaN;
#def hi = high;
#def lo = low;
def bn = BarNumber();


# chg
def wickht = high - low;


# ---- wicks or body -------------------
#input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max( open, close);
  lowx = min( open, close);
}

# reassign hi lo  to new vars
def hi = highx;
def lo = lowx;

# ----------------------------------------



# color alts  gray 90,90,90    tan 70,70,70    grn 70,110,20
input show_shading = yes;
#input color_numbers_0to255 = yes;
#shading color is created by entering RGB color numbers, 0-255 , for red,green,blue
input shade_color_red_num0to255 = 70;
def cred = shade_color_red_num0to255;
input shade_color_green_num0to255 = 110;
def cgrn = shade_color_green_num0to255;
input shade_color_blue_num0to255 = 20;
def cblu = shade_color_blue_num0to255;

# show test data in labels and bubbles
input showtest = no;
def lastbar = !isnan(close[0]) and isnan(close[-1]);

# define the shading color
DefineGlobalColor( "shade1" , CreateColor(cred, cgrn, cblu));
addlabel(yes, market, GlobalColor( "shade1" ));

# -----------------------------------------------

#  calc total minutes , from midnight , to each time
def start1hr = Floor(start1 / 100);
def start1min = start1 - (start1hr * 100);
def start1minttl = (start1hr * 60) + start1min;
AddLabel(showtest, start1 + ".." + "st hrs=" + start1hr + ".." + start1min + ".." + start1minttl, Color.CYAN);

def end1hr = Floor(end1 / 100);
def end1min = end1 - (end1hr * 100);
def end1minttl = (end1hr * 60) + end1min;
AddLabel(showtest, end1 + ".." + "end hrs=" + end1hr + ".." + end1min + ".." + end1minttl, Color.RED);

# ===========================>>>>>>>>>>>>>>>>>>>>

#  elapsed min in current period
def stmin2 = SecondsFromTime(start1);
def stmin = stmin2 / (60);
AddLabel(showtest, "period min=" + stmin , Color.GREEN);

# ===========================>>>>>>>>>>>>>>>>>>>

#  is bar in a time period? , minutes in a day , 24 x 60 = 1440
def daymin = 1440;
def endz = 2359;
def startz = 0000;
def  first1 = if SecondsTillTime(start1) == 0 then 1 else 0;
def  last1 = if SecondsFromTime(end1) == 0 then 1 else 0;

def period1min;
def period1;
if start1minttl > end1minttl
then {
   # spans midnight , period=(24-start)+end , end to start
    period1min = (daymin - start1minttl) + end1minttl;
    period1 = if ((SecondsFromTime(start1) >= 0 and SecondsTillTime(endz) > 0) or ( SecondsFromTime(startz) >= 0 and SecondsTillTime(end1) > 0)) then 1 else 0;
} else {
   # ok , period=end-start , start to end
    period1min = (end1minttl - start1minttl);
    period1 = if SecondsFromTime(start1) >= 0 and SecondsTillTime(end1) > 0 then 1 else 0;
}

# ====================================

AddVerticalLine(ssevl and first1, "Start " + start1 , Color.GREEN, Curve.MEDIUM_DASH);
AddVerticalLine(ssevl and last1, "End " + end1 , Color.ORANGE, Curve.MEDIUM_DASH);

#  display time as hr:min ,  but trailing 0 not shown
#AddVerticalLine(ssevl and first1, "start " + start1hr + ":" + start1min , Color.GREEN, Curve.MEDIUM_DASH);
#AddVerticalLine(ssevl and last1, "end " + end1hr + ":" + end1min , Color.ORANGE, Curve.MEDIUM_DASH);
AddChartBubble(showtest, low - 1, period1, if period1 then Color.GREEN else Color.CYAN, no);

# midnight
def midn = 0000;
def midnite = if (smvl and SecondsTillTime(midn) == 0) then 1 else 0;
AddVerticalLine(midnite, "midnight" , Color.BLUE, Curve.MEDIUM_DASH);
AddLabel(showtest, "daymin=" + daymin + "  end1minttl=" + end1minttl + "  start1minttl=" + start1minttl, Color.GREEN);

AddLabel(showtest, "period1 min=" + period1min, Color.YELLOW);
AddLabel(scpl and period1, "hrs: " + round((period1min/60),1), GlobalColor("shade1"));

# -----------------------------------------------

# get chart agg minutes
def chagg = GetAggregationPeriod();
def aggmin = chagg / (1000 * 60);
AddLabel(showtest, "agg=" + aggmin, Color.CYAN);

# calc qty of bars of period1, for the current chart time
def period1bars = period1min / aggmin;
AddLabel(showtest, "period1 bars=" + period1bars, Color.YELLOW);

# ex. trade session = 6.5 hrs = 390 min
# 5 min chart = 390/5 = 78 bars  over 6.5 hours
# def len = 78;

# =====================<<<<<<<<<<<<<<<<<<<<<<

#  add check if in period and if last bar
def currentperiodbars = if (period1 and lastbar) then (stmin / aggmin) else 0;
AddLabel(showtest, "current period bars=" + currentperiodbars, Color.magenta);
AddLabel(scpl and period1, "bars " + currentperiodbars + "/" + period1bars, GlobalColor("shade1"));


#  find the high of active period, only on last bar
def xhi = fold xi = 0 to currentperiodbars
    with n = hi
    do Max(n, GetValue(hi, xi));

AddLabel(scpl and period1, "highest: " + xhi , GlobalColor("shade1"));

#  find the low of active period, only on last bar
def xlo = fold xj = 0 to currentperiodbars
    with m = lo
    do Min(m, GetValue(lo, xj));

AddLabel(scpl and period1, "lowest: " + xlo , GlobalColor("shade1"));

def len = period1bars - 1;

# ---------------------------------------------

# find high value for previous/complete time period, draw a line
def hi2 = if first1 then Highest(hi[-len], len + 1)
   else if last1 then na
   else if period1 then hi2[1]
   else na;

plot hiline = hi2;
hiline.SetDefaultColor(Color.WHITE);
#hiline.SetStyle(Curve.MEDIUM_DASH);
hiline.SetLineWeight(1);
hiline.HideBubble();

# ------------------------------------------------
# find low value for previous/complete time period, draw a line

def lo2 = if first1 then Lowest(lo[-len], len + 1)
   else if last1 then na
   else if period1 then lo2[1]
   else na;

plot loline = lo2;
loline.SetDefaultColor(Color.WHITE);
#loline.SetStyle(Curve.MEDIUM_DASH);
loline.SetLineWeight(1);
loline.HideBubble();

# ---------------------------------------------

# shading
def hiline2 = if show_shading then hiline else na;
def loline2 = if show_shading then loline else na;

#addcloud(hiline,loline,color.light_gray,color.light_gray);
AddCloud(hiline2, loline2, GlobalColor( "shade1" ), GlobalColor( "shade1" ) );
#

Would it be possible to add a line across the chart that shows the midnight opening price?
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

My indicator marks every third new high candle. I've scripted a boolean wedge above it but would visually prefer a box. The last line is my latest attempt to draw it with addcloud.

Code:
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
def PatternPlot =
    if ((Sum(IsUp, 1)[3] >= 0)) and
    ((Sum(IsUp, 1)[2] >= 0)) and
    ((Sum(IsUp, 1)[1] >= 0)) and
    ((Sum(IsUp, 1)[0] >= 0)) and
    Highest(high[3], 1) < Highest(high[2], 1) and
    Highest(high[2], 1) < Highest(high[1], 1) and
    Highest(high[1], 1) < Highest(high[0], 1) then 1 else 0;
def cancelPatternPlot = if PatternPlot[1] ==1 then 1 else 0;

plot pattern = PatternPlot==1 and cancelPatternPlot==0;
pattern.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
pattern.SetDefaultColor(Color.LIGHT_RED);
pattern.SetLineWeight(2);

AddCloud(if pattern then high else Double.NaN, if pattern then low else Double.NaN, Color.RED);
Is it possible to script the 3rd new Low candle as well? Thanks.
 
Would it be possible to add a line across the chart that shows the midnight opening price?

i think this will work.
could look for the first bar of a different day and read the open, and save it.

Code:
def diffday = GetDay() <> GetDay()[1];
def dayopen = if diffday then open else dayopen[1];
plot dayo = dayopen;
dayo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dayo.SetDefaultColor(color.magenta);
 
here is my version of a study, to specify a repeating time period, that i made a few months ago.
this doesn't use highestall(), as it can cause a script to become complex, and slow down TOS.

find the high and low, over some specified time period, every 24 hours
# hilolines_07a
http://tos.mx/ggTL45x
# 2020-07-17
# halcyonguy
# time periods work over midnight
# can choose wicks or body
# draw shading, bounded by start/stop times and highest/lowest levels
# enter start and end times, (24 hour EST), for a period < 24 hours
# can choose to show several parameters , labels, lines, shading
# pick /es for testing. trades most of the day

ulgOaLg.jpg


Code:
# hilolines_07

# abandon ver06
# start with a copy of ver05, not 05b. this works over midnight
#  add , wicks or body


# hilolines_06
#  removed too much in v05b. doesn't work over midnight
#   wicks or body


# hilolines_05
# 2020-07-17
# halcyonguy
# draw shading, bounded by start/stop times and highest/lowest levels
#  enter start and end times, (24 hour EST), for a period < 24 hours
#  can choose to show several parameters , labels, lines, shading
#   pick  /es  for testing.  trades most of day

declare hide_on_daily;

input candle_levels = {default "wick" , "body" };

input market = { default "U.S." , "EU" , "Asian" , "-"};

input start1_est = 0930;
input end1_est = 1600;
def start1 = start1_EST;
def end1 = end1_est;

input show_start_end_vertical_lines = yes;
def ssevl = show_start_end_vertical_lines;
input show_midnight_vertical_line = yes;
def smvl = show_midnight_vertical_line;
input show_current_period_labels = yes;
def scpl = show_current_period_labels;

def na = Double.NaN;
#def hi = high;
#def lo = low;
def bn = BarNumber();


# chg
def wickht = high - low;


# ---- wicks or body -------------------
#input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max( open, close);
  lowx = min( open, close);
}

# reassign hi lo  to new vars
def hi = highx;
def lo = lowx;

# ----------------------------------------



# color alts  gray 90,90,90    tan 70,70,70    grn 70,110,20
input show_shading = yes;
#input color_numbers_0to255 = yes;
#shading color is created by entering RGB color numbers, 0-255 , for red,green,blue
input shade_color_red_num0to255 = 70;
def cred = shade_color_red_num0to255;
input shade_color_green_num0to255 = 110;
def cgrn = shade_color_green_num0to255;
input shade_color_blue_num0to255 = 20;
def cblu = shade_color_blue_num0to255;

# show test data in labels and bubbles
input showtest = no;
def lastbar = !isnan(close[0]) and isnan(close[-1]);

# define the shading color
DefineGlobalColor( "shade1" , CreateColor(cred, cgrn, cblu));
addlabel(yes, market, GlobalColor( "shade1" ));

# -----------------------------------------------

#  calc total minutes , from midnight , to each time
def start1hr = Floor(start1 / 100);
def start1min = start1 - (start1hr * 100);
def start1minttl = (start1hr * 60) + start1min;
AddLabel(showtest, start1 + ".." + "st hrs=" + start1hr + ".." + start1min + ".." + start1minttl, Color.CYAN);

def end1hr = Floor(end1 / 100);
def end1min = end1 - (end1hr * 100);
def end1minttl = (end1hr * 60) + end1min;
AddLabel(showtest, end1 + ".." + "end hrs=" + end1hr + ".." + end1min + ".." + end1minttl, Color.RED);

# ===========================>>>>>>>>>>>>>>>>>>>>

#  elapsed min in current period
def stmin2 = SecondsFromTime(start1);
def stmin = stmin2 / (60);
AddLabel(showtest, "period min=" + stmin , Color.GREEN);

# ===========================>>>>>>>>>>>>>>>>>>>

#  is bar in a time period? , minutes in a day , 24 x 60 = 1440
def daymin = 1440;
def endz = 2359;
def startz = 0000;
def  first1 = if SecondsTillTime(start1) == 0 then 1 else 0;
def  last1 = if SecondsFromTime(end1) == 0 then 1 else 0;

def period1min;
def period1;
if start1minttl > end1minttl
then {
   # spans midnight , period=(24-start)+end , end to start
    period1min = (daymin - start1minttl) + end1minttl;
    period1 = if ((SecondsFromTime(start1) >= 0 and SecondsTillTime(endz) > 0) or ( SecondsFromTime(startz) >= 0 and SecondsTillTime(end1) > 0)) then 1 else 0;
} else {
   # ok , period=end-start , start to end
    period1min = (end1minttl - start1minttl);
    period1 = if SecondsFromTime(start1) >= 0 and SecondsTillTime(end1) > 0 then 1 else 0;
}

# ====================================

AddVerticalLine(ssevl and first1, "Start " + start1 , Color.GREEN, Curve.MEDIUM_DASH);
AddVerticalLine(ssevl and last1, "End " + end1 , Color.ORANGE, Curve.MEDIUM_DASH);

#  display time as hr:min ,  but trailing 0 not shown
#AddVerticalLine(ssevl and first1, "start " + start1hr + ":" + start1min , Color.GREEN, Curve.MEDIUM_DASH);
#AddVerticalLine(ssevl and last1, "end " + end1hr + ":" + end1min , Color.ORANGE, Curve.MEDIUM_DASH);
AddChartBubble(showtest, low - 1, period1, if period1 then Color.GREEN else Color.CYAN, no);

# midnight
def midn = 0000;
def midnite = if (smvl and SecondsTillTime(midn) == 0) then 1 else 0;
AddVerticalLine(midnite, "midnight" , Color.BLUE, Curve.MEDIUM_DASH);
AddLabel(showtest, "daymin=" + daymin + "  end1minttl=" + end1minttl + "  start1minttl=" + start1minttl, Color.GREEN);

AddLabel(showtest, "period1 min=" + period1min, Color.YELLOW);
AddLabel(scpl and period1, "hrs: " + round((period1min/60),1), GlobalColor("shade1"));

# -----------------------------------------------

# get chart agg minutes
def chagg = GetAggregationPeriod();
def aggmin = chagg / (1000 * 60);
AddLabel(showtest, "agg=" + aggmin, Color.CYAN);

# calc qty of bars of period1, for the current chart time
def period1bars = period1min / aggmin;
AddLabel(showtest, "period1 bars=" + period1bars, Color.YELLOW);

# ex. trade session = 6.5 hrs = 390 min
# 5 min chart = 390/5 = 78 bars  over 6.5 hours
# def len = 78;

# =====================<<<<<<<<<<<<<<<<<<<<<<

#  add check if in period and if last bar
def currentperiodbars = if (period1 and lastbar) then (stmin / aggmin) else 0;
AddLabel(showtest, "current period bars=" + currentperiodbars, Color.magenta);
AddLabel(scpl and period1, "bars " + currentperiodbars + "/" + period1bars, GlobalColor("shade1"));


#  find the high of active period, only on last bar
def xhi = fold xi = 0 to currentperiodbars
    with n = hi
    do Max(n, GetValue(hi, xi));

AddLabel(scpl and period1, "highest: " + xhi , GlobalColor("shade1"));

#  find the low of active period, only on last bar
def xlo = fold xj = 0 to currentperiodbars
    with m = lo
    do Min(m, GetValue(lo, xj));

AddLabel(scpl and period1, "lowest: " + xlo , GlobalColor("shade1"));

def len = period1bars - 1;

# ---------------------------------------------

# find high value for previous/complete time period, draw a line
def hi2 = if first1 then Highest(hi[-len], len + 1)
   else if last1 then na
   else if period1 then hi2[1]
   else na;

plot hiline = hi2;
hiline.SetDefaultColor(Color.WHITE);
#hiline.SetStyle(Curve.MEDIUM_DASH);
hiline.SetLineWeight(1);
hiline.HideBubble();

# ------------------------------------------------
# find low value for previous/complete time period, draw a line

def lo2 = if first1 then Lowest(lo[-len], len + 1)
   else if last1 then na
   else if period1 then lo2[1]
   else na;

plot loline = lo2;
loline.SetDefaultColor(Color.WHITE);
#loline.SetStyle(Curve.MEDIUM_DASH);
loline.SetLineWeight(1);
loline.HideBubble();

# ---------------------------------------------

# shading
def hiline2 = if show_shading then hiline else na;
def loline2 = if show_shading then loline else na;

#addcloud(hiline,loline,color.light_gray,color.light_gray);
AddCloud(hiline2, loline2, GlobalColor( "shade1" ), GlobalColor( "shade1" ) );
#

Wow this is awesome. I really like what you did with this. I've been implementing the Asia Range in my trading but I have question. I like to keep my charts clean, is there a way to only show the last period? Or last 24 hours? Just so it removes some of the boxes from the chart and less distraction for myself.
 
i think this will work.
could look for the first bar of a different day and read the open, and save it.

Code:
def diffday = GetDay() <> GetDay()[1];
def dayopen = if diffday then open else dayopen[1];
plot dayo = dayopen;
dayo.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dayo.SetDefaultColor(color.magenta);

This didn't quite work - The midnight vertical line is showing up at 22:00, but this line is printing off the candle at 23:00. I tried to tinker with the code but couldn't see a solution. Any ideas?
 
This didn't quite work - The midnight vertical line is showing up at 22:00, but this line is printing off the candle at 23:00. I tried to tinker with the code but couldn't see a solution. Any ideas?

This will determine the Closest time to Midnight (Eastern Time Zone) if there was no trading at Midnight.

If you are viewing on a chart with a local time setting other than Eastern Time Zone, the plot and vertical line will appear at the local time that Midnight occurred in the Eastern Time Zone (eg; local time Pacific Time Zone, Midnight Eastern Time Zone will appear at 2100 instead of 0000).

Capture.jpg
Ruby:
#Test to see if there is a bar at midnight. If not, use the closest_to_Midnight
def Closest_to_Midnight = if IsNaN(SecondsFromTime(0000)) then
                          Closest_to_Midnight[1] else
                          SecondsFromTime(0000);
#Due to Symbols have differing Times When the New Day's trading starts, then the prior Day's last Closet_to_Midnight result will be greater than the Next Day's Closest_to_Midnight result
def Midnight            = Closest_to_Midnight[1] > Closest_to_Midnight;

#Define and Plot a Horizontal Line the open at Midnight or the Closest_to_Midnight
def midnightopen  = if Midnight then open else midnightopen[1];
plot openmidnight = midnightopen;
openmidnight.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

#Verticcal Linea at Midnight
input show_vertical = yes;
AddVerticalLine(show_vertical and Midnight, "Midnight or if not then Closest to Time After Midnight ", Color.CYAN);
 
Back again - I really like this study for backtesting. I'd like to have a bubble show me the length of the range its showing. I've never successfully added a bubble so would appreciate any help. Here is what I tried, my code is at lines 68 and 148.

Code:
# hilolines_07

# abandon ver06
# start with a copy of ver05, not 05b. this works over midnight
#  add , wicks or body


# hilolines_06
#  removed too much in v05b. doesn't work over midnight
#   wicks or body


# hilolines_05
# 2020-07-17
# halcyonguy
# draw shading, bounded by start/stop times and highest/lowest levels
#  enter start and end times, (24 hour EST), for a period < 24 hours
#  can choose to show several parameters , labels, lines, shading
#   pick  /es  for testing.  trades most of day

declare hide_on_daily;

input candle_levels = {default "wick" , "body" };

input market = { default "U.S." , "EU" , "Asian" , "-"};

input start1_est = 0930;
input end1_est = 1600;
def start1 = start1_EST;
def end1 = end1_est;

input show_start_end_vertical_lines = yes;
def ssevl = show_start_end_vertical_lines;
input show_midnight_vertical_line = yes;
def smvl = show_midnight_vertical_line;
input show_current_period_labels = yes;
def scpl = show_current_period_labels;

def na = Double.NaN;
#def hi = high;
#def lo = low;
def bn = BarNumber();


# chg
def wickht = high - low;


# ---- wicks or body -------------------
#input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max( open, close);
  lowx = min( open, close);
}

# reassign hi lo  to new vars
def hi = highx;
def lo = lowx;

#Cribbage

Def RLength = hi-lo;

#Cribbage End

# ----------------------------------------



# color alts  gray 90,90,90    tan 70,70,70    grn 70,110,20
input show_shading = yes;
#input color_numbers_0to255 = yes;
#shading color is created by entering RGB color numbers, 0-255 , for red,green,blue
input shade_color_red_num0to255 = 70;
def cred = shade_color_red_num0to255;
input shade_color_green_num0to255 = 110;
def cgrn = shade_color_green_num0to255;
input shade_color_blue_num0to255 = 20;
def cblu = shade_color_blue_num0to255;

# show test data in labels and bubbles
input showtest = no;
def lastbar = !isnan(close[0]) and isnan(close[-1]);

# define the shading color
DefineGlobalColor( "shade1" , CreateColor(cred, cgrn, cblu));
addlabel(yes, market, GlobalColor( "shade1" ));

# -----------------------------------------------

#  calc total minutes , from midnight , to each time
def start1hr = Floor(start1 / 100);
def start1min = start1 - (start1hr * 100);
def start1minttl = (start1hr * 60) + start1min;
AddLabel(showtest, start1 + ".." + "st hrs=" + start1hr + ".." + start1min + ".." + start1minttl, Color.CYAN);

def end1hr = Floor(end1 / 100);
def end1min = end1 - (end1hr * 100);
def end1minttl = (end1hr * 60) + end1min;
AddLabel(showtest, end1 + ".." + "end hrs=" + end1hr + ".." + end1min + ".." + end1minttl, Color.RED);

# ===========================>>>>>>>>>>>>>>>>>>>>

#  elapsed min in current period
def stmin2 = SecondsFromTime(start1);
def stmin = stmin2 / (60);
AddLabel(showtest, "period min=" + stmin , Color.GREEN);

# ===========================>>>>>>>>>>>>>>>>>>>

#  is bar in a time period? , minutes in a day , 24 x 60 = 1440
def daymin = 1440;
def endz = 2359;
def startz = 0000;
def  first1 = if SecondsTillTime(start1) == 0 then 1 else 0;
def  last1 = if SecondsFromTime(end1) == 0 then 1 else 0;

def period1min;
def period1;
if start1minttl > end1minttl
then {
   # spans midnight , period=(24-start)+end , end to start
    period1min = (daymin - start1minttl) + end1minttl;
    period1 = if ((SecondsFromTime(start1) >= 0 and SecondsTillTime(endz) > 0) or ( SecondsFromTime(startz) >= 0 and SecondsTillTime(end1) > 0)) then 1 else 0;
} else {
   # ok , period=end-start , start to end
    period1min = (end1minttl - start1minttl);
    period1 = if SecondsFromTime(start1) >= 0 and SecondsTillTime(end1) > 0 then 1 else 0;
}

# ====================================

AddVerticalLine(ssevl and first1, "Start " + start1 , Color.GREEN, Curve.MEDIUM_DASH);
AddVerticalLine(ssevl and last1, "End " + end1 , Color.ORANGE, Curve.MEDIUM_DASH);

#  display time as hr:min ,  but trailing 0 not shown
#AddVerticalLine(ssevl and first1, "start " + start1hr + ":" + start1min , Color.GREEN, Curve.MEDIUM_DASH);
#AddVerticalLine(ssevl and last1, "end " + end1hr + ":" + end1min , Color.ORANGE, Curve.MEDIUM_DASH);

#Cribbage Start

input showrangelength = yes;

AddChartBubble(showrangelength, RLength, yes);

#Cribbage End

AddChartBubble(showtest, low - 1, period1, if period1 then Color.GREEN else Color.CYAN, no);

# midnight
def midn = 0000;
def midnite = if (smvl and SecondsTillTime(midn) == 0) then 1 else 0;
AddVerticalLine(midnite, "midnight" , Color.BLUE, Curve.MEDIUM_DASH);
AddLabel(showtest, "daymin=" + daymin + "  end1minttl=" + end1minttl + "  start1minttl=" + start1minttl, Color.GREEN);

AddLabel(showtest, "period1 min=" + period1min, Color.YELLOW);
AddLabel(scpl and period1, "hrs: " + round((period1min/60),1), GlobalColor("shade1"));

# -----------------------------------------------

# get chart agg minutes
def chagg = GetAggregationPeriod();
def aggmin = chagg / (1000 * 60);
AddLabel(showtest, "agg=" + aggmin, Color.CYAN);

# calc qty of bars of period1, for the current chart time
def period1bars = period1min / aggmin;
AddLabel(showtest, "period1 bars=" + period1bars, Color.YELLOW);

# ex. trade session = 6.5 hrs = 390 min
# 5 min chart = 390/5 = 78 bars  over 6.5 hours
# def len = 78;

# =====================<<<<<<<<<<<<<<<<<<<<<<

#  add check if in period and if last bar
def currentperiodbars = if (period1 and lastbar) then (stmin / aggmin) else 0;
AddLabel(showtest, "current period bars=" + currentperiodbars, Color.magenta);
AddLabel(scpl and period1, "bars " + currentperiodbars + "/" + period1bars, GlobalColor("shade1"));


#  find the high of active period, only on last bar
def xhi = fold xi = 0 to currentperiodbars
    with n = hi
    do Max(n, GetValue(hi, xi));

AddLabel(scpl and period1, "highest: " + xhi , GlobalColor("shade1"));

#  find the low of active period, only on last bar
def xlo = fold xj = 0 to currentperiodbars
    with m = lo
    do Min(m, GetValue(lo, xj));

AddLabel(scpl and period1, "lowest: " + xlo , GlobalColor("shade1"));

def len = period1bars - 1;

# ---------------------------------------------

# find high value for previous/complete time period, draw a line
def hi2 = if first1 then Highest(hi[-len], len + 1)
   else if last1 then na
   else if period1 then hi2[1]
   else na;

plot hiline = hi2;
hiline.SetDefaultColor(Color.WHITE);
#hiline.SetStyle(Curve.MEDIUM_DASH);
hiline.SetLineWeight(1);
hiline.HideBubble();

# ------------------------------------------------
# find low value for previous/complete time period, draw a line

def lo2 = if first1 then Lowest(lo[-len], len + 1)
   else if last1 then na
   else if period1 then lo2[1]
   else na;

plot loline = lo2;
loline.SetDefaultColor(Color.WHITE);
#loline.SetStyle(Curve.MEDIUM_DASH);
loline.SetLineWeight(1);
loline.HideBubble();

# ---------------------------------------------

# shading
def hiline2 = if show_shading then hiline else na;
def loline2 = if show_shading then loline else na;

#addcloud(hiline,loline,color.light_gray,color.light_gray);
AddCloud(hiline2, loline2, GlobalColor( "shade1" ), GlobalColor( "shade1" ) );
#
 
Back again - I really like this study for backtesting. I'd like to have a bubble show me the length of the range its showing. I've never successfully added a bubble so would appreciate any help. Here is what I tried, my code is at lines 68 and 148.

Code:
# hilolines_07

# abandon ver06
# start with a copy of ver05, not 05b. this works over midnight
#  add , wicks or body


# hilolines_06
#  removed too much in v05b. doesn't work over midnight
#   wicks or body


# hilolines_05
# 2020-07-17
# halcyonguy
# draw shading, bounded by start/stop times and highest/lowest levels
#  enter start and end times, (24 hour EST), for a period < 24 hours
#  can choose to show several parameters , labels, lines, shading
#   pick  /es  for testing.  trades most of day

declare hide_on_daily;

input candle_levels = {default "wick" , "body" };

input market = { default "U.S." , "EU" , "Asian" , "-"};

input start1_est = 0930;
input end1_est = 1600;
def start1 = start1_EST;
def end1 = end1_est;

input show_start_end_vertical_lines = yes;
def ssevl = show_start_end_vertical_lines;
input show_midnight_vertical_line = yes;
def smvl = show_midnight_vertical_line;
input show_current_period_labels = yes;
def scpl = show_current_period_labels;

def na = Double.NaN;
#def hi = high;
#def lo = low;
def bn = BarNumber();


# chg
def wickht = high - low;


# ---- wicks or body -------------------
#input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max( open, close);
  lowx = min( open, close);
}

# reassign hi lo  to new vars
def hi = highx;
def lo = lowx;

#Cribbage

Def RLength = hi-lo;

#Cribbage End

# ----------------------------------------



# color alts  gray 90,90,90    tan 70,70,70    grn 70,110,20
input show_shading = yes;
#input color_numbers_0to255 = yes;
#shading color is created by entering RGB color numbers, 0-255 , for red,green,blue
input shade_color_red_num0to255 = 70;
def cred = shade_color_red_num0to255;
input shade_color_green_num0to255 = 110;
def cgrn = shade_color_green_num0to255;
input shade_color_blue_num0to255 = 20;
def cblu = shade_color_blue_num0to255;

# show test data in labels and bubbles
input showtest = no;
def lastbar = !isnan(close[0]) and isnan(close[-1]);

# define the shading color
DefineGlobalColor( "shade1" , CreateColor(cred, cgrn, cblu));
addlabel(yes, market, GlobalColor( "shade1" ));

# -----------------------------------------------

#  calc total minutes , from midnight , to each time
def start1hr = Floor(start1 / 100);
def start1min = start1 - (start1hr * 100);
def start1minttl = (start1hr * 60) + start1min;
AddLabel(showtest, start1 + ".." + "st hrs=" + start1hr + ".." + start1min + ".." + start1minttl, Color.CYAN);

def end1hr = Floor(end1 / 100);
def end1min = end1 - (end1hr * 100);
def end1minttl = (end1hr * 60) + end1min;
AddLabel(showtest, end1 + ".." + "end hrs=" + end1hr + ".." + end1min + ".." + end1minttl, Color.RED);

# ===========================>>>>>>>>>>>>>>>>>>>>

#  elapsed min in current period
def stmin2 = SecondsFromTime(start1);
def stmin = stmin2 / (60);
AddLabel(showtest, "period min=" + stmin , Color.GREEN);

# ===========================>>>>>>>>>>>>>>>>>>>

#  is bar in a time period? , minutes in a day , 24 x 60 = 1440
def daymin = 1440;
def endz = 2359;
def startz = 0000;
def  first1 = if SecondsTillTime(start1) == 0 then 1 else 0;
def  last1 = if SecondsFromTime(end1) == 0 then 1 else 0;

def period1min;
def period1;
if start1minttl > end1minttl
then {
   # spans midnight , period=(24-start)+end , end to start
    period1min = (daymin - start1minttl) + end1minttl;
    period1 = if ((SecondsFromTime(start1) >= 0 and SecondsTillTime(endz) > 0) or ( SecondsFromTime(startz) >= 0 and SecondsTillTime(end1) > 0)) then 1 else 0;
} else {
   # ok , period=end-start , start to end
    period1min = (end1minttl - start1minttl);
    period1 = if SecondsFromTime(start1) >= 0 and SecondsTillTime(end1) > 0 then 1 else 0;
}

# ====================================

AddVerticalLine(ssevl and first1, "Start " + start1 , Color.GREEN, Curve.MEDIUM_DASH);
AddVerticalLine(ssevl and last1, "End " + end1 , Color.ORANGE, Curve.MEDIUM_DASH);

#  display time as hr:min ,  but trailing 0 not shown
#AddVerticalLine(ssevl and first1, "start " + start1hr + ":" + start1min , Color.GREEN, Curve.MEDIUM_DASH);
#AddVerticalLine(ssevl and last1, "end " + end1hr + ":" + end1min , Color.ORANGE, Curve.MEDIUM_DASH);

#Cribbage Start

input showrangelength = yes;

AddChartBubble(showrangelength, RLength, yes);

#Cribbage End

AddChartBubble(showtest, low - 1, period1, if period1 then Color.GREEN else Color.CYAN, no);

# midnight
def midn = 0000;
def midnite = if (smvl and SecondsTillTime(midn) == 0) then 1 else 0;
AddVerticalLine(midnite, "midnight" , Color.BLUE, Curve.MEDIUM_DASH);
AddLabel(showtest, "daymin=" + daymin + "  end1minttl=" + end1minttl + "  start1minttl=" + start1minttl, Color.GREEN);

AddLabel(showtest, "period1 min=" + period1min, Color.YELLOW);
AddLabel(scpl and period1, "hrs: " + round((period1min/60),1), GlobalColor("shade1"));

# -----------------------------------------------

# get chart agg minutes
def chagg = GetAggregationPeriod();
def aggmin = chagg / (1000 * 60);
AddLabel(showtest, "agg=" + aggmin, Color.CYAN);

# calc qty of bars of period1, for the current chart time
def period1bars = period1min / aggmin;
AddLabel(showtest, "period1 bars=" + period1bars, Color.YELLOW);

# ex. trade session = 6.5 hrs = 390 min
# 5 min chart = 390/5 = 78 bars  over 6.5 hours
# def len = 78;

# =====================<<<<<<<<<<<<<<<<<<<<<<

#  add check if in period and if last bar
def currentperiodbars = if (period1 and lastbar) then (stmin / aggmin) else 0;
AddLabel(showtest, "current period bars=" + currentperiodbars, Color.magenta);
AddLabel(scpl and period1, "bars " + currentperiodbars + "/" + period1bars, GlobalColor("shade1"));


#  find the high of active period, only on last bar
def xhi = fold xi = 0 to currentperiodbars
    with n = hi
    do Max(n, GetValue(hi, xi));

AddLabel(scpl and period1, "highest: " + xhi , GlobalColor("shade1"));

#  find the low of active period, only on last bar
def xlo = fold xj = 0 to currentperiodbars
    with m = lo
    do Min(m, GetValue(lo, xj));

AddLabel(scpl and period1, "lowest: " + xlo , GlobalColor("shade1"));

def len = period1bars - 1;

# ---------------------------------------------

# find high value for previous/complete time period, draw a line
def hi2 = if first1 then Highest(hi[-len], len + 1)
   else if last1 then na
   else if period1 then hi2[1]
   else na;

plot hiline = hi2;
hiline.SetDefaultColor(Color.WHITE);
#hiline.SetStyle(Curve.MEDIUM_DASH);
hiline.SetLineWeight(1);
hiline.HideBubble();

# ------------------------------------------------
# find low value for previous/complete time period, draw a line

def lo2 = if first1 then Lowest(lo[-len], len + 1)
   else if last1 then na
   else if period1 then lo2[1]
   else na;

plot loline = lo2;
loline.SetDefaultColor(Color.WHITE);
#loline.SetStyle(Curve.MEDIUM_DASH);
loline.SetLineWeight(1);
loline.HideBubble();

# ---------------------------------------------

# shading
def hiline2 = if show_shading then hiline else na;
def loline2 = if show_shading then loline else na;

#addcloud(hiline,loline,color.light_gray,color.light_gray);
AddCloud(hiline2, loline2, GlobalColor( "shade1" ), GlobalColor( "shade1" ) );
#

I just saw this. Make the change to your bubble code at 148 area with the following:

Code:
#Cribbage Start

input showrangelength = yes;

AddChartBubble(showrangelength, hi, RLength, color.yellow, yes);

This will place a bubble at the high of each bar with the Rlength.
 
Hi @halcyonguy I added your indicator and it works great, however the custom field wasn't working, so I instead edited your code to add a "Sydney" Case to your Switch statement and it will not show on the charts. I also changed the names of the text like U.S. to NY and H.K. to Tokyo and that all worked just fine. Only thing not working is getting the additional Case to show on the chart.
 
Hi @halcyonguy I added your indicator and it works great, however the custom field wasn't working, so I instead edited your code to add a "Sydney" Case to your Switch statement and it will not show on the charts. I also changed the names of the text like U.S. to NY and H.K. to Tokyo and that all worked just fine. Only thing not working is getting the additional Case to show on the chart.
i will try to look at it tonight.

what custom field?
these?
input custom_start1_est = 0930;
input custom_end1_est = 1600;

do you want a 4th time period added?
 
i will try to look at it tonight.

what custom field?
these?
input custom_start1_est = 0930;
input custom_end1_est = 1600;

do you want a 4th time period added?
Yes, when you go into the Market dropdown and select "choose" then enter a custom time, it doesn't work...I was trying to add the Sydney session...what I did instead of use choose, I modified the code per the paste below, but Sydney won't show on the chart (you'll notice I renamed the other options and updated their times as well and that all worked just fine, Sydney just doesn't show is all).

switch (market) {
case "NY":
# NY 8:00am to 5:00pm EST
startx = 0800;
stopx = 1700;
case "London":
# London 3:00am to 12:00pm EST
startx = 0300;
stopx = 1200;
case "Sydney":
# Sydney 5:00pm to 2:00am EST
startx = 1700;
stopx = 0200;
case "Tokyo":
# Tokyo 7:00pm to 4:00am EST
startx = 1900;
stopx = 0400;
case "choose":
startx = 0;
stopx = 0;
}
 
So it seems to have some issues around working on weekends, as the last24 doesn't capture anything. I'll test it over the week, and see what it starts grabbing later today (when global markets have opened). but for now, a quick fix is this replacement based on the GetDayofWeek() function ... which the docs say may not give the right day for futures. So take this with the usual disclaimer and grain of salt:

Code:
def dayOfWeek = GetDayOfWeek(GETyyyyMmDd());
def day_mult = if dayOfWeek == 5 then 3 else if dayOfWeek == 4 then 2 else 1;
def last24 = highestall(getTime()) - (day_mult * 86400 * 1000);

On my display, it looks like this:

AdxTSkE.png


The code for bubbles is like this:

Code:
AddChartBubble("time condition" = GMT == LondonOpen and London[-1] == 1, "price location" = LondonHighest, text = "London Open", color = Color.GREEN);

Happy Trading,
mashume
@mashume : I only have Tokyo Open and Close, could you please show the code with both Tokyo, London included. Also, it would be great if you can include
Broader EU : 3 am to 12:30 PM EST,
Germany: 3am to 11:00 AM,
India: 11:45 p.m to 6 a.m EST.

It would be nice to have an option to toggle these timezones to be displayed or Not. For eg: after 6 a.m EST, I can turn off India timezone
 
sorry @NickC i forgot to post my code.

this draws lines and clouds, to identify possible reversal time periods,

for a time period, enter start and stop times. draws a cloud, (shaded region)
if stop = 0 then it draws just a line.

Ruby:
# reversals_06a_study
# 2019-01-19
# halcyonguy

# draw vertical lines, at possible price reversals
# shade areas near timeProfile periods
#  https://www.tradermentality.com/2016/02/intraday-reversal-times.html


# --------------------------------------------------------------

# reversal times EST
# ranges  est
#  9:30-9:35    --- major
#  9:50-10:10   --- major
#  10:25-10:35

# times  est
#  11:15
#  12:00
#  12:45
#   1:30  --- major
#   2:15  --- major
#   3:00  --- major
#   3:30
#   4:01  --- major



# make sure all ranges have a start and stop time
#  if stop time =0 then  stop = start +1


# -- reference links ------------------------------------------------------------

# http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Date---Time/SecondsFromTime.html
# http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddVerticalLine.html
# http://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/GetColor.html

# -----------------------------------------------------------

#color = getcolor(colnum)
#  0=magenta, 1=cyan, 2=pink, 3=gray, 4=orange
#  5=red, 6=green, 7=dark gray, 8=yellow, 9=white


# time input data
# HHMM  EST   1200 = noon est   1020 = 10:20 est

# a value used if data is not able to be plotted
 def na = Double.NaN;


input display_vertical_lines = yes;
input display_shading = yes;

input line_color_0to9 = 1;
# 1=cyan

input shade_color_0to9 = 8;
# 8=yellow

# make shorter var names
def disvl = display_vertical_lines;
def diss = display_shading;

def linecol = line_color_0to9;
def shcol = shade_color_0to9;

input shade_width_minutes = 4;
input shade_ht = 4;
# shade_ht  is a % of price.  3 become   0.003 , or 0.3% , times the price, for a vertical offset, added to high  or subtracted from low

# convert shade width min to sec
def sh_wsec = shade_width_minutes * 60;

# devide shade ht/1000
def sh_ht = shade_ht / 1000;


input enter_times_in_24hour_est_hhmm = 0;

# ranges of time  est
input rng01_start = 0930;
input rng01_stop = 0935;

input rng02_start = 0950;
input rng02_stop = 1010;

input rng03_start = 1025;
input rng03_stop = 1035;

input rng04_start = 1115;
input rng04_stop = 0;

input rng05_start = 1200;
input rng05_stop = 0;

input rng06_start = 1245;
input rng06_stop = 0;

input rng07_start = 1330;
input rng07_stop = 0;

input rng08_start = 1415;
input rng08_stop = 0;

input rng09_start = 1500;
input rng09_stop = 0;

input rng10_start = 1530;
input rng10_stop = 0;

input rng11_start = 1601;
input rng11_stop = 0;

input rng12_start = 0;
input rng12_stop = 0;

input rng13_start = 0;
input rng13_stop = 0;

input rng14_start = 0;
input rng14_stop = 0;

input rng15_start = 0;
input rng15_stop = 0;


# convert start times from 24 to 12 hr , for displaying as text on start vertical lines

def r01starttxt = if rng01_start >= 1300 then rng01_start - 1200 else rng01_start;
def r02starttxt = if rng02_start >= 1300 then rng02_start - 1200 else rng02_start;
def r03starttxt = if rng03_start >= 1300 then rng03_start - 1200 else rng03_start;
def r04starttxt = if rng04_start >= 1300 then rng04_start - 1200 else rng04_start;
def r05starttxt = if rng05_start >= 1300 then rng05_start - 1200 else rng05_start;

def r06starttxt = if rng06_start >= 1300 then rng06_start - 1200 else rng06_start;
def r07starttxt = if rng07_start >= 1300 then rng07_start - 1200 else rng07_start;
def r08starttxt = if rng08_start >= 1300 then rng08_start - 1200 else rng08_start;
def r09starttxt = if rng09_start >= 1300 then rng09_start - 1200 else rng09_start;
def r10starttxt = if rng10_start >= 1300 then rng10_start - 1200 else rng10_start;

def r11starttxt = if rng11_start >= 1300 then rng11_start - 1200 else rng11_start;
def r12starttxt = if rng12_start >= 1300 then rng12_start - 1200 else rng12_start;
def r13starttxt = if rng13_start >= 1300 then rng13_start - 1200 else rng13_start;
def r14starttxt = if rng14_start >= 1300 then rng14_start - 1200 else rng14_start;
def r15starttxt = if rng15_start >= 1300 then rng15_start - 1200 else rng15_start;

# verify stop numbers  HHMM

def r01stop2 = if rng01_stop == 0 then rng01_start else rng01_stop;
def r02stop2 = if rng02_stop == 0 then rng02_start else rng02_stop;
def r03stop2 = if rng03_stop == 0 then rng03_start else rng03_stop;
def r04stop2 = if rng04_stop == 0 then rng04_start else rng04_stop;
def r05stop2 = if rng05_stop == 0 then rng05_start else rng05_stop;
def r06stop2 = if rng06_stop == 0 then rng06_start else rng06_stop;
def r07stop2 = if rng07_stop == 0 then rng07_start else rng07_stop;
def r08stop2 = if rng08_stop == 0 then rng08_start else rng08_stop;
def r09stop2 = if rng09_stop == 0 then rng09_start else rng09_stop;
def r10stop2 = if rng10_stop == 0 then rng10_start else rng10_stop;
def r11stop2 = if rng11_stop == 0 then rng11_start else rng11_stop;
def r12stop2 = if rng12_stop == 0 then rng12_start else rng12_stop;
def r13stop2 = if rng13_stop == 0 then rng13_start else rng13_stop;
def r14stop2 = if rng14_stop == 0 then rng14_start else rng14_stop;
def r15stop2 = if rng15_stop == 0 then rng15_start else rng15_stop;


# true/false , r01sh , display shading if within  (time period  +- shade width seconds )
# add shading around time of reversal

def r01sh = if ( diss and ( secondstillTime(rng01_start) <= sh_wsec) and ( secondsfromTime(r01stop2) <= sh_wsec )) then 1 else 0;
def r01hi = if r01sh then high + (close * sh_ht)  else na;
def r01lo = if r01sh then low - (close * sh_ht)  else na;
addcloud(r01hi,r01lo,getcolor(shcol),getcolor(shcol));

def r02sh = if ( diss and ( secondstillTime(rng02_start) <= sh_wsec) and ( secondsfromTime(r02stop2) <= sh_wsec )) then 1 else 0;
def r02hi = if r02sh then high + (close * sh_ht)  else na;
def r02lo = if r02sh then low - (close * sh_ht)  else na;
addcloud(r02hi,r02lo,getcolor(shcol),getcolor(shcol));

def r03sh = if ( diss and ( secondstillTime(rng03_start) <= sh_wsec) and ( secondsfromTime(r03stop2) <= sh_wsec )) then 1 else 0;
def r03hi = if r03sh then high + (close * sh_ht)  else na;
def r03lo = if r03sh then low - (close * sh_ht)  else na;
addcloud(r03hi,r03lo,getcolor(shcol),getcolor(shcol));

def r04sh = if ( diss and ( secondstillTime(rng04_start) <= sh_wsec) and ( secondsfromTime(r04stop2) <= sh_wsec )) then 1 else 0;
def r04hi = if r04sh then high + (close * sh_ht)  else na;
def r04lo = if r04sh then low - (close * sh_ht)  else na;
addcloud(r04hi,r04lo,getcolor(shcol),getcolor(shcol));

def r05sh = if ( diss and ( secondstillTime(rng05_start) <= sh_wsec) and ( secondsfromTime(r05stop2) <= sh_wsec )) then 1 else 0;
def r05hi = if r05sh then high + (close * sh_ht)  else na;
def r05lo = if r05sh then low - (close * sh_ht)  else na;
addcloud(r05hi,r05lo,getcolor(shcol),getcolor(shcol));

def r06sh = if ( diss and ( secondstillTime(rng06_start) <= sh_wsec) and ( secondsfromTime(r06stop2) <= sh_wsec )) then 1 else 0;
def r06hi = if r06sh then high + (close * sh_ht)  else na;
def r06lo = if r06sh then low - (close * sh_ht)  else na;
addcloud(r06hi,r06lo,getcolor(shcol),getcolor(shcol));

def r07sh = if ( diss and ( secondstillTime(rng07_start) <= sh_wsec) and ( secondsfromTime(r07stop2) <= sh_wsec )) then 1 else 0;
def r07hi = if r07sh then high + (close * sh_ht)  else na;
def r07lo = if r07sh then low - (close * sh_ht)  else na;
addcloud(r07hi,r07lo,getcolor(shcol),getcolor(shcol));

def r08sh = if ( diss and ( secondstillTime(rng08_start) <= sh_wsec) and ( secondsfromTime(r08stop2) <= sh_wsec )) then 1 else 0;
def r08hi = if r08sh then high + (close * sh_ht)  else na;
def r08lo = if r08sh then low - (close * sh_ht)  else na;
addcloud(r08hi,r08lo,getcolor(shcol),getcolor(shcol));

def r09sh = if ( diss and ( secondstillTime(rng09_start) <= sh_wsec) and ( secondsfromTime(r09stop2) <= sh_wsec )) then 1 else 0;
def r09hi = if r09sh then high + (close * sh_ht)  else na;
def r09lo = if r09sh then low - (close * sh_ht)  else na;
addcloud(r09hi,r09lo,getcolor(shcol),getcolor(shcol));


# 10-15
def r10sh = if ( diss and ( secondstillTime(rng10_start) <= sh_wsec) and ( secondsfromTime(r10stop2) <= sh_wsec )) then 1 else 0;
def r10hi = if r10sh then high + (close * sh_ht)  else na;
def r10lo = if r10sh then low - (close * sh_ht)  else na;
addcloud(r10hi,r10lo,getcolor(shcol),getcolor(shcol));

def r11sh = if ( diss and ( secondstillTime(rng11_start) <= sh_wsec) and ( secondsfromTime(r11stop2) <= sh_wsec )) then 1 else 0;
def r11hi = if r11sh then high + (close * sh_ht)  else na;
def r11lo = if r11sh then low - (close * sh_ht)  else na;
addcloud(r11hi,r11lo,getcolor(shcol),getcolor(shcol));

def r12sh = if ( diss and ( secondstillTime(rng12_start) <= sh_wsec) and ( secondsfromTime(r12stop2) <= sh_wsec )) then 1 else 0;
def r12hi = if r12sh then high + (close * sh_ht)  else na;
def r12lo = if r12sh then low - (close * sh_ht)  else na;
addcloud(r12hi,r12lo,getcolor(shcol),getcolor(shcol));

def r13sh = if ( diss and ( secondstillTime(rng13_start) <= sh_wsec) and ( secondsfromTime(r13stop2) <= sh_wsec )) then 1 else 0;
def r13hi = if r13sh then high + (close * sh_ht)  else na;
def r13lo = if r13sh then low - (close * sh_ht)  else na;
addcloud(r13hi,r13lo,getcolor(shcol),getcolor(shcol));

def r14sh = if ( diss and ( secondstillTime(rng14_start) <= sh_wsec) and ( secondsfromTime(r14stop2) <= sh_wsec )) then 1 else 0;
def r14hi = if r14sh then high + (close * sh_ht)  else na;
def r14lo = if r14sh then low - (close * sh_ht)  else na;
addcloud(r14hi,r14lo,getcolor(shcol),getcolor(shcol));

def r15sh = if ( diss and ( secondstillTime(rng15_start) <= sh_wsec) and ( secondsfromTime(r15stop2) <= sh_wsec )) then 1 else 0;
def r15hi = if r15sh then high + (close * sh_ht)  else na;
def r15lo = if r15sh then low - (close * sh_ht)  else na;
addcloud(r15hi,r15lo,getcolor(shcol),getcolor(shcol));




# ======== vertical lines ===================================================

#def linecol = line_color_0to9;
#  ex   getcolor(linecol)

#def disvl = display_vertical_lines;   true/false  master control

# display a line at start time, with the time listed as EST
# display a line at stop time, if stop is not =0

# stroke = curve.___    firm , long_dash , medium_dash , short_dash , points


# ------ vertical line at start time , with start time ------------------------------------

#  true/false  enable, draw vertical lines  start
def r01st = secondsFromTime(rng01_start)==0;
def r02st = secondsFromTime(rng02_start)==0;
def r03st = secondsFromTime(rng03_start)==0;
def r04st = secondsFromTime(rng04_start)==0;
def r05st = secondsFromTime(rng05_start)==0;
def r06st = secondsFromTime(rng06_start)==0;
def r07st = secondsFromTime(rng07_start)==0;
def r08st = secondsFromTime(rng08_start)==0;
def r09st = secondsFromTime(rng09_start)==0;
def r10st = secondsFromTime(rng10_start)==0;
def r11st = secondsFromTime(rng11_start)==0;
def r12st = secondsFromTime(rng12_start)==0;
def r13st = secondsFromTime(rng13_start)==0;
def r14st = secondsFromTime(rng14_start)==0;
def r15st = secondsFromTime(rng15_start)==0;


#  draw vertical line at start time , with start time
AddVerticalLine(disvl and r01st, "TIME EST - " + r01starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r02st, "TIME EST - " + r02starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r03st, "TIME EST - " + r03starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r04st, "TIME EST - " + r04starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r05st, "TIME EST - " + r05starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r06st, "TIME EST - " + r06starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r07st, "TIME EST - " + r07starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r08st, "TIME EST - " + r08starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r09st, "TIME EST - " + r09starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r10st, "TIME EST - " + r10starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r11st, "TIME EST - " + r11starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r12st, "TIME EST - " + r12starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r13st, "TIME EST - " + r13starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r14st, "TIME EST - " + r14starttxt, color = getcolor(linecol), stroke = Curve.short_dash);
AddVerticalLine(disvl and r15st, "TIME EST - " + r15starttxt, color = getcolor(linecol), stroke = Curve.short_dash);



# ------ vertical line at stop time ------------------------------------

#  true/false   enable , draw vertical line at stop time
def r01sp = if rng01_stop > 0 and secondsFromTime(rng01_stop)==0 then 1 else 0;
def r02sp = if rng02_stop > 0 and secondsFromTime(rng02_stop)==0 then 1 else 0;
def r03sp = if rng03_stop > 0 and secondsFromTime(rng03_stop)==0 then 1 else 0;
def r04sp = if rng04_stop > 0 and secondsFromTime(rng04_stop)==0 then 1 else 0;
def r05sp = if rng05_stop > 0 and secondsFromTime(rng05_stop)==0 then 1 else 0;
def r06sp = if rng06_stop > 0 and secondsFromTime(rng06_stop)==0 then 1 else 0;
def r07sp = if rng07_stop > 0 and secondsFromTime(rng07_stop)==0 then 1 else 0;
def r08sp = if rng08_stop > 0 and secondsFromTime(rng08_stop)==0 then 1 else 0;
def r09sp = if rng09_stop > 0 and secondsFromTime(rng09_stop)==0 then 1 else 0;
def r10sp = if rng10_stop > 0 and secondsFromTime(rng10_stop)==0 then 1 else 0;
def r11sp = if rng11_stop > 0 and secondsFromTime(rng11_stop)==0 then 1 else 0;
def r12sp = if rng12_stop > 0 and secondsFromTime(rng12_stop)==0 then 1 else 0;
def r13sp = if rng13_stop > 0 and secondsFromTime(rng13_stop)==0 then 1 else 0;
def r14sp = if rng14_stop > 0 and secondsFromTime(rng14_stop)==0 then 1 else 0;
def r15sp = if rng15_stop > 0 and secondsFromTime(rng15_stop)==0 then 1 else 0;



#  draw vertical line at stop
AddVerticalLine(disvl and r01sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r02sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r03sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r04sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r05sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r06sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r07sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r08sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r09sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r10sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r11sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r12sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r13sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r14sp, "", color = getcolor(linecol), stroke = Curve.POINTS);
AddVerticalLine(disvl and r15sp, "", color = getcolor(linecol), stroke = Curve.POINTS);

#

LA9CXjf.jpg

Wow...this is so awesome ...however it doesn't show TIME EST lines post 3:30 PM EST, may I know what I'm doing wrong here?



Workspace Linke:
http://tos.mx/8b2YDKi
 
Back again - I really like this study for backtesting. I'd like to have a bubble show me the length of the range its showing. I've never successfully added a bubble so would appreciate any help. Here is what I tried, my code is at lines 68 and 148.

Code:
# hilolines_07

# abandon ver06
# start with a copy of ver05, not 05b. this works over midnight
#  add , wicks or body


# hilolines_06
#  removed too much in v05b. doesn't work over midnight
#   wicks or body


# hilolines_05
# 2020-07-17
# halcyonguy
# draw shading, bounded by start/stop times and highest/lowest levels
#  enter start and end times, (24 hour EST), for a period < 24 hours
#  can choose to show several parameters , labels, lines, shading
#   pick  /es  for testing.  trades most of day

declare hide_on_daily;

input candle_levels = {default "wick" , "body" };

input market = { default "U.S." , "EU" , "Asian" , "-"};

input start1_est = 0930;
input end1_est = 1600;
def start1 = start1_EST;
def end1 = end1_est;

input show_start_end_vertical_lines = yes;
def ssevl = show_start_end_vertical_lines;
input show_midnight_vertical_line = yes;
def smvl = show_midnight_vertical_line;
input show_current_period_labels = yes;
def scpl = show_current_period_labels;

def na = Double.NaN;
#def hi = high;
#def lo = low;
def bn = BarNumber();


# chg
def wickht = high - low;


# ---- wicks or body -------------------
#input candle_levels = {default "wick" , "body" };
def highx;
def lowx;
switch (candle_levels) {
case "wick":
  highx = high;
  lowx = low;
case "body":
  highx = max( open, close);
  lowx = min( open, close);
}

# reassign hi lo  to new vars
def hi = highx;
def lo = lowx;

#Cribbage

Def RLength = hi-lo;

#Cribbage End

# ----------------------------------------



# color alts  gray 90,90,90    tan 70,70,70    grn 70,110,20
input show_shading = yes;
#input color_numbers_0to255 = yes;
#shading color is created by entering RGB color numbers, 0-255 , for red,green,blue
input shade_color_red_num0to255 = 70;
def cred = shade_color_red_num0to255;
input shade_color_green_num0to255 = 110;
def cgrn = shade_color_green_num0to255;
input shade_color_blue_num0to255 = 20;
def cblu = shade_color_blue_num0to255;

# show test data in labels and bubbles
input showtest = no;
def lastbar = !isnan(close[0]) and isnan(close[-1]);

# define the shading color
DefineGlobalColor( "shade1" , CreateColor(cred, cgrn, cblu));
addlabel(yes, market, GlobalColor( "shade1" ));

# -----------------------------------------------

#  calc total minutes , from midnight , to each time
def start1hr = Floor(start1 / 100);
def start1min = start1 - (start1hr * 100);
def start1minttl = (start1hr * 60) + start1min;
AddLabel(showtest, start1 + ".." + "st hrs=" + start1hr + ".." + start1min + ".." + start1minttl, Color.CYAN);

def end1hr = Floor(end1 / 100);
def end1min = end1 - (end1hr * 100);
def end1minttl = (end1hr * 60) + end1min;
AddLabel(showtest, end1 + ".." + "end hrs=" + end1hr + ".." + end1min + ".." + end1minttl, Color.RED);

# ===========================>>>>>>>>>>>>>>>>>>>>

#  elapsed min in current period
def stmin2 = SecondsFromTime(start1);
def stmin = stmin2 / (60);
AddLabel(showtest, "period min=" + stmin , Color.GREEN);

# ===========================>>>>>>>>>>>>>>>>>>>

#  is bar in a time period? , minutes in a day , 24 x 60 = 1440
def daymin = 1440;
def endz = 2359;
def startz = 0000;
def  first1 = if SecondsTillTime(start1) == 0 then 1 else 0;
def  last1 = if SecondsFromTime(end1) == 0 then 1 else 0;

def period1min;
def period1;
if start1minttl > end1minttl
then {
   # spans midnight , period=(24-start)+end , end to start
    period1min = (daymin - start1minttl) + end1minttl;
    period1 = if ((SecondsFromTime(start1) >= 0 and SecondsTillTime(endz) > 0) or ( SecondsFromTime(startz) >= 0 and SecondsTillTime(end1) > 0)) then 1 else 0;
} else {
   # ok , period=end-start , start to end
    period1min = (end1minttl - start1minttl);
    period1 = if SecondsFromTime(start1) >= 0 and SecondsTillTime(end1) > 0 then 1 else 0;
}

# ====================================

AddVerticalLine(ssevl and first1, "Start " + start1 , Color.GREEN, Curve.MEDIUM_DASH);
AddVerticalLine(ssevl and last1, "End " + end1 , Color.ORANGE, Curve.MEDIUM_DASH);

#  display time as hr:min ,  but trailing 0 not shown
#AddVerticalLine(ssevl and first1, "start " + start1hr + ":" + start1min , Color.GREEN, Curve.MEDIUM_DASH);
#AddVerticalLine(ssevl and last1, "end " + end1hr + ":" + end1min , Color.ORANGE, Curve.MEDIUM_DASH);

#Cribbage Start

input showrangelength = yes;

AddChartBubble(showrangelength, RLength, yes);

#Cribbage End

AddChartBubble(showtest, low - 1, period1, if period1 then Color.GREEN else Color.CYAN, no);

# midnight
def midn = 0000;
def midnite = if (smvl and SecondsTillTime(midn) == 0) then 1 else 0;
AddVerticalLine(midnite, "midnight" , Color.BLUE, Curve.MEDIUM_DASH);
AddLabel(showtest, "daymin=" + daymin + "  end1minttl=" + end1minttl + "  start1minttl=" + start1minttl, Color.GREEN);

AddLabel(showtest, "period1 min=" + period1min, Color.YELLOW);
AddLabel(scpl and period1, "hrs: " + round((period1min/60),1), GlobalColor("shade1"));

# -----------------------------------------------

# get chart agg minutes
def chagg = GetAggregationPeriod();
def aggmin = chagg / (1000 * 60);
AddLabel(showtest, "agg=" + aggmin, Color.CYAN);

# calc qty of bars of period1, for the current chart time
def period1bars = period1min / aggmin;
AddLabel(showtest, "period1 bars=" + period1bars, Color.YELLOW);

# ex. trade session = 6.5 hrs = 390 min
# 5 min chart = 390/5 = 78 bars  over 6.5 hours
# def len = 78;

# =====================<<<<<<<<<<<<<<<<<<<<<<

#  add check if in period and if last bar
def currentperiodbars = if (period1 and lastbar) then (stmin / aggmin) else 0;
AddLabel(showtest, "current period bars=" + currentperiodbars, Color.magenta);
AddLabel(scpl and period1, "bars " + currentperiodbars + "/" + period1bars, GlobalColor("shade1"));


#  find the high of active period, only on last bar
def xhi = fold xi = 0 to currentperiodbars
    with n = hi
    do Max(n, GetValue(hi, xi));

AddLabel(scpl and period1, "highest: " + xhi , GlobalColor("shade1"));

#  find the low of active period, only on last bar
def xlo = fold xj = 0 to currentperiodbars
    with m = lo
    do Min(m, GetValue(lo, xj));

AddLabel(scpl and period1, "lowest: " + xlo , GlobalColor("shade1"));

def len = period1bars - 1;

# ---------------------------------------------

# find high value for previous/complete time period, draw a line
def hi2 = if first1 then Highest(hi[-len], len + 1)
   else if last1 then na
   else if period1 then hi2[1]
   else na;

plot hiline = hi2;
hiline.SetDefaultColor(Color.WHITE);
#hiline.SetStyle(Curve.MEDIUM_DASH);
hiline.SetLineWeight(1);
hiline.HideBubble();

# ------------------------------------------------
# find low value for previous/complete time period, draw a line

def lo2 = if first1 then Lowest(lo[-len], len + 1)
   else if last1 then na
   else if period1 then lo2[1]
   else na;

plot loline = lo2;
loline.SetDefaultColor(Color.WHITE);
#loline.SetStyle(Curve.MEDIUM_DASH);
loline.SetLineWeight(1);
loline.HideBubble();

# ---------------------------------------------

# shading
def hiline2 = if show_shading then hiline else na;
def loline2 = if show_shading then loline else na;

#addcloud(hiline,loline,color.light_gray,color.light_gray);
AddCloud(hiline2, loline2, GlobalColor( "shade1" ), GlobalColor( "shade1" ) );
#
whats the use of adding bubbles?
 
So it seems to have some issues around working on weekends, as the last24 doesn't capture anything. I'll test it over the week, and see what it starts grabbing later today (when global markets have opened). but for now, a quick fix is this replacement based on the GetDayofWeek() function ... which the docs say may not give the right day for futures. So take this with the usual disclaimer and grain of salt:

Code:
def dayOfWeek = GetDayOfWeek(GETyyyyMmDd());
def day_mult = if dayOfWeek == 5 then 3 else if dayOfWeek == 4 then 2 else 1;
def last24 = highestall(getTime()) - (day_mult * 86400 * 1000);

On my display, it looks like this:

AdxTSkE.png


The code for bubbles is like this:

Code:
AddChartBubble("time condition" = GMT == LondonOpen and London[-1] == 1, "price location" = LondonHighest, text = "London Open", color = Color.GREEN);

Happy Trading,
mashume
@mashume : I just want the Low/High lines extended for each session ( London/Tokyo/EU) with a label showing instead of having cloud. Image showing but without a label, it would be great to have a label that says ( Tokyo High/Tokyo Low/London High/ London Low/EU High/EU Low ). Great work. Thank you.


 
So it seems to have some issues around working on weekends, as the last24 doesn't capture anything. I'll test it over the week, and see what it starts grabbing later today (when global markets have opened). but for now, a quick fix is this replacement based on the GetDayofWeek() function ... which the docs say may not give the right day for futures. So take this with the usual disclaimer and grain of salt:

Code:
def dayOfWeek = GetDayOfWeek(GETyyyyMmDd());
def day_mult = if dayOfWeek == 5 then 3 else if dayOfWeek == 4 then 2 else 1;
def last24 = highestall(getTime()) - (day_mult * 86400 * 1000);

On my display, it looks like this:

AdxTSkE.png


The code for bubbles is like this:

Code:
AddChartBubble("time condition" = GMT == LondonOpen and London[-1] == 1, "price location" = LondonHighest, text = "London Open", color = Color.GREEN);

Happy Trading,
mashume

@mashume today, I see that London Open/Long Active High are showing incorrect values as shown in the below screen shot. It is showing $3886.7 as London Active High...but current session started from 3 AM EST and the high should have been $3848.. Also, may I know what the variables hrange and lrange represent here?

workspace link: http://tos.mx/oUxTLrV
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
249 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top