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

autoloader

Active 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
That's fantastic. Thank you very much. I just saw your post yesterday so that's why I didn't respond earlier. I never got a notice that my post was responded to so I just assumed it faded away without a response.
 
I also made my own variation of this by adding weekly highs and lows and to allow me to use a percentage of yesterday's ATR (I set mine to use 80% of it as I am conservative). Feel free to use.

Code:
# Previous Day High/Low/Close + Premarket High/Low + High/Low/Open of Day + ATR Lines
# Created by Wiinii
# V1.6
# Some code based on code by Mobius (premarke) and TraderKevin (ATR lines
# https://usethinkscript.com/threads/previous-day-high-low-close-premarket-high-low-high-low-open-of-day-for-thinkorswim.13139/

declare hide_on_daily;
input extend_plots = yes;
input length = 1;
input showOnlyLastPeriod = yes;
input ShowBubbles = yes;
input locate_bubbles_at = { Expansion, default Time};
input locate_bubbles_at_time = 600;
input BarsFromExpansion = 1;
input ShowPricesInBubbles = yes;
input ShowPremarket = yes;

def bn = BarNumber();
def na = Double.NaN;
def h  = high;
def l  = low;
def o = open;
def c = close;
def v = volume;
input aggregationPeriod = AggregationPeriod.DAY;
def displace = -1;
def timeopen = SecondsFromTime(locate_bubbles_at_time) == 0;
def isExpansion = locate_bubbles_at == locate_bubbles_at.Expansion and IsNaN(close);
def firstExpansionBar = if !IsNaN(close[-1]) and isExpansion then 1 else if isExpansion then firstExpansionBar[1] + 1 else 0;
def BubbleLocation = if locate_bubbles_at == locate_bubbles_at.Time then timeopen else isExpansion and firstExpansionBar == BarsFromExpansion;

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


plot PD_High;
plot PD_Low;
plot PD_Close;
plot PD_Open;
plot PW_High;
plot PW_Low;

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];
def PD_O = if extend_plots and IsNaN(close)
           then PD_O[1]
           else open(period = aggregationPeriod)[-displace];

def PW_H = if extend_plots and IsNaN(close)
           then PW_H[1]
           else High(period = aggregationPeriod.WEEK)[-displace];
          

def PW_L = if extend_plots and IsNaN(close)
           then PW_L[1]
           else Low(period = aggregationPeriod.WEEK)[-displace];



if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PD_High = na;
    PD_Low = na;
    PD_Close = na;
    PD_Open = na;
    PW_High = na;
    PW_Low = na;
} else {
    PD_High  = PD_H;
    PD_Low   = PD_L;
    PD_Close = PD_C;
    PD_Open = PD_O;
    PW_High = PW_H;
    PW_Low = PW_L;
}
;

PD_High.SetDefaultColor(Color.DARK_ORANGE);
PD_High.SetStyle(Curve.LONG_DASH);
PD_High.SetLineWeight(2);
PD_High.HideTitle();

PD_Low.SetDefaultColor(Color.DARK_ORANGE);
PD_Low.SetStyle(Curve.LONG_DASH);
PD_Low.SetLineWeight(2);
PD_Low.HideTitle();

PD_Close.SetDefaultColor(Color.WHITE);
PD_Close.SetStyle(Curve.LONG_DASH);
PD_Close.SetLineWeight(2);
PD_Close.HideTitle();

PD_Open.SetDefaultColor(Color.GRAY);
PD_Open.SetStyle(Curve.LONG_DASH);
PD_Open.SetLineWeight(2);
PD_Open.HideTitle();


PW_High.SetDefaultColor(Color.PLUM);
PW_High.SetStyle(Curve.LONG_DASH);
PW_High.SetLineWeight(2);
PW_High.HideTitle();


PW_Low.SetDefaultColor(Color.pink);
PW_Low.SetStyle(Curve.LONG_DASH);
PW_Low.SetLineWeight(2);
PW_Low.HideTitle();





DefineGlobalColor("PD_High", CreateColor(255, 204, 153));
DefineGlobalColor("PD_Low", CreateColor(255, 204, 153));
DefineGlobalColor("PD_Close", Color.WHITE);
DefineGlobalColor("PD_Open", Color.GRAY);
DefineGlobalColor("PW_High", Color.PLUM);
DefineGlobalColor("PW_Low", Color.PINK);

AddChartBubble(ShowBubbles and BubbleLocation , PD_High, "PrevDayH: " + (if ShowPricesInBubbles then AsText(PD_High) else ""), GlobalColor("PD_High"));
AddChartBubble(ShowBubbles and BubbleLocation, PD_Low, "PrevDayL: " + (if ShowPricesInBubbles then AsText(PD_Low) else ""), GlobalColor("PD_Low"), no);
AddChartBubble(ShowBubbles and BubbleLocation , PD_Close, "PrevDayC: " + (if ShowPricesInBubbles then AsText(PD_Close) else ""), GlobalColor("PD_Close"));
AddChartBubble(ShowBubbles and BubbleLocation , PD_Open, "PrevDayO: " + (if ShowPricesInBubbles then AsText(PD_Open) else ""), GlobalColor("PD_Open"));

AddChartBubble(ShowBubbles and BubbleLocation, PW_High, "PrevWeekH: " + (if ShowPricesInBubbles then AsText(PW_High) else ""), GlobalColor("PW_High"));
AddChartBubble(ShowBubbles and BubbleLocation, PW_LOW, "PrevWeekL: " + (if ShowPricesInBubbles then AsText(PW_LOW) else ""), GlobalColor("PW_LOW"), no);


PD_High.HideBubble();
PD_Low.HideBubble();
PD_Close.HideBubble();
PD_Open.HideBubble();


#----- Premarket High/Low -----# Thanks to Mobius

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;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PM_High = na;
    PM_Low = na;
} else {
    PM_High = if OverNightHigh > 0 then OverNightHigh else na;
    PM_Low = if OverNightLow > 0 then OverNightLow else na;
}

#PM_High.SetHiding(!PlotOverNightExtremes);
PM_High.SetLineWeight(2);
PM_High.SetDefaultColor(Color.CYAN);
PM_High.SetStyle(Curve.LONG_DASH);
PM_High.HideBubble();
PM_High.HideTitle();

#PM_Low.SetHiding(!PlotOverNightExtremes);
PM_Low.SetStyle(Curve.LONG_DASH);
PM_Low.SetDefaultColor(Color.CYAN);
PM_Low.HideBubble();
PM_Low.HideTitle();

DefineGlobalColor("PM_High", CreateColor(102, 255, 255));
AddChartBubble(ShowBubbles and ShowPremarket and if locate_bubbles_at == locate_bubbles_at.Time then bn == ONhighBar else isExpansion and firstExpansionBar == BarsFromExpansion, PM_High, "PMH: " + (if ShowPricesInBubbles then AsText(PM_High) else ""), GlobalColor("PM_High"));

DefineGlobalColor("PM_Low", CreateColor(102, 255, 255));
AddChartBubble(ShowBubbles  and ShowPremarket and if locate_bubbles_at == locate_bubbles_at.Time then bn == ONlowBar else isExpansion and firstExpansionBar == BarsFromExpansion, PM_Low, "PML: " + (if ShowPricesInBubbles then AsText(PM_Low) else ""), GlobalColor("PM_Low"), no);


#----- Today Open/High/Low -----#


plot HODPlot;
plot LODPlot;
plot DayOpenPlot;

def High_of_Day = if extend_plots and IsNaN(close)
           then High_of_Day[1]
           else Highest(high(period = aggregationPeriod), length);
def Low_of_Day = if extend_plots and IsNaN(close)
           then Low_of_Day[1]
           else Lowest(low(period = aggregationPeriod), length);
def DayOpen = if extend_plots and IsNaN(close)
           then DayOpen[1]
           else open(period = aggregationPeriod);
          
    if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    HODPlot = na;
    LODPlot = na;
    DayOpenPlot = na;
    
} else {
    HODPlot = High_of_Day;
    LODPlot = Low_of_Day;
    DayOpenPlot = DayOpen;

 
}

DayOpenPlot.SetDefaultColor (Color.GRAY);
DayOpenPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
DayOpenPlot.SetLineWeight(2);
DayOpenPlot.HideTitle();

HODPlot.SetDefaultColor(CreateColor(0, 128, 255));
HODPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HODPlot.SetLineWeight(2);
HODPlot.HideTitle();

LODPlot.SetDefaultColor(CreateColor(0, 128, 255));
LODPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LODPlot.SetLineWeight(2);
LODPlot.HideTitle();








DefineGlobalColor("Open", Color.LIGHT_GRAY);
DefineGlobalColor("High_of_Day", CreateColor(102, 178, 255));
DefineGlobalColor("Low_of_Day", CreateColor(102, 178, 255));
AddChartBubble(ShowBubbles and BubbleLocation, DayOpen, "Open: " + (if ShowPricesInBubbles then AsText(DayOpen) else ""), GlobalColor("Open"));
AddChartBubble(ShowBubbles and BubbleLocation, High_of_Day, "HOD: " + (if ShowPricesInBubbles then AsText(High_of_Day) else ""), GlobalColor("High_of_Day"));
AddChartBubble(ShowBubbles and BubbleLocation, Low_of_Day, "LOD: " + (if ShowPricesInBubbles then AsText(Low_of_Day) else ""), GlobalColor("Low_of_Day"), no);

#Point Of Control

def highestHigh = if PD_High >= High_of_Day then PD_High else High_of_Day;
def LowestLow = if PD_Low <= Low_of_Day then PD_Low else Low_of_Day;



#----- ATR Lines -----# Thaanks to TraderKevin
input showAtrLines = Yes;
input atrLinesFrom = {default pdc, dayOpen};
input ATRlength = 14;
input averageType = AverageType.WILDERS;
input ATRPct = 80;

def ATR = (MovingAverage(averageType, TrueRange(high(period = ”DAY”)[1], close(period = ”DAY”)[1], low(period = ”DAY”)[1]), ATRlength)) *(ATRPct/100);


def hatr = if extend_plots and IsNaN(close)then hatr[1] else if atrLinesFrom  == atrLinesFrom .dayOpen then DayOpen + ATR else PD_Close + ATR;
def latr = if extend_plots and IsNaN(close)then latr[1] else if atrLinesFrom  == atrLinesFrom .dayOpen then DayOpen - ATR else PD_Close - ATR;

plot hatrplot= hatr;
plot latrplot= latr;
hatrplot.SetLineWeight(5);
hatrplot.SetDefaultColor(Color.RED);
hatrplot.SetStyle(Curve.LONG_DASH);
hatrplot.HideBubble();
hatrplot.HideTitle();
hatrplot.SetHiding(showAtrLines == no);

latrplot.SetLineWeight(5);
latrplot.SetStyle(Curve.LONG_DASH);
latrplot.SetDefaultColor(Color.GREEN);
latrplot.HideBubble();
latrplot.HideTitle();
latrplot.SetHiding(showAtrLines == no);

def POC = if extend_plots and isnan(close) then poc[1] else(highestHigh + LowestLow) / 2;
plot POCPlot = POC;

DefineGlobalColor("POCPlot", CreateColor(121, 121, 121));
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, POCPlot, "POC: " + (if ShowPricesInBubbles then AsText(POCPlot) else ""), GlobalColor("POCPlot"), no);

DefineGlobalColor("hatrplot", Color.light_red);
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, hatrplot, "ATRH: " + (if ShowPricesInBubbles then AsText(hatrplot) else ""), GlobalColor("hatrplot"));

DefineGlobalColor("latrplot", Color.LIGHT_GREEN);
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, latrplot, "ATRL: " + (if ShowPricesInBubbles then AsText(latrplot) else ""), GlobalColor("latrplot"), no);
 
I also made my own variation of this by adding weekly highs and lows and to allow me to use a percentage of yesterday's ATR (I set mine to use 80% of it as I am conservative). Feel free to use.

Code:
# Previous Day High/Low/Close + Premarket High/Low + High/Low/Open of Day + ATR Lines
# Created by Wiinii
# V1.6
# Some code based on code by Mobius (premarke) and TraderKevin (ATR lines
# https://usethinkscript.com/threads/previous-day-high-low-close-premarket-high-low-high-low-open-of-day-for-thinkorswim.13139/

declare hide_on_daily;
input extend_plots = yes;
input length = 1;
input showOnlyLastPeriod = yes;
input ShowBubbles = yes;
input locate_bubbles_at = { Expansion, default Time};
input locate_bubbles_at_time = 600;
input BarsFromExpansion = 1;
input ShowPricesInBubbles = yes;
input ShowPremarket = yes;

def bn = BarNumber();
def na = Double.NaN;
def h  = high;
def l  = low;
def o = open;
def c = close;
def v = volume;
input aggregationPeriod = AggregationPeriod.DAY;
def displace = -1;
def timeopen = SecondsFromTime(locate_bubbles_at_time) == 0;
def isExpansion = locate_bubbles_at == locate_bubbles_at.Expansion and IsNaN(close);
def firstExpansionBar = if !IsNaN(close[-1]) and isExpansion then 1 else if isExpansion then firstExpansionBar[1] + 1 else 0;
def BubbleLocation = if locate_bubbles_at == locate_bubbles_at.Time then timeopen else isExpansion and firstExpansionBar == BarsFromExpansion;

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


plot PD_High;
plot PD_Low;
plot PD_Close;
plot PD_Open;
plot PW_High;
plot PW_Low;

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];
def PD_O = if extend_plots and IsNaN(close)
           then PD_O[1]
           else open(period = aggregationPeriod)[-displace];

def PW_H = if extend_plots and IsNaN(close)
           then PW_H[1]
           else High(period = aggregationPeriod.WEEK)[-displace];
         

def PW_L = if extend_plots and IsNaN(close)
           then PW_L[1]
           else Low(period = aggregationPeriod.WEEK)[-displace];



if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PD_High = na;
    PD_Low = na;
    PD_Close = na;
    PD_Open = na;
    PW_High = na;
    PW_Low = na;
} else {
    PD_High  = PD_H;
    PD_Low   = PD_L;
    PD_Close = PD_C;
    PD_Open = PD_O;
    PW_High = PW_H;
    PW_Low = PW_L;
}
;

PD_High.SetDefaultColor(Color.DARK_ORANGE);
PD_High.SetStyle(Curve.LONG_DASH);
PD_High.SetLineWeight(2);
PD_High.HideTitle();

PD_Low.SetDefaultColor(Color.DARK_ORANGE);
PD_Low.SetStyle(Curve.LONG_DASH);
PD_Low.SetLineWeight(2);
PD_Low.HideTitle();

PD_Close.SetDefaultColor(Color.WHITE);
PD_Close.SetStyle(Curve.LONG_DASH);
PD_Close.SetLineWeight(2);
PD_Close.HideTitle();

PD_Open.SetDefaultColor(Color.GRAY);
PD_Open.SetStyle(Curve.LONG_DASH);
PD_Open.SetLineWeight(2);
PD_Open.HideTitle();


PW_High.SetDefaultColor(Color.PLUM);
PW_High.SetStyle(Curve.LONG_DASH);
PW_High.SetLineWeight(2);
PW_High.HideTitle();


PW_Low.SetDefaultColor(Color.pink);
PW_Low.SetStyle(Curve.LONG_DASH);
PW_Low.SetLineWeight(2);
PW_Low.HideTitle();





DefineGlobalColor("PD_High", CreateColor(255, 204, 153));
DefineGlobalColor("PD_Low", CreateColor(255, 204, 153));
DefineGlobalColor("PD_Close", Color.WHITE);
DefineGlobalColor("PD_Open", Color.GRAY);
DefineGlobalColor("PW_High", Color.PLUM);
DefineGlobalColor("PW_Low", Color.PINK);

AddChartBubble(ShowBubbles and BubbleLocation , PD_High, "PrevDayH: " + (if ShowPricesInBubbles then AsText(PD_High) else ""), GlobalColor("PD_High"));
AddChartBubble(ShowBubbles and BubbleLocation, PD_Low, "PrevDayL: " + (if ShowPricesInBubbles then AsText(PD_Low) else ""), GlobalColor("PD_Low"), no);
AddChartBubble(ShowBubbles and BubbleLocation , PD_Close, "PrevDayC: " + (if ShowPricesInBubbles then AsText(PD_Close) else ""), GlobalColor("PD_Close"));
AddChartBubble(ShowBubbles and BubbleLocation , PD_Open, "PrevDayO: " + (if ShowPricesInBubbles then AsText(PD_Open) else ""), GlobalColor("PD_Open"));

AddChartBubble(ShowBubbles and BubbleLocation, PW_High, "PrevWeekH: " + (if ShowPricesInBubbles then AsText(PW_High) else ""), GlobalColor("PW_High"));
AddChartBubble(ShowBubbles and BubbleLocation, PW_LOW, "PrevWeekL: " + (if ShowPricesInBubbles then AsText(PW_LOW) else ""), GlobalColor("PW_LOW"), no);


PD_High.HideBubble();
PD_Low.HideBubble();
PD_Close.HideBubble();
PD_Open.HideBubble();


#----- Premarket High/Low -----# Thanks to Mobius

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;
if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    PM_High = na;
    PM_Low = na;
} else {
    PM_High = if OverNightHigh > 0 then OverNightHigh else na;
    PM_Low = if OverNightLow > 0 then OverNightLow else na;
}

#PM_High.SetHiding(!PlotOverNightExtremes);
PM_High.SetLineWeight(2);
PM_High.SetDefaultColor(Color.CYAN);
PM_High.SetStyle(Curve.LONG_DASH);
PM_High.HideBubble();
PM_High.HideTitle();

#PM_Low.SetHiding(!PlotOverNightExtremes);
PM_Low.SetStyle(Curve.LONG_DASH);
PM_Low.SetDefaultColor(Color.CYAN);
PM_Low.HideBubble();
PM_Low.HideTitle();

DefineGlobalColor("PM_High", CreateColor(102, 255, 255));
AddChartBubble(ShowBubbles and ShowPremarket and if locate_bubbles_at == locate_bubbles_at.Time then bn == ONhighBar else isExpansion and firstExpansionBar == BarsFromExpansion, PM_High, "PMH: " + (if ShowPricesInBubbles then AsText(PM_High) else ""), GlobalColor("PM_High"));

DefineGlobalColor("PM_Low", CreateColor(102, 255, 255));
AddChartBubble(ShowBubbles  and ShowPremarket and if locate_bubbles_at == locate_bubbles_at.Time then bn == ONlowBar else isExpansion and firstExpansionBar == BarsFromExpansion, PM_Low, "PML: " + (if ShowPricesInBubbles then AsText(PM_Low) else ""), GlobalColor("PM_Low"), no);


#----- Today Open/High/Low -----#


plot HODPlot;
plot LODPlot;
plot DayOpenPlot;

def High_of_Day = if extend_plots and IsNaN(close)
           then High_of_Day[1]
           else Highest(high(period = aggregationPeriod), length);
def Low_of_Day = if extend_plots and IsNaN(close)
           then Low_of_Day[1]
           else Lowest(low(period = aggregationPeriod), length);
def DayOpen = if extend_plots and IsNaN(close)
           then DayOpen[1]
           else open(period = aggregationPeriod);
         
    if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    HODPlot = na;
    LODPlot = na;
    DayOpenPlot = na;
   
} else {
    HODPlot = High_of_Day;
    LODPlot = Low_of_Day;
    DayOpenPlot = DayOpen;

 
}

DayOpenPlot.SetDefaultColor (Color.GRAY);
DayOpenPlot.SetPaintingStrategy(PaintingStrategy.DASHES);
DayOpenPlot.SetLineWeight(2);
DayOpenPlot.HideTitle();

HODPlot.SetDefaultColor(CreateColor(0, 128, 255));
HODPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
HODPlot.SetLineWeight(2);
HODPlot.HideTitle();

LODPlot.SetDefaultColor(CreateColor(0, 128, 255));
LODPlot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
LODPlot.SetLineWeight(2);
LODPlot.HideTitle();








DefineGlobalColor("Open", Color.LIGHT_GRAY);
DefineGlobalColor("High_of_Day", CreateColor(102, 178, 255));
DefineGlobalColor("Low_of_Day", CreateColor(102, 178, 255));
AddChartBubble(ShowBubbles and BubbleLocation, DayOpen, "Open: " + (if ShowPricesInBubbles then AsText(DayOpen) else ""), GlobalColor("Open"));
AddChartBubble(ShowBubbles and BubbleLocation, High_of_Day, "HOD: " + (if ShowPricesInBubbles then AsText(High_of_Day) else ""), GlobalColor("High_of_Day"));
AddChartBubble(ShowBubbles and BubbleLocation, Low_of_Day, "LOD: " + (if ShowPricesInBubbles then AsText(Low_of_Day) else ""), GlobalColor("Low_of_Day"), no);

#Point Of Control

def highestHigh = if PD_High >= High_of_Day then PD_High else High_of_Day;
def LowestLow = if PD_Low <= Low_of_Day then PD_Low else Low_of_Day;



#----- ATR Lines -----# Thaanks to TraderKevin
input showAtrLines = Yes;
input atrLinesFrom = {default pdc, dayOpen};
input ATRlength = 14;
input averageType = AverageType.WILDERS;
input ATRPct = 80;

def ATR = (MovingAverage(averageType, TrueRange(high(period = ”DAY”)[1], close(period = ”DAY”)[1], low(period = ”DAY”)[1]), ATRlength)) *(ATRPct/100);


def hatr = if extend_plots and IsNaN(close)then hatr[1] else if atrLinesFrom  == atrLinesFrom .dayOpen then DayOpen + ATR else PD_Close + ATR;
def latr = if extend_plots and IsNaN(close)then latr[1] else if atrLinesFrom  == atrLinesFrom .dayOpen then DayOpen - ATR else PD_Close - ATR;

plot hatrplot= hatr;
plot latrplot= latr;
hatrplot.SetLineWeight(5);
hatrplot.SetDefaultColor(Color.RED);
hatrplot.SetStyle(Curve.LONG_DASH);
hatrplot.HideBubble();
hatrplot.HideTitle();
hatrplot.SetHiding(showAtrLines == no);

latrplot.SetLineWeight(5);
latrplot.SetStyle(Curve.LONG_DASH);
latrplot.SetDefaultColor(Color.GREEN);
latrplot.HideBubble();
latrplot.HideTitle();
latrplot.SetHiding(showAtrLines == no);

def POC = if extend_plots and isnan(close) then poc[1] else(highestHigh + LowestLow) / 2;
plot POCPlot = POC;

DefineGlobalColor("POCPlot", CreateColor(121, 121, 121));
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, POCPlot, "POC: " + (if ShowPricesInBubbles then AsText(POCPlot) else ""), GlobalColor("POCPlot"), no);

DefineGlobalColor("hatrplot", Color.light_red);
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, hatrplot, "ATRH: " + (if ShowPricesInBubbles then AsText(hatrplot) else ""), GlobalColor("hatrplot"));

DefineGlobalColor("latrplot", Color.LIGHT_GREEN);
AddChartBubble(ShowBubbles and showAtrLines and BubbleLocation, latrplot, "ATRL: " + (if ShowPricesInBubbles then AsText(latrplot) else ""), GlobalColor("latrplot"), no);
Really good. Thank you.
 

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