Previous Day High and Low Breakout Indicator for ThinkorSwim

Hey SleepyZ!

Quick question on this, do you mind confirming the fact this won't update if a new low/high is hit in current day correct? For example, lets say 10L is 4000 and in the current day session we hit 3998. Until tomorrow, the marked price level from the indicator will remain at 4000 and not update intraday correct?

The above code as written includes the current day's high/low. Make sure you have the highest days_ago displayed on your chart.

The code below should exclude the current day as you want. The bubbles have been edited to display the days_ago input.

Screenshot-2023-02-27-091757.png
Code:
#HH_LL_From_XYZ_days_ago_Excluding_Today_INCLUDING_EXT_HOURS

script hl {
    input Days_Ago = 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) ;
    def H = if thisDay == Days_Ago then high else if thisDay <= Days_Ago and thisDay > 1 then Max(high, H[1]) else Double.NaN;
    plot HH = if thisDay > Days_Ago then Double.NaN else HighestAll(H);
    def L = if thisDay == Days_Ago then low else if thisDay <= Days_Ago and thisDay > 1 then Min(low, L[1]) else Double.NaN;
    plot LL = if thisDay > Days_Ago then Double.NaN else LowestAll(L);
}

input days_ago_1 = 10;
input days_ago_2 = 20;
input days_ago_3 = 30;

input show_on_lastday_only = no;
def lastday = GetDay() == GetLastDay();
plot H1 = if show_on_lastday_only and lastday then hl(days_ago_1) else if !show_on_lastday_only then hl(days_ago_1) else Double.NaN;
plot H2 = if show_on_lastday_only and lastday then hl(days_ago_2) else if !show_on_lastday_only then hl(days_ago_2) else Double.NaN;
plot H3 = if show_on_lastday_only and lastday then hl(days_ago_3) else if !show_on_lastday_only then hl(days_ago_3) else Double.NaN;

plot L1 = if show_on_lastday_only and lastday then hl(days_ago_1).LL else if !show_on_lastday_only then hl(days_ago_1).LL else Double.NaN;
plot L2 = if show_on_lastday_only and lastday then hl(days_ago_2).LL else if !show_on_lastday_only then hl(days_ago_2).LL else Double.NaN;
plot L3 = if show_on_lastday_only and lastday then hl(days_ago_3).LL else if !show_on_lastday_only then hl(days_ago_3).LL else Double.NaN;

DefineGlobalColor("HL10", Color.CYAN);
DefineGlobalColor("HL20", Color.WHITE);
DefineGlobalColor("HL30", Color.YELLOW);

H1.SetDefaultColor(GlobalColor("HL10"));
L1.SetDefaultColor(GlobalColor("HL10"));
H2.SetDefaultColor(GlobalColor("HL20"));
L2.SetDefaultColor(GlobalColor("HL20"));
H3.SetDefaultColor(GlobalColor("HL30"));
L3.SetDefaultColor(GlobalColor("HL30"));

input show_bubbles = yes;
input bubblemover  = 2;
def b = bubblemover;
def b1 = b + 1;
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), H1[b], days_ago_1 + "H", H1.TakeValueColor());
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), H2[b], days_ago_2 + "H", H2.TakeValueColor());
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), H3[b], days_ago_3 + "H", H3.TakeValueColor());

AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), L1[b], days_ago_1 + "L", L1.TakeValueColor());
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), L2[b], days_ago_2 + "L", L2.TakeValueColor());
AddChartBubble(IsNaN(close[b]) and !IsNaN(close[b1]), L3[b], days_ago_3 + "L", L3.TakeValueColor());
 

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

The above code as written includes the current day's high/low. Make sure you have the highest days_ago displayed on your chart.

The code below should exclude the current day as you want. The bubbles have been edited to display the days_ago input.
Perfect, thank you for the update!

One thing I noticed today was the on one of the studies in which I updated the code to look at 3 day High/Low, if you see the below screenshot, the 3L looks to be off. Is there any reason that trying to look back only 3days based on how the code was written would cause this issue?

I am using this on ES 4H charts with 360 day timeframes and extended hours on, for reference. I would think the 3L would line up with all the others at 3947.5ish?

image.png
 
Perfect, thank you for the update!

One thing I noticed today was the on one of the studies in which I updated the code to look at 3 day High/Low, if you see the below screenshot, the 3L looks to be off. Is there any reason that trying to look back only 3days based on how the code was written would cause this issue?

I am using this on ES 4H charts with 360 day timeframes and extended hours on, for reference. I would think the 3L would line up with all the others at 3947.5ish?

image.png

It appears that symbols trading on Sun/Monday do not work well in defining day periods when used on 2hr/4hr timeframes with the available built-in 'day' functions, getday() or getyyyymmdd(). It appears to work on other intraday timeframes. I do not have a workaround for this at this time. So I recommend you just add a day to days_ago input for each of the Sun/Monday's involved.
 
It appears that symbols trading on Sun/Monday do not work well in defining day periods when used on 2hr/4hr timeframes with the available built-in 'day' functions, getday() or getyyyymmdd(). It appears to work on other intraday timeframes. I do not have a workaround for this at this time. So I recommend you just add a day to days_ago input for each of the Sun/Monday's involved.
Makes sense! I figured it had something to do with the futures instrument and odd timing it trades compared to the standard market. Much appreciated!
 
I got this indicator from
https://usethinkscript.com/threads/previous-day-high-and-low-breakout-indicator-for-thinkorswim.154/
It's very interesting, I need adding this item to my watchlist, if open bell the stock price crosses above/below yesterday high/low then give me color signal on my watchlist column. Thank you very much for helping this matter.

# 5 min opening range
# Robert Payne
#Plot opening range high / low
input OpenRangeMinutes = 5;
input MarketOpenTime = 0930;
input ShowTodayOnly = yes;

def Today = if GetDay() == GetLastDay() then 1 else 0;
def FirstMinute = if SecondsFromTime(MarketOpenTime) < 60 then 1 else 0;
def OpenRangeTime = if SecondsFromTime(MarketOpenTime) < 60 * OpenRangeMinutes then 1 else 0;

#Plot yesterday's high / low
plot Yhigh = if ShowTodayOnly and !Today then Double.NaN else high(period = "day" )[1];
plot Ylow = if ShowTodayOnly and !Today then Double.NaN else low(period = "day" )[1];

Yhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Yhigh.SetDefaultColor(Color.UPTICK);
Yhigh.SetLineWeight(2);
Ylow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Ylow.SetDefaultColor(Color.DOWNTICK);
Ylow.SetLineWeight(2);

#Plot pivot
plot Pivot = if ShowTodayOnly and !Today then Double.NaN else (high(period = "day" )[1] + low(period = "day" )[1] + close(period = "day" )[1]) / 3;
Pivot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Pivot.SetDefaultColor(Color.YELLOW);
Pivot.SetLineWeight(2);

#Plot 10 day high / low
plot TenHigh = if ShowTodayOnly and !Today then Double.NaN else Highest(high(period = "day" )[1], 10);
plot TenLow = if ShowTodayOnly and !Today then Double.NaN else Lowest(low(period = "day" )[1], 10);

TenHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TenHigh.SetDefaultColor(Color.UPTICK);
TenHigh.SetLineWeight(3);
TenLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TenLow.SetDefaultColor(Color.DOWNTICK);
TenLow.SetLineWeight(3);

AssignPriceColor(if close >= Yhigh then Color.GREEN
else if close <= Ylow then Color.RED
else Color.CURRENT);

AssignPriceColor(if close > Yhigh and open < close then Color.White
else if close > YHIGH and open > close then Color.Gray
else if close < Ylow and open < close then Color.White
else if close < Ylow and open > close then Color.Gray
else Color.CURRENT);
 
Last edited by a moderator:
This indicators can be plotted on any chart depending on your trading strategy. It is based on break of ranges or start of trends. ( higher high lower highs and visa versa) it will alert the text "Breakout" in red or green depending on the direction (bullish or bearish).

# chatGPT Breakout and Trends Indicator for Thinkorswim

# Inputs
input numHigherLows = 3; # Number of consecutive higher lows to consider for the pattern

# Calculate breakout and higher lows
def previousHigh = high(period = AggregationPeriod.DAY)[1];
def previousLow = low(period = AggregationPeriod.DAY)[1];
def breakout = high > previousHigh;
def higherLows = low > Highest(low, numHigherLows);

# Plot signals
plot BreakoutSignal = breakout;
BreakoutSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
BreakoutSignal.SetDefaultColor(Color.GREEN);
BreakoutSignal.SetLineWeight(2);

plot HigherLowsSignal = higherLows;
HigherLowsSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
HigherLowsSignal.SetDefaultColor(Color.BLUE);
HigherLowsSignal.SetLineWeight(2);

# Calculate breakdown and lower lows
def breakdown = low < previousLow;
def lowerLows = low < Lowest(low, numHigherLows);

# Plot signals
plot BreakdownSignal = breakdown;
BreakdownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
BreakdownSignal.SetDefaultColor(Color.RED);
BreakdownSignal.SetLineWeight(2);

plot LowerLowsSignal = lowerLows;
LowerLowsSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
LowerLowsSignal.SetDefaultColor(Color.MAGENTA);
LowerLowsSignal.SetLineWeight(2);

# Display text
AddLabel(breakout, "Breakout", Color.GREEN);
AddLabel(higherLows, "Higher Lows", Color.BLUE);
AddLabel(breakdown, "Breakdown", Color.RED);
AddLabel(lowerLows, "Lower Lows", Color.MAGENTA);
 
Last edited by a moderator:
Is there a way to add chart bubbles to these levels?





Rich (BB code):
# 5 min opening range
# Robert Payne
#Plot opening range high / low
input OpenRangeMinutes = 5;
input MarketOpenTime = 0930;
input ShowTodayOnly = yes;

def Today = if GetDay() == GetLastDay() then 1 else 0;
def FirstMinute = if SecondsFromTime(MarketOpenTime) < 60 then 1 else 0;
def OpenRangeTime = if SecondsFromTime(MarketOpenTime) < 60 * OpenRangeMinutes then 1 else 0;

#Plot yesterday's high / low
plot Yhigh = if ShowTodayOnly and !Today then Double.NaN else high(period = "day" )[1];
plot Ylow = if ShowTodayOnly and !Today then Double.NaN else low(period = "day" )[1];

Yhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Yhigh.SetDefaultColor(Color.UPTICK);
Yhigh.SetLineWeight(2);
Ylow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Ylow.SetDefaultColor(Color.DOWNTICK);
Ylow.SetLineWeight(2);

#Plot pivot
plot Pivot = if ShowTodayOnly and !Today then Double.NaN else (high(period = "day" )[1] + low(period = "day" )[1] + close(period = "day" )[1]) / 3;
Pivot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Pivot.SetDefaultColor(Color.YELLOW);
Pivot.SetLineWeight(2);

#Plot 10 day high / low
plot TenHigh = if ShowTodayOnly and !Today then Double.NaN else Highest(high(period = "day" )[1], 10);
plot TenLow = if ShowTodayOnly and !Today then Double.NaN else Lowest(low(period = "day" )[1], 10);

TenHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TenHigh.SetDefaultColor(Color.UPTICK);
TenHigh.SetLineWeight(3);
TenLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TenLow.SetDefaultColor(Color.DOWNTICK);
TenLow.SetLineWeight(3);

AssignPriceColor(if close >= Yhigh then Color.GREEN
else if close <= Ylow then Color.RED
else Color.CURRENT);

AssignPriceColor(if close > Yhigh and open < close then Color.White
else if close > YHIGH and open > close then Color.Gray
else if close < Ylow and open < close then Color.White
else if close < Ylow and open > close then Color.Gray
else Color.CURRENT);

Is there a way to make #Plot pivot be 50% of the high and low of day. Thanks in advance.
 
Is there a way to make #Plot pivot be 50% of the high and low of day. Thanks in advance.

Yes this will plot the 50% of yesterday's high/low, denoted by the [1]. Today's High/Low would be [0] or omit the brackets.

Code:
plot Pivot = if ShowTodayOnly and !Today then Double.NaN else (high(period = "day" )[1] + low(period = "day" )[1]) / 2;
 
Is there a way to add code for Back testing that show only trades between 9:30 est to 16:00 est exclusively no trades in strategy reports outside of this timeframe
 
I took this indicator from Robert and tweaked it a little to my need. Originally it plots the 5 min opening range breakout. However, I took that out and only keep the previous day high and low. Then I add some customizations to it to make the breakout or breakdown more noticeable.

Here is what it looks like:

View attachment 4489

When the price is within the previous day high and low range then you would see the color of the regular candle. When it's outside of the range then red candles become gray and green candles become white.

thinkScript Code

Rich (BB code):
# 5 min opening range
# Robert Payne
#Plot opening range high / low
input OpenRangeMinutes = 5;
input MarketOpenTime = 0930;
input ShowTodayOnly = yes;

def Today = if GetDay() == GetLastDay() then 1 else 0;
def FirstMinute = if SecondsFromTime(MarketOpenTime) < 60 then 1 else 0;
def OpenRangeTime = if SecondsFromTime(MarketOpenTime) < 60 * OpenRangeMinutes then 1 else 0;

#Plot yesterday's high / low
plot Yhigh = if ShowTodayOnly and !Today then Double.NaN else high(period = "day" )[1];
plot Ylow = if ShowTodayOnly and !Today then Double.NaN else low(period = "day" )[1];

Yhigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Yhigh.SetDefaultColor(Color.UPTICK);
Yhigh.SetLineWeight(2);
Ylow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Ylow.SetDefaultColor(Color.DOWNTICK);
Ylow.SetLineWeight(2);

#Plot pivot
plot Pivot = if ShowTodayOnly and !Today then Double.NaN else (high(period = "day" )[1] + low(period = "day" )[1] + close(period = "day" )[1]) / 3;
Pivot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Pivot.SetDefaultColor(Color.YELLOW);
Pivot.SetLineWeight(2);

#Plot 10 day high / low
plot TenHigh = if ShowTodayOnly and !Today then Double.NaN else Highest(high(period = "day" )[1], 10);
plot TenLow = if ShowTodayOnly and !Today then Double.NaN else Lowest(low(period = "day" )[1], 10);

TenHigh.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TenHigh.SetDefaultColor(Color.UPTICK);
TenHigh.SetLineWeight(3);
TenLow.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TenLow.SetDefaultColor(Color.DOWNTICK);
TenLow.SetLineWeight(3);

AssignPriceColor(if close >= Yhigh then Color.GREEN
  else if close <= Ylow then Color.RED
  else Color.CURRENT);

AssignPriceColor(if close > Yhigh and open < close then Color.White
  else if close > YHIGH and open > close then Color.Gray
  else if close < Ylow and open < close then Color.White
  else if close < Ylow and open > close then Color.Gray
  else Color.CURRENT);

Shareable Link

https://tos.mx/tbjMgb

Previous Intradays High, Low, Mean

Code:
# Previous Intradays High, Low, Mean
# Mobius
# V01.12.2017 Desktop and Mobile
# Revised original code that uses SecondsFromTime() and SecondsTillTime(). Code now uses RegularTradingStart() and RegularTradingEnd() to bracket RTH. Works in Mobile Apps

def bar = barNumber();
def h = high;
def l = low;
def c = close;
def firstBar = if getTime() crosses above RegularTradingStart(GetYYYYMMDD()) and
                  !isNaN(close)
               then bar
               else double.nan;
def lastBar = if getTime() crosses above RegularTradingEnd(GetYYYYMMDD()) and
                 !isNaN(close)
              then bar
              else double.nan;
addVerticalLine(bar == HighestAll(firstBar), "first bar", color.cyan, curve.short_dash);
addVerticalLine(bar == HighestAll(lastBar), "last bar", color.cyan, curve.short_dash);
def bar_t1 = if !isNaN(firstBar)
            then bar
            else bar_t1[1];
def bar_t2 = if !isNaN(lastBar)
             then bar
             else bar_t2[1];
def prevFirstBar = if bar_t1 != bar_t1[1]
              then bar_t1[1]
              else prevFirstBar[1];
def prevLastBar = if bar_t2 != bar_t2[1]
                  then bar_t2[1]
                  else prevLastBar[1];
addVerticalLine(bar == HighestAll(prevFirstBar), "prev first bar", color.red, curve.short_dash);
addVerticalLine(bar == HighestAll(prevLastBar), "prev Last bar", color.red, curve.short_dash);
def hh = if bar == HighestAll(prevFirstBar)
         then h
         else if between(bar, highestAll(prevFirstBar), highestAll(LastBar)) and
                 h > hh[1]
              then h
              else hh[1];
def hhBar = if h == hh and between(bar, highestAll(prevFirstBar), highestAll(LastBar))
            then barNumber()
            else double.nan;
def ll = if bar == HighestAll(prevFirstBar)
         then l
         else if between(bar, highestAll(prevFirstBar), highestAll(LastBar)) and
                 l < ll[1]
              then l
              else ll[1];
def llBar = if l == ll and between(bar, highestAll(prevFirstBar), highestAll(LastBar))
            then bar
            else double.nan;
plot PrevDayHigh = if bar >= highestAll(hhBar)
                   then highestAll(if isNaN(c[-1])
                                   then hh
                                   else double.nan)
                   else double.nan;
     PrevDayHigh.SetStyle(Curve.Long_Dash);
     PrevDayHigh.SetLineWeight(3);
     PrevDayHigh.SetDefaultColor(Color.Green);
     PrevDayHigh.HideTitle();
plot PrevDayLow = if bar >= HighestAll(llBar)
                  then highestAll(if isNaN(c[-1])
                                  then ll
                                  else double.nan)
                  else double.nan;
     PrevDayLow.SetStyle(Curve.Long_Dash);
     PrevDayLow.SetLineWeight(3);
     PrevDayLow.SetDefaultColor(Color.Red);
     PrevDayLow.HideTitle();
def hl2bar = Floor((highestAll(hhbar) + highestAll(llBar)) / 2);
def hl2price = Round(((PrevDayHigh + PrevDayLow) / 2) / TickSize(), 0) * TickSize();
plot PrevDayHL2 = if bar >= highestAll(HL2bar)
               then highestAll(if !isNaN(c[-1])
                               then HL2price
                               else double.nan)
               else double.nan;
     PrevDayHL2.SetStyle(Curve.Long_Dash);
     PrevDayHL2.SetLineWeight(3);
     PrevDayHL2.SetDefaultColor(Color.Yellow);
     PrevDayHL2.HideTitle();
# End Code Previous Days High, Low, Mean
Hello @BenTen

I have no clue how to code lol but I think these visuals are amazing! I am missing an indicator that shows me these greyed out bars like you have here but I am looking for it on "Todays 5 min time frame" do you know if something like that exists?
 
Last edited by a moderator:

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
549 Online
Create Post

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