shows the previous day's premarket high and low instead of the previous day's regular trading hours high/low.

autoloader

Member
VIP
This code is exactly how I want it except it still shows the previous day's premarket high and low instead of the previous day's regular trading hours high/low.

Has anyone ever figured out a fix for this?

https://usethinkscript.com/threads/...-open-of-day-atr-lines-for-thinkorswim.13139/
Code:
# Previous Day High/Low/Close + Premarket High/Low + High/Low/Open of Day
# Some code based on code by Mobius
# Created by Wiinii

# tweaked to show previous day's HLC and premarket hi/lo only

declare hide_on_daily;

input extend_plots = yes;
input length = 1;
input showOnlyLastPeriod = yes;
input ShowBubbles = yes;
input locate_bubbles_at_time = 0930;


def bn = BarNumber();
def na = Double.NaN;
def h  = high;
def l  = low;
def o = open;
def c = close;
def v = volume;
def aggregationPeriod = AggregationPeriod.DAY;
def displace = -1;
def timeopen = SecondsFromTime(locate_bubbles_at_time) == 0;

#----- Previous Day High/Low/Close -----#

plot PD_High;
plot PD_Low;
plot PD_Close;
def PD_H = if extend_plots and isnan(close)
           then PD_H[1]
           else Highest(high(period = aggregationPeriod)[-displace], length);
def PD_L = if extend_plots and isnan(close)
           then PD_L[1]
           else Lowest(low(period = aggregationPeriod)[-displace], length);
def PD_C = if extend_plots and isnan(close)
           then PD_C[1]
           else close(period = aggregationPeriod)[-displace];
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PD_High = na;
    PD_Low = na;
    PD_Close = na;
} else {
    PD_High  = PD_H;
    PD_Low   = PD_L;
    PD_Close = PD_C;
}

PD_High.SetDefaultColor(color.DARK_ORANGE);
PD_High.SetpaintingStrategy(paintingStrategy.HORIZONTAL);
PD_High.SetLineWeight(2);
PD_High.HideTitle();

PD_Low.SetDefaultColor(color.DARK_ORANGE);
PD_Low.SetpaintingStrategy(paintingStrategy.HORIZONTAL);
PD_Low.SetLineWeight(2);
PD_Low.HideTitle();

PD_Close.SetDefaultColor(color.WHITE);
PD_Close.SetPaintingStrategy(paintingStrategy.HORIZONTAL);
PD_Close.SetLineWeight(2);
PD_Close.HideTitle();

input bubble_placement = {default right_edge, timeopen};

def right_edge = barnumber() == highestall(barnumber() - 1);

DefineGlobalColor("PD_High", CreateColor(255,204,153));
AddChartBubble(ShowBubbles and
if bubble_placement == bubble_placement.timeopen
then timeopen
else right_edge, PD_High, "PD High", GlobalColor("PD_High"));

DefineGlobalColor("PD_Low", CreateColor(255,204,153));
AddChartBubble(ShowBubbles and
if bubble_placement == bubble_placement.timeopen
then timeopen
else right_edge, PD_Low, "PD Low", GlobalColor("PD_Low"), no);

DefineGlobalColor("PD_Close", color.WHITE);
AddChartBubble(ShowBubbles and
if bubble_placement == bubble_placement.timeopen
then timeopen
else right_edge, PD_Close, "PD Close", GlobalColor("PD_Close"));


#----- Premarket High/Low -----#

def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def vol = if GlobeX and !GlobeX[1]
then v
else if GlobeX
then vol[1] + v
else na;

def GlobeX_Volume = vol;

def ONhigh = if GlobeX and !GlobeX[1]
then h
else if GlobeX and
h > ONhigh[1]
then h
else ONhigh[1];

def ONhighBar = if GlobeX and h == ONhigh
then bn
else na;

def ONlow = if GlobeX and !GlobeX[1]
then l
else if GlobeX and
l < ONlow[1]
then l
else ONlow[1];

def ONlowBar = if GlobeX and l == ONlow
then bn
else na;

def OverNightHigh = if BarNumber() == HighestAll(ONhighBar)
then ONhigh
else OverNightHigh[1];

def OverNightLow = if BarNumber() == HighestAll(ONlowBar)
then ONlow
else OverNightLow[1];

plot PM_High;
plot PM_Low;
def  PM_H = if extend_plots and isnan(close)
            then PM_H[1]
            else if OverNightHigh > 0 then OverNightHigh else na;
def  PM_L = if extend_plots and isnan(close)
            then PM_L[1]
            else if OverNightLow > 0 then OverNightLow else na;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PM_High = na;
    PM_Low = na;
} else {
    PM_High = PM_H;
    PM_Low  = PM_L;
}

#PM_High.SetHiding(!PlotOverNightExtremes);
PM_High.SetLineWeight(2);
PM_High.SetDefaultColor(color.CYAN);
PM_High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PM_High.HideBubble();
PM_High.HideTitle();

#PM_Low.SetHiding(!PlotOverNightExtremes);
PM_Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PM_Low.SetDefaultColor(Color.CYAN);
PM_Low.HideBubble();
PM_Low.HideTitle();

DefineGlobalColor("PM_High", CreateColor(102,255,255));
AddChartBubble(ShowBubbles and right_edge, PM_High, "PM High", GlobalColor("PM_High"));

DefineGlobalColor("PM_Low", CreateColor(102,255,255));
AddChartBubble(ShowBubbles and right_edge, PM_Low, "PM Low", GlobalColor("PM_Low"), no);
 
Last edited by a moderator:
Solution
I let Gemini give this a shot just to see how it did. This what you're looking for? Took it a few tries to get it right, but adjust the market times you're looking for and give it a look.

C:
# Title: Previous_Day_RTH_High_Low
# Author: Gemini
# Version: 1.6
# Date: 2025-04-17
# Description: Plots the previous trading day's RTH high/low. Uses
#              SecondsFromTime/SecondsTillTime for reliable time checking in ET.
#              RTH is defined as 9:00 AM to 4:00 PM ET.
#
# Define Regular Trading Hours (RTH) time window (9:00 AM to 4:00 PM ET)
# Use SecondsFromTime and SecondsTillTime relative to Exchange Time Zone.
# Bar starts at or after 09:00 ET AND bar starts before 16:00 ET.
def isRTH = SecondsFromTime(0900) >= 0 and...
I let Gemini give this a shot just to see how it did. This what you're looking for? Took it a few tries to get it right, but adjust the market times you're looking for and give it a look.

C:
# Title: Previous_Day_RTH_High_Low
# Author: Gemini
# Version: 1.6
# Date: 2025-04-17
# Description: Plots the previous trading day's RTH high/low. Uses
#              SecondsFromTime/SecondsTillTime for reliable time checking in ET.
#              RTH is defined as 9:00 AM to 4:00 PM ET.
#
# Define Regular Trading Hours (RTH) time window (9:00 AM to 4:00 PM ET)
# Use SecondsFromTime and SecondsTillTime relative to Exchange Time Zone.
# Bar starts at or after 09:00 ET AND bar starts before 16:00 ET.
def isRTH = SecondsFromTime(0900) >= 0 and SecondsTillTime(1600) > 0;

# Detect the start of a new trading day
def isNewDay = GetDay() != GetDay()[1];
# Alternatively, use GetYYYYMMDD():
# def isNewDay = GetYYYYMMDD() != GetYYYYMMDD()[1];

# --- Calculate RTH High for the Current Day ---
rec rthHighToday = if isNewDay then
                       # On a new day, if the first bar is RTH, start with its high.
                       if isRTH then high else Double.NaN
                   else if isRTH then
                       # During RTH, update with the maximum of the previous value and current high.
                       if IsNaN(rthHighToday[1]) then high else Max(rthHighToday[1], high)
                   else
                       # Outside RTH, carry forward the last known RTH high for today.
                       rthHighToday[1];

# --- Calculate RTH Low for the Current Day ---
rec rthLowToday = if isNewDay then
                      # On a new day, if the first bar is RTH, start with its low.
                      if isRTH then low else Double.NaN
                  else if isRTH then
                      # During RTH, update with the minimum of the previous value and current low.
                      if IsNaN(rthLowToday[1]) then low else Min(rthLowToday[1], low)
                  else
                      # Outside RTH, carry forward the last known RTH low for today.
                      rthLowToday[1];

# --- Capture Previous Day's RTH High and Low ---
# These values update only once per day, at the start of the new day.
rec prevDayRTHHigh = if isNewDay then
                         # Capture the final calculated RTH high from the previous day's last bar.
                         rthHighToday[1]
                     else
                         # Keep the value constant throughout the current day.
                         prevDayRTHHigh[1];

rec prevDayRTHLow = if isNewDay then
                        # Capture the final calculated RTH low from the previous day's last bar.
                        rthLowToday[1]
                    else
                        # Keep the value constant throughout the current day.
                        prevDayRTHLow[1];

# --- Plot the Previous Day Lines ---
# Only plot if the previous day's value is a valid number (not NaN).
plot PrevRTH_High = if !IsNaN(prevDayRTHHigh) then prevDayRTHHigh else Double.NaN;
PrevRTH_High.SetDefaultColor(Color.GREEN);
PrevRTH_High.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevRTH_High.SetStyle(Curve.SHORT_DASH);
PrevRTH_High.SetLineWeight(2);

plot PrevRTH_Low = if !IsNaN(prevDayRTHLow) then prevDayRTHLow else Double.NaN;
PrevRTH_Low.SetDefaultColor(Color.RED);
PrevRTH_Low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
PrevRTH_Low.SetStyle(Curve.SHORT_DASH);
PrevRTH_Low.SetLineWeight(2);

# --- Optional: Keep Debug Plots for Confirmation ---
# You can uncomment/unhide these later if the main plots work.
plot Debug_RTH_High = rthHighToday;
Debug_RTH_High.SetDefaultColor(Color.CYAN);
Debug_RTH_High.SetLineWeight(1);
Debug_RTH_High.SetStyle(Curve.POINTS);
Debug_RTH_High.Hide(); # Hide by default

plot Debug_RTH_Low = rthLowToday;
Debug_RTH_Low.SetDefaultColor(Color.MAGENTA);
Debug_RTH_Low.SetLineWeight(1);
Debug_RTH_Low.SetStyle(Curve.POINTS);
Debug_RTH_Low.Hide(); # Hide by default

# Optional: Plot RTH flag based on new logic
plot IsRTH_Flag = isRTH;
IsRTH_Flag.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
IsRTH_Flag.SetDefaultColor(Color.YELLOW);
IsRTH_Flag.HideTitle();
IsRTH_Flag.Hide(); # Hide by default

# --- Optional Labels ---
 AddLabel(yes, "Prev RTH High: " + (if !IsNaN(PrevRTH_High) then AsPrice(PrevRTH_High) else "N/A"), Color.GREEN);
 AddLabel(yes, "Prev RTH Low: " + (if !IsNaN(PrevRTH_Low) then AsPrice(PrevRTH_Low) else "N/A"), Color.RED);

1744901698377.png
 
Solution

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
260 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