adjustment to script for futures open vs RTH

Smith1158

New member
I updated the below, because I found the answer to my question about the difference between Futures and RTH for calculating the open.
https://usethinkscript.com/threads/pre-market-after-market-for-thinkorswim.75/#post-330


Hello,
I have found a ton of really great scripts on here for marking levels such as ONL, ONH, yesterdays close and today open. Unfortunately, since I trade futures I'm running into an issue because the Open and Close time differs from RTH and I would like to calculate yesterday's RTH close, High and low, but at 1600 yesterday not 1800 which is when Futures close. I also want to mark the open, but since Futures open isn't at 0930 I'm having issues. I will probably need to adjust a few different scripts, so I'd like have an idea how to present the argument. I have also been searching for some hours to find the answer, but haven't found any and even found people asking for it, but to no avail.


Here is an example of a prev. day close script that is calculating based on 1800 and not 1600

input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = no;
input deviation = 20;

plot PreviousDayClose;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) { PreviousDayClose = Double.NaN;}
else { PreviousDayClose = Highest(close(period = aggregationPeriod)[-displace], length);}

PreviousDayClose.SetLineWeight(3);
PreviousDayClose.SetDefaultColor(Color.GREEN);
 
Last edited:
Solution
I updated the below, because I found the answer to my question about the difference between Futures and RTH for calculating the open.
https://usethinkscript.com/threads/pre-market-after-market-for-thinkorswim.75/#post-330


Hello,
I have found a ton of really great scripts on here for marking levels such as ONL, ONH, yesterdays close and today open. Unfortunately, since I trade futures I'm running into an issue because the Open and Close time differs from RTH and I would like to calculate yesterday's RTH close, High and low, but at 1600 yesterday not 1800 which is when Futures close. I also want to mark the open, but since Futures open isn't at 0930 I'm having issues. I will probably need to adjust a few different scripts...
I updated the below, because I found the answer to my question about the difference between Futures and RTH for calculating the open.
https://usethinkscript.com/threads/pre-market-after-market-for-thinkorswim.75/#post-330


Hello,
I have found a ton of really great scripts on here for marking levels such as ONL, ONH, yesterdays close and today open. Unfortunately, since I trade futures I'm running into an issue because the Open and Close time differs from RTH and I would like to calculate yesterday's RTH close, High and low, but at 1600 yesterday not 1800 which is when Futures close. I also want to mark the open, but since Futures open isn't at 0930 I'm having issues. I will probably need to adjust a few different scripts, so I'd like have an idea how to present the argument. I have also been searching for some hours to find the answer, but haven't found any and even found people asking for it, but to no avail.


Here is an example of a prev. day close script that is calculating based on 1800 and not 1600

input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = no;
input deviation = 20;

plot PreviousDayClose;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) { PreviousDayClose = Double.NaN;}
else { PreviousDayClose = Highest(close(period = aggregationPeriod)[-displace], length);}

PreviousDayClose.SetLineWeight(3);
PreviousDayClose.SetDefaultColor(Color.GREEN);

This plots the RTH (0930:1600) for HLCO for the previous day on today's chart

Capture.jpg
Ruby:
#Prior Day HLC RegularTradingHours
input rthbegin = 0930;
input rthend   = 1600;
def pastOpen   = If((SecondsTillTime(rthbegin) > 0), 0, 1);
def pastClose  = If((SecondsTillTime(rthend) > 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def date       = GetYYYYMMDD();
def c = close;
def h = high;
def l = low;
def o = open;

#Highest High during RTH from Previous Day Determined
def begin      = CompoundValue(1, if getday()==getday()[1]
                                  then if SecondsFromTime(rthbegin) == 0
                                       then h
                                       else if h > begin[1] and marketOpen
                                       then h
                                       else begin[1]
                                  else 0, begin[1]);
def beginbn    = if GetDay() == GetLastDay()
                 then 0
                 else if getday()==getday()[1]
                      then if h == begin and marketOpen
                           then BarNumber()
                           else beginbn[1]
                 else 0;
def beginprice = if getday()==getday()[1]
                 then if BarNumber() == HighestAll(beginbn)
                      then h
                      else beginprice[1]
                 else beginprice[1];

#Lowest Low During RTH from Previous Day Determined
def begin1      = CompoundValue(1, if getday()==getday()[1]
                                   then if SecondsFromTime(rthbegin) == 0
                                        then l
                                        else if l < begin1[1] and marketOpen
                                        then l
                                        else begin1[1]
                                   else 0, l);
def beginbn1    = if GetDay() == GetLastDay()
                  then 0
                  else if l == begin1
                  then BarNumber()
                  else beginbn1[1];
def beginprice1 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn1)
                       then l
                       else beginprice1[1]
                  else beginprice1[1];

#Close During RTH from Previous Day Determined
def begin2      = CompoundValue(1, if date == date[1]
                                   then if SecondsFromTime(rthend) == 0
                                        then c
                                        else begin2[1]
                                   else 0, c);
def beginbn2    = if GetDay() == GetLastDay()
                  then 0
                  else if c == begin2
                       then BarNumber()
                       else beginbn2[1];
def beginprice2 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn2)
                       then c
                       else beginprice2[1]
                  else beginprice2[1];

#Open During RTH from Previous Day Determined
def begin3      = CompoundValue(1, if date == date[1]
                                   then if SecondsFromTime(rthbegin) == 0
                                        then o
                                        else begin3[1]
                                   else 0, 0);
def beginbn3    = if GetDay() == GetLastDay()
                  then 0
                  else if date==date[1] and  o == (begin3)
                       then BarNumber()
                       else beginbn3[1];
def beginprice3 = if date == date[1]
                  then if BarNumber() == (beginbn3)
                       then o
                       else beginprice3[1]
                  else beginprice3[1];

plot ph = if getday()!=getlastday() then double.nan else beginprice;
ph.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ph.setdefaultColor(color.green);
plot pl = if getday()!=getlastday() then double.nan else beginprice1;
pl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl.setdefaultColor(color.red);
plot pc = if getday()!=getlastday() then double.nan else beginprice2;
pc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pc.setdefaultColor(color.yellow);
plot po = if getday()!=getlastday() then double.nan else beginprice3;
po.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
po.setdefaultColor(color.white);

input showbubbles_Prior_OHLC = yes;
input n = 5;
def  n1 = n + 1;
def StartPlot = if showbubbles_Prior_OHLC == yes
                then (IsNaN(c[n]) and !IsNaN(c[n1]))
                else Double.NaN;

AddChartBubble(StartPlot,  Round(ph[n1], 2), "PH-" + astext(Round(ph[n1], 2)), Color.GREEN,   yes);
AddChartBubble(StartPlot,  Round(pl[n1], 2), "PL-" + astext(Round(pl[n1], 2)), Color.RED,     yes);
AddChartBubble(StartPlot,  Round(pc[n1], 2), "PC-" + astext(Round(pc[n1], 2)), Color.YELLOW,  yes);
AddChartBubble(StartPlot,  Round(po[n1], 2), "PO-" + astext(Round(po[n1], 2)), Color.white,  yes);
 
Solution
This plots the RTH (0930:1600) for HLCO for the previous day on today's chart

Beautiful! Is there anyway that Premarket high and lows can be added but for Futures times as well? Ive tried different scripts but they account for different times it seems like.

Also, previous close is different from RTH for futures( I trade /ES). Anyway to adjust that as well so it can close RTH instead of after hours?
 
Beautiful! Is there anyway that Premarket high and lows can be added but for Futures times as well? Ive tried different scripts but they account for different times it seems like.

Also, previous close is different from RTH for futures( I trade /ES). Anyway to adjust that as well so it can close RTH instead of after hours?

See if these 2 scripts patched into 1 script that I previously did will provide want you want for futures.

Capture.jpg
Ruby:
#Example_PreviousOHLC_Futures
input rthbegin = 0930;
input rthend   = 1615;
def   dow      = getdayofweek(getyyyYMMDD());
def lastopen   = if getday()==getlastday()- (if dow==5 then 3 else 1) and secondsfromtime(rthbegin)==0
                 then open
                 else lastopen[1];
def lastopenbn = if getday()==getlastday()- (if dow==5 then 3 else 1) and secondsfromtime(rthbegin)==0
                 then barnumber()
                 else lastopenbn[1];
plot po = if barnumber()>=highestall(lastopenbn) then lastopen else double.nan;


def lastclose    = CompoundValue(1, if SecondsTillTime(rthend)[1] > 0 and
                                       SecondsTillTime(rthend) <= 0 or
                                      (SecondsTillTime(rthend)[1] < SecondsTillTime(rthend) and
                                       SecondsTillTime(rthend)[1] > 0)
                            then close
                            else lastclose[1], close);
def lastclosebn = CompoundValue(1, if SecondsTillTime(rthend)[1] > 0 and
                                   SecondsTillTime(rthend) <= 0 or
                                  (SecondsTillTime(rthend)[1] < SecondsTillTime(rthend) and
                                   SecondsTillTime(rthend)[1] > 0)
                            then barnumber()
                            else lastclosebn[1], 1);
plot pc = if barnumber()>=highestall(lastclosebn) then lastclose else Double.NaN;
pc.SetPaintingStrategy(PaintingStrategy.DASHES);
pc.SetDefaultColor(Color.yellow);

def pastOpen   = If((SecondsTillTime(rthbegin) > 0), 0, 1);
def pastClose  = If((SecondsTillTime(rthend) > 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def date       = GetYYYYMMDD();
def c = close;
def h = high;
def l = low;
def o = open;

#Highest High during RTH from Previous Day Determined
def begin      = CompoundValue(1, if getday()==getday()[1]
                                  then if SecondsFromTime(rthbegin) == 0
                                       then h
                                       else if h > begin[1] and marketOpen
                                       then h
                                       else begin[1]
                                  else 0, begin[1]);
def beginbn    = if GetDay() == GetLastDay()
                 then 0
                 else if getday()==getday()[1]
                      then if h == begin and marketOpen
                           then BarNumber()
                           else beginbn[1]
                 else 0;
def beginprice = if getday()==getday()[1]
                 then if BarNumber() == HighestAll(beginbn)
                      then h
                      else beginprice[1]
                 else beginprice[1];

#Lowest Low During RTH from Previous Day Determined
def begin1      = CompoundValue(1, if date == date[1]
                                   then if SecondsFromTime(rthbegin) == 0
                                        then l
                                        else if l < begin1[1] and marketOpen
                                        then l
                                        else begin1[1]
                                   else 0, l);
def beginbn1    = if GetDay() == GetLastDay()
                  then 0
                  else if l == begin1
                  then BarNumber()
                  else beginbn1[1];
def beginprice1 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn1)
                       then l
                       else beginprice1[1]
                  else beginprice1[1];

plot ph = if getday() == getlastday() then beginprice[1] else double.nan ;
ph.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ph.setdefaultColor(color.green);
plot pl = beginprice1[1];
pl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl.setdefaultColor(color.red);

#Premarket_HL_Futures

script pre {
    input daysback = 0;
    def ymd      = GetYYYYMMDD();
    def capture  = !IsNaN(close) and ymd != ymd[1];
    def dayCount = CompoundValue(1, if capture then dayCount[1] + 1 else dayCount[1], 0);
    def thisDay  = HighestAll(dayCount) - dayCount  ;

    def phigh    = if thisDay[1] == daysback + 1 and thisDay == daysback
                   then high
                   else if thisDay == daysback and GetTime() < RegularTradingStart(GetYYYYMMDD())
                   then  Max(high, phigh[1]) else phigh[1];
    def plow    = if thisDay[1] == daysback + 1 and thisDay == daysback
                  then low
                  else if thisDay == daysback and GetTime() < RegularTradingStart(GetYYYYMMDD())
                  then  Min(low, plow[1]) else plow[1];
    plot ph     = if thisday > daysback  then double.nan else phigh;
    plot pl     = if thisday > daysback  then double.nan else plow;
}
plot preh = pre();
plot prel = pre().pl;

input showbubbles_Prior_OHLC = yes;
input n = 5;
def  n1 = n + 1;
def StartPlot = if showbubbles_Prior_OHLC == yes
                then (IsNaN(c[n]) and !IsNaN(c[n1]))
                else Double.NaN;

AddChartBubble(StartPlot,  Round(ph[n1], 2), "PH-\n" + astext(Round(ph[n1], 2)), Color.GREEN,   yes);
AddChartBubble(StartPlot,  Round(pl[n1], 2), "PL-\n" + astext(Round(pl[n1], 2)), Color.RED,     yes);
AddChartBubble(StartPlot,  Round(pc[n1], 2), "PC-\n" + astext(Round(pc[n1], 2)), Color.YELLOW,  yes);
AddChartBubble(StartPlot,  Round(po[n1], 2), "PO-\n" + astext(Round(po[n1], 2)), Color.white,  yes);
AddChartBubble(StartPlot,  Round(preh[n1], 2), "PreH-\n" + astext(Round(preh[n1], 2)), Color.GREEN,   yes);
AddChartBubble(StartPlot,  Round(prel[n1], 2), "PreL-\n" + astext(Round(prel[n1], 2)), Color.RED,     yes);
 
Thank you very much for this script! I have been looking for an RTH ONLY (0930-1600) OHLC script for ES for a while. Despite numerous attempts to code it myself, I could never get RTH only, I always ended up getting ETH values.

That being said, I took a couple cracks at it, but the code is a bit above my current skill level.. does anyone know how to modify the script to plot the SAME values, RTH (0930-1600) OHLC, but for the day BEFORE yesterday?

Much thanks in advance!
 
Thank you very much for this script! I have been looking for an RTH ONLY (0930-1600) OHLC script for ES for a while. Despite numerous attempts to code it myself, I could never get RTH only, I always ended up getting ETH values.

That being said, I took a couple cracks at it, but the code is a bit above my current skill level.. does anyone know how to modify the script to plot the SAME values, RTH (0930-1600) OHLC, but for the day BEFORE yesterday?

Much thanks in advance!

This will allow you to display on the current day the RTH OHLC for the day at the input daysback. The image below shows those from 2 days ago as you were seeking.

Capture.jpg
Ruby:
input daysback = 2;

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

#Prior Day HLC RegularTradingHours
input rthbegin = 0930;
input rthend   = 1600;
def pastOpen   = If((SecondsTillTime(rthbegin) > 0), 0, 1);
def pastClose  = If((SecondsTillTime(rthend) > 0), 0, 1);
def marketOpen = If(pastOpen and !pastClose, 1, 0);
def date       = GetYYYYMMDD();
def c = close;
def h = high;
def l = low;
def o = open;

#Highest High during RTH from Previous Day Determined
def begin      = CompoundValue(1, if thisday==daysback
                                  then if SecondsFromTime(rthbegin) == 0
                                       then h
                                       else if h > begin[1] and marketOpen
                                       then h
                                       else begin[1]
                                  else 0, begin[1]);
def beginbn    = if GetDay() == GetLastDay()
                 then 0
                 else if getday()==getday()[1]
                      then if h == begin and marketOpen
                           then BarNumber()
                           else beginbn[1]
                 else 0;
def beginprice = if getday()==getday()[1]
                 then if BarNumber() == HighestAll(beginbn)
                      then h
                      else beginprice[1]
                 else beginprice[1];

#Lowest Low During RTH from Previous Day Determined
def begin1      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthbegin) == 0
                                        then l
                                        else if l < begin1[1] and marketOpen
                                        then l
                                        else begin1[1]
                                   else 0, l);
def beginbn1    = if GetDay() == GetLastDay()
                  then 0
                  else if l == begin1
                  then BarNumber()
                  else beginbn1[1];
def beginprice1 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn1)
                       then l
                       else beginprice1[1]
                  else beginprice1[1];

#Close During RTH from Previous Day Determined
def begin2      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthend) == 0
                                        then c
                                        else begin2[1]
                                   else 0, c);
def beginbn2    = if GetDay() == GetLastDay()
                  then 0
                  else if c == begin2
                       then BarNumber()
                       else beginbn2[1];
def beginprice2 = if date == date[1]
                  then if BarNumber() == HighestAll(beginbn2)
                       then c
                       else beginprice2[1]
                  else beginprice2[1];

#Open During RTH from Previous Day Determined
def begin3      = CompoundValue(1, if thisday==daysback
                                   then if SecondsFromTime(rthbegin) == 0
                                        then o
                                        else begin3[1]
                                   else 0, 0);
def beginbn3    = if GetDay() == GetLastDay()
                  then 0
                  else if date==date[1] and  o == (begin3)
                       then BarNumber()
                       else beginbn3[1];
def beginprice3 = if date == date[1]
                  then if BarNumber() == (beginbn3)
                       then o
                       else beginprice3[1]
                  else beginprice3[1];

plot ph = if getday()!=getlastday() then double.nan else beginprice;
ph.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ph.setdefaultColor(color.green);
plot pl = if getday()!=getlastday() then double.nan else beginprice1;
pl.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pl.setdefaultColor(color.red);
plot pc = if getday()!=getlastday() then double.nan else beginprice2;
pc.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
pc.setdefaultColor(color.yellow);
plot po = if getday()!=getlastday() then double.nan else beginprice3;
po.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
po.setdefaultColor(color.white);


input showbubbles_Prior_OHLC = yes;
input n = 5;
def  n1 = n + 1;
def StartPlot = if showbubbles_Prior_OHLC == yes
                then (IsNaN(c[n]) and !IsNaN(c[n1]))
                else Double.NaN;

AddChartBubble(StartPlot,  Round(ph[n1], 2), "PH"+ daysback + "-" + astext(Round(ph[n1], 2)), Color.GREEN,   yes);
AddChartBubble(StartPlot,  Round(pl[n1], 2), "PL"+ daysback + "-" + astext(Round(pl[n1], 2)), Color.RED,     yes);
AddChartBubble(StartPlot,  Round(pc[n1], 2), "PC"+ daysback + "-" + astext(Round(pc[n1], 2)), Color.YELLOW,  yes);
AddChartBubble(StartPlot,  Round(po[n1], 2), "PO"+ daysback + "-" + astext(Round(po[n1], 2)), Color.white,  yes);

plot ph1 = if getday()!=getlastday() then double.nan else beginprice *1.025;
ph1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
ph1.setdefaultColor(color.green);
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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