how to change offset after market close for plotted line

greco26

Active member
I've been working on this for days and can't figure it out. The script plots lines at the prior day's high and low so that during RTH, I can see if intraday price is near the prior high/low. On another version of this study, I also have the week and month as well but I kept the code small for this post. Simple enough.

Assume today is Tuesday. What I want to do is once Tuesday's trading day is over (but is still the same day), I want to shift the plotted lines from Monday's close to today's closed bar (Tuesday). Then at Wednesday's open. the plotted lines should shift back to Tuesday's hi/lo.

I tried to create dynamic offsets but TOS doesn't like that. I tried to modify the code to add a variable to shift the plots forward after RTH but TOS doesn't like that. I've tried several other ways and can't figure out how to get it to work. Any advise would be awesome! @halcyonguy

Code:
# prevday_levels_onalldays_01
# halcyonguy
# 21-08-12
#
# find high, low, opens
# draw a horizontal line across the chart, at the price levels.
# can change the offset , to look at older periods.
# bubbles on the right, identify the lines.
#  the bubbles list the offset.

# Modifications by Tim G
#Sept 14, 2021
#Sept 26, 2021
#Nov 8, 2021


input showbubbles = yes;
input show1d  = yes;
input offset = 1;

# place bubbles after last bar, to id the lines

def futurebar = 3;
def x = (!IsNaN(close[futurebar]) and IsNaN(close[futurebar - 1]));


### 1d timeframe ###

def tf1d = AggregationPeriod.DAY;
def valid1d = GetAggregationPeriod() <= AggregationPeriod.DAY;
def h1d;
def l1d;
def l1dago;
def o1d;
def o1dc;
def c1d;


if valid1d and show1d {
    h1d = high (period = tf1d)[offset]; ;
    l1d = low  (period = tf1d) [offset];
    l1dago = low  (period = tf1d)[1];
    o1d = open (period = tf1d);
    o1dc =  open (period = tf1d);
    c1d = close(period = tf1d);


} else {
    h1d = Double.NaN;
    l1d = Double.NaN;
    l1dago = Double.NaN;
    o1d = Double.NaN;
    o1dc = Double.NaN;
    c1d = Double.NaN;
}


def bn1d = HighestAll(if open == o1dc then BarNumber()-1 else Double.NaN);

plot daylow = if BarNumber() < bn1d then Double.NaN else HighestAll( if (!IsNaN(close) and IsNaN(close[-1])) then l1d else double.nan);
plot daylow2 = if BarNumber() < bn1d then Double.NaN else HighestAll( if (!IsNaN(close) and IsNaN(close[-1]))then l1d else double.nan);
plot dayhigh = if BarNumber() < bn1d then Double.NaN else HighestAll( if (!IsNaN(close) and IsNaN(close[-1]))then h1d else double.nan);

plot dayhigh2 = if BarNumber() < bn1d then Double.NaN else HighestAll( if (!IsNaN(close) and IsNaN(close[-1]))then h1d else double.nan);

dayhigh.SetDefaultColor(Color.YELLOW);
dayhigh.SetStyle(Curve.SHORT_DASH);
dayhigh.SetLineWeight(1);
dayhigh2.SetDefaultColor(Color.CYAN);
dayhigh2.SetLineWeight(1);

daylow.SetDefaultColor(Color.YELLOW);
daylow.SetStyle(Curve.SHORT_DASH);
daylow.SetLineWeight(1);
daylow2.SetDefaultColor(Color.CYAN);
daylow2.SetLineWeight(1);

AddChartBubble(showbubbles and x, daylow, "D[" + offset + "]", Color.MAGENTA, yes);
AddChartBubble(showbubbles and x, dayhigh, "D[" + offset + "]", Color.GREEN, yes);
 

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

@halcyonguy Any chance you know how to do this?
maybe, i'm confused by this.
...from a 'close' to a high/low,
then back to a high/low... not back to a close?
are lines always from a high and low?


...shift the plotted lines from Monday's close to today's closed bar (Tuesday). Then at Wednesday's open. the plotted lines should shift back to Tuesday's hi/lo.
 
Sorry, I see what you mean and I did make that confusing. The lines will always be the high and low of the day (or the prior day). Not the close price. When I referenced 'close' I meant the close of RTH. Sorry about that...
 
Sorry, I see what you mean and I did make that confusing. The lines will always be the high and low of the day (or the prior day). Not the close price. When I referenced 'close' I meant the close of RTH. Sorry about that...
thanks for explaining. i will look at this this weekend.
 
What I've been trying to figure out is how to change these values so that I can shift the line plots forward after the market has closed for the day but the highest all part keep messing me up.

Code:
def bn1d = HighestAll(if open == o1d then BarNumber()-1  else double.nan);
plot daylow = if BarNumber() < bn1d then Double.NaN else HighestAll( if (!IsNaN(close) and IsNaN(close[-1])) then l1d else double.nan);
 
What I've been trying to figure out is how to change these values so that I can shift the line plots forward after the market has closed for the day but the highest all part keep messing me up.

Code:
def bn1d = HighestAll(if open == o1d then BarNumber()-1  else double.nan);
plot daylow = if BarNumber() < bn1d then Double.NaN else HighestAll( if (!IsNaN(close) and IsNaN(close[-1])) then l1d else double.nan);

See if this helps. It uses the def RTH in the sethiding function to show previous days high/low until post market hours on the current day and then uses the current days high/low. In testing this in ONDemand, it required me to use 1630 instead of 1600 to determine post market. It works, but hopefully in realtime it will work with 1600.

Edit: Had a chance to test this during the trading day today and I think this version works the way you wanted. You can choose to eliminate the future lines plots at the input hidenextdaylines.

The first image below is during RTHs and shows the previous day's high/low and the difference between hidenextdaylines and not below.

Capture1.jpg

The second image is post market and automatically plots the high/low of the current day. This will remain through the RTH the next as it becomes the previous day.
Capture2.jpg
Ruby:
def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
input hidenextdaylines = no;

def rth = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) and SecondsFromTime(0100) >= 0  and SecondsFromTime(1600) < 0 then 1 else 0);

plot xdh = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow(displace = 0);
xdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdh.SetHiding(rth == 1);

plot xpdh = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow();
xpdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xpdh.SetHiding(rth == 0);

plot xdl = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow(displace = 0).DailyLow;
xdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdl.SetHiding(rth == 1);

plot xpdl = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow().DailyLow;
xpdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xpdl.SetHiding(rth == 0);
 
Last edited:
Awesome, this looks great. I modified it a bit to plot the lines across the chart and doubled everything for a candy cane line look. Fancy stuff! :) Hopefully my mods won't mess up the code but I think it should be fine. I'll see tomorrow and let you know. Thank you again! And thank you @halcyonguy too!!


Code:
def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
input hidenextdaylines = no;

def rth = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) and SecondsFromTime(0100) >= 0  and SecondsFromTime(1600) < 0 then 1 else 0);

### 1d timeframe ###



def bn1d = HighestAll(if open == OPEN then BarNumber()  else double.nan);

plot xdh1 = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DAILYHIGHLOW(DISPLACE = 0));
xdh1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdh1.SetHiding(rth == 1);
xdh1.SetDefaultColor(Color.YELLOW);
xdh1.SetStyle(Curve.SHORT_DASH);
xdh1.SetLineWeight(1);
plot xdhB = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow());
xdhB.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdhB.SetHiding(rth == 0);
xdhB.SetDefaultColor(Color.YELLOW);
xdhB.SetStyle(Curve.SHORT_DASH);
xdhB.SetLineWeight(1);

plot xdh2 = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DAILYHIGHLOW(DISPLACE = 0));
xdh2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdh2.SetHiding(rth == 1);
xdh2.SetDefaultColor(Color.CYAN);
xdh2.SetLineWeight(1);
plot xdh2B = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow());
xdh2B.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdh2B.SetHiding(rth == 0);
xdh2B.SetDefaultColor(Color.CYAN);
xdh2B.SetLineWeight(1);


plot xdL1 = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DAILYHIGHLOW(DISPLACE = 0).DAILYLOW);
xdL1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdL1.SetHiding(rth == 1);
xdL1.SetDefaultColor(Color.YELLOW);
xdL1.SetStyle(Curve.SHORT_DASH);
xdL1.SetLineWeight(1);
plot xdLB = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow().DAILYLOW);
xdLB.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdLB.SetHiding(rth == 0);
xdLB.SetDefaultColor(Color.YELLOW);
xdLB.SetStyle(Curve.SHORT_DASH);
xdLB.SetLineWeight(1);

plot xdL2 = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DAILYHIGHLOW(DISPLACE = 0).DAILYLOW);
xdL2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdL2.SetHiding(rth == 1);
xdL2.SetDefaultColor(Color.CYAN);
xdL2.SetLineWeight(1);
plot xdhLB = if BarNumber() < bn1d then Double.NaN else highestall(if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else DailyHighLow().DAILYLOW);
xdhLB.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdhLB.SetHiding(rth == 0);
xdhLB.SetDefaultColor(Color.CYAN);
xdhLB.SetLineWeight(1);
 
I guess I messes something up in my code. The plotted lines shifted to today's high and low after market opened instead of staying on yesterday's high and low. Any thoughts on what I did wrong?
 
I guess I messes something up in my code. The plotted lines shifted to today's high and low after market opened instead of staying on yesterday's high and low. Any thoughts on what I did wrong?

The part of my code using RTH to set hiding does not work on Daily and Above Charts. However, here is a workaround that you may want to try. It uses an intraday chart (the images below are a 1hr chart) with the following code and a multitime candle overlay set at DAY. Both studies are in the same lower panel and the show price graph is unchecked in general settings. This allows the use of the intraday timeframe to determine when the trading session ends as you want with only psuedo daily candles showing.

The first image is the previous day's high/low extended over the current daily candle. The second image is the auto movement of the lines after the current day's trading session.

The last code below is the multi-timeframe candle overlay code shown in the image.

Capture.jpg
Capture.jpg
Ruby:
def ymd      = GetYYYYMMDD();
def candles  = !IsNaN(close);
def capture  = candles and ymd != ymd[1];
def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
def thisDay  = (HighestAll(dayCount) - dayCount) ;
input hidenextdaylines = no;

#Exended Dailyhighlow Lines-------------------------------------------------------------
def dhext = if isnan(DailyHighLow(displace = 0)) then dhext[1] else DailyHighLow(displace = 0);
def dhpext= if isnan(DailyHighLow(displace = 0)) then dhpext[1] else dailyhighlow();
def dlext = if isnan(DailyHighLow(displace = 0)) then dlext[1] else DailyHighLow(displace = 0).DailyLow;
def dlpext= if isnan(DailyHighLow(displace = 0)) then dlpext[1] else DailyHighLow().DailyLow;

#RTH Used to Hide Dailyhighlow lines----------------------------------------------------
def rth = HighestAll(if IsNaN(close[-1]) and !IsNaN(close) and SecondsFromTime(0100) >= 0  and SecondsFromTime(1600) < 0 then 1 else 0);
#---------------------------------------------------------------------------------------

plot xdh = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else dhext;
xdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdh.SetHiding(rth == 1);

plot xpdh = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else dhpext;
xpdh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xpdh.SetHiding(rth == 0);

plot xdl = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else dlext;
xdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xdl.SetHiding(rth == 1);

plot xpdl = if hidenextdaylines and IsNaN(close[1]) or thisDay > 0 then Double.NaN else dlpext;
xpdl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
xpdl.SetHiding(rth == 0);

Multi_timeframe candle overlay
Ruby:
input showcloud = yes;
input agg  = AggregationPeriod.hour;

plot o = open(period = agg);
plot c = close(period = agg);
plot h  = high(period = agg);
plot l   = low(period = agg);
h.AssignValueColor(if o > c then Color.RED else Color.GREEN);
l.AssignValueColor(if o > c then Color.RED else Color.GREEN);
o.setdefaultColor(color.white);
c.setdefaultColor(color.yellow);
o.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
c.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
h.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
l.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

AddCloud(if showcloud and o > c then o else double.nan, c, Color.LIGHT_RED, color2 = Color.LIGHT_red);
AddCloud(if showcloud then h else Double.NaN, if o>o then o else c, Color.gray, color2 = Color.gray);
AddCloud(if showcloud and o < c then c else double.nan, o, Color.LIGHT_green, color2 = Color.LIGHT_green);
AddCloud(if showcloud then if o > c then c else o else double.nan, l, Color.gray, color2 = Color.gray);
def dn = if h < h[1] and l<= l[1]
         then 1
         else if dn[1] == 1 and l < l[1] and c[1] > c
         then 1
         else 0;
def up = if h >= h[1] and l > l[1]
         then 1
         else if up[1] == 1 and h > h[1] and c > c[1]
         then 1
         else 0;
input pricecolor = no;
AssignPriceColor(if pricecolor
                 then if up == 1
                      then Color.GREEN
                      else if dn == 1
                      then Color.RED
                      else Color.YELLOW
                 else Color.CURRENT);
 

Attachments

  • Capture.jpg
    Capture.jpg
    71.5 KB · Views: 184
Oh wooooowww! This is so awesome. It really is amazing what you guys think of and how you’re able to code it. I love learning stuff like this. Thank you so much.
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
288 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