Previous Day High and Low Breakout Indicator for ThinkorSwim

Hello, I need some help to improvise the Daily high low indicator. I mainly used for scalping in 1min time-frame chart without pre-market session. I converted into TOS strategy for testing purposes but there is a problem with it. It creates the first order right away after opening candle. For example, If I set "marketOpen" time as "931" or 9:31am, It creates order at next candle or 9:32 according to the 9:31 candle. If 9:31 candle is red than it creates a short position if it's green than it creates long position. I tried to fix it by changing "marketOpen" time, let's say "940", same issue, order at 9:32 candle and 9:41 candle.

So my goal is to disable creating order at the second candle depending on "marketOpen" time OR in another word, ignore first candle and previous candle before "marketOpen" time. for instance, if my "marketOpen" time is 1000 or 10 am then ignore all candles from 9:30 am till 10 am.

mI6PJF1.png

gslL5sL.png

XtlpAdi.png


Link for strategy
https://tos.mx/ltZhUKr
Link for Workspace w/ stop loss added
https://tos.mx/qbPjsdD

Code:
input marketOpen = 931;
input marketClose = 1600;
input intraDaySpan = {Default "SameDay" , "OverNight"};
input numberOfDays = 1;
input numberOfYears = 0;

def okToPlot = GetLastDay() - numberOfDays <= GetDay() and GetLastYear() - numberOfYears <= GetYear() ;

def OpenCounter =  SecondsFromTime(marketOpen);
def CloseCounter = SecondsTillTime(marketClose);

def MarketHours = if OpenCounter >= 0 and CloseCounter >= 0 then 1 else 0;

def beforeMidnight = OpenCounter >= 0 and CloseCounter <= 0;
def afterMidnight = OpenCounter <= 0 and CloseCounter >= 0 ;

def Today ;
def hideChartBubbles ;
rec DailyHigh ;
rec DailyLow ;

switch (intraDaySpan) {
    case "SameDay":
    Today = if GetDay() != GetDay()[1] then 1 else 0;
    DailyHigh = if MarketHours then if high > DailyHigh[1] then high else DailyHigh[1] else high;
    DailyLow = if Today then low else if MarketHours then if low < DailyLow[1] then low else DailyLow[1] else low;
    hideChartBubbles = MarketHours;
    case "OverNight":
    Today = 0;
    DailyHigh = if beforeMidnight or afterMidnight then if high > DailyHigh[1] then high else DailyHigh[1] else  high;
    DailyLow = if beforeMidnight or afterMidnight then if low < DailyLow[1] then low else DailyLow[1] else low;
    hideChartBubbles = beforeMidnight or afterMidnight;
};

plot TodaysHigh = if okToPlot and hideChartBubbles  then DailyHigh else Double.NaN;
plot TodaysLow = if okToPlot and hideChartBubbles then DailyLow else Double.NaN;
TodaysHigh.SetDefaultColor(Color.GREEN);
TodaysLow.SetDefaultColor(Color.RED);


Def high= DailyHigh > DailyHigh[1] ;
Alert(high,"New High", Alert.BAR, Sound.Bell);
AddOrder(OrderType.BUY_AUTO, high , tickColor = GetColor(1), arrowColor = GetColor(1), name = ".");



Def low= DailyLow < DailyLow[1] ;
Alert(low, "New Low", Alert.BAR, Sound.Bell);
AddOrder(OrderType.Sell_AUTO, low , tickColor = GetColor(1), arrowColor = GetColor(1), name = ".");
your workspace dos not work
 

hydroflask

New member

Robby Luca

New member
I'm trying to build a scanner that will scan stock that is above Yesterday's High.

Aggregation: 1m [Including Extended-Hours trading session]
Code:
def Price = close;

def day = AggregationPeriod.DAY;
def YestHigh = high(period = day)[1];

plot scan = Price > YestHigh;
Secondary period not allowed: Day.

What would be the work around for this? I will be using this in the premarket.
 

XeoNoX

Well-known member
VIP
@Robby Luca

PREVIOUS ALL DAY HIGH INCLUDING EXTENDED and PREMARKET HOURS
recommended aggregation should be 1 minute
MANUALLY CHANGE THE DATE TO THE PREVIOUS DAYS DATE
Code:
#PREV ALL DAY HIGH INCLUDING EXT HOURS
#BY XeoNoX via usethikscript.com
# recommended aggregation should be 1 minute
#MANUALLY CHANGE THE DATE TO THE PREVIOUS DAYS DATE
#format is YEAR MONTH DAY YYYYMMDD
input date = 20210301;
def PREV_ALL_DAY_HIGH = HighestAll(if  GetYYYYMMDD()== 20210301 then high else 0);
AddLabel (yes, "Prev All Day High " +  (PREV_ALL_DAY_HIGH)  );

for the scan just change
Code:
AddLabel (yes, "Prev All Day High " +  (PREV_ALL_DAY_HIGH)  );
to
Code:
plot scan = PREV_ALL_DAY_HIGH < close;

if you found this post useful remember to leave a thumbs up!
 

Mrfibonacci

New member
@BenTen or anyone else who may know,

I am looking for a custom ThinkScript study similar to the above, but I need help with a script which plots the previous days High, Low, and Close as horizontal lines on any intraday chart on the ToS mobile app (I use the 3 min chart on my phone). Has anyone done this before for ToS mobile? I already got this set up for the Desktop ToS platform but unable to get a script working for mobile. Thank you so much if you can help me make this possible!
 

Robby Luca

New member
@BenTen or anyone else who may know,

I am looking for a custom ThinkScript study similar to the above, but I need help with a script which plots the previous days High, Low, and Close as horizontal lines on any intraday chart on the ToS mobile app (I use the 3 min chart on my phone). Has anyone done this before for ToS mobile? I already got this set up for the Desktop ToS platform but unable to get a script working for mobile. Thank you so much if you can help me make this possible!
There's an indicator called DailyHighLow in TOS already. Can plot highs and lows.
And this is the code to plot prev day close line. Not sure how it works on mobile to be honest.
Code:
input aggregationPeriod = AggregationPeriod.DAY;
input length = 1;
input displace = -1;
input showOnlyLastPeriod = yes;

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

XeoNoX

Well-known member
VIP
@BenTen

It looks like it doesn't work, are there any other column studies you have handy that do?

- AAPL and MARA gapping above previous HOD, column didn't know:

eI965kd.png

MhvEBvN.png


- PDD gapping above previous HOD, column says gapping down:

O7MWOyk.png
it APPEARS there are no gaps from previous day high to the current day on the charts your posted. however since you failed to screenshot the entire chart i may be wrong. If you are asking a question relating to price and time, for future reference the price and time would be helpful in the screenshot. One of the most strangest things people do is reference price and time on a chart and show just one of the two making the chart essentially just random bars and lines with nothing to reference to.
 

hydroflask

New member
it APPEARS there are no gaps from previous day high to the current day on the charts your posted. however since you failed to screenshot the entire chart i may be wrong. If you are asking a question relating to price and time, for future reference the price and time would be helpful in the screenshot. One of the most strangest things people do is reference price and time on a chart and show just one of the two making the chart essentially just random bars and lines with nothing to reference to.

Thanks for the feedback, you have a great point and I will do that in the future. However, in the screenshot, you can still see in PDD for example that the pre-market this morning is far above the previous day's high which is marked by a yellow line. Therefore, the study isn't working correctly, because it should show that PDD is gapping up this morning. Can you help?
 

XeoNoX

Well-known member
VIP
So this is a perfect setup for what I am trying to catch, but I'm not able to catch it... 1min candle Breakout over previous DAY high, over average volume, and my HiLowBreakout study alerted. But it doesn't. Here is the code for the HiLow study:

def CurrentDay = GetDay() == GetLastDay();
def previousHigh = if CurrentDay then high(period = AggregationPeriod.Day)[1] else Double.NaN;
def previousLow = if CurrentDay then low(period = AggregationPeriod.Day)[1] else Double.NaN;
plot scan = high crosses above previousHigh;

I need to have that study populate a watchlist. I can see that it happened, but I'm limited by the conflicting DAY/1min difference. It seems if the study triggers and plots a value of (1) versus (0), there should be a way to populate based on that...?
IhKPqmn.png
Realtime watchlist populates like below. So the high of the DAY crossed, technically. I need the high of the 1min to populate.

XprCorN.png
HELP, please!!


it plots 1 or 0 because it means the condition you set is true or fales. 1 is true and 0 is false.

you stated:
"1min candle Breakout over previous DAY high, over average volume, and my HiLowBreakout study alerted. But it doesn't. Here is the code for the HiLow study:"

there is already a study with a post somewhere on here for price goes above previous day high. You high low break out when the condition is met you want for it you have to make sure it equals true

so then you are going to want both to be true. however since you fail to find the and post the 2 codes you need/are using then its impossible to help you all the way. but here is the theory of what you will have to do.

plot both conditions are true ( equal to 1, im my example below greater than 0 which 1 is greater than 0)
plot scan = highlow>0 and breakprevday > 0;
 

XeoNoX

Well-known member
VIP
High From XYZ days ago INCLUDING EXT HOURS (This will work for previous day high as well)
BY XeoNoX via usethikscript.com
Recommended aggregation should be 1 minute
Days and time are according to USA Eastern (NewYork) time
Change input Days_Ago to your desired days ago

Code:
#High From XYZ days ago INCLUDING EXT HOURS
#BY XeoNoX via usethikscript.com
# recommended aggregation should be 1 minute
#Days and time are according to USA Eastern (NewYork) time
#Change input Days_Ago to your desired days ago
Input Days_Ago = 2;
def lastDate = HighestAll( if GetDay() == GetLastDay() and GetYear() == GetLastYear() then GetYYYYMMDD() else Double.NaN );
def today = if GetYYYYMMDD() < lastDate then GetYYYYMMDD() else lastDate;
def priorday = if CountTradingDays(today, LastDate ) == Days_Ago then yes else no;
def PREV_ALL_DAY_HIGH = HighestAll(if  priorday then high else 0);
AddLabel (yes, "High From " + Days_ago + " Days ago: " +  (PREV_ALL_DAY_HIGH)  );


I9r436P.jpg
 

rjacobse7

New member
Hi,

I have the following code, which will give me the low value and the high value of each candle between 4-5 pm, and then grabs the highest of the highs and the lowest of the lows depending on the day aggregation. However, I only want it to consider the most recent day between 4-5pm when calculating this high and low. How can i fix this code such that the behavior of the ahHigh and ahLow is as it would be on a 1D aggregation, IE only show the previous days high and low between 4-5pm

Code:
#====================================
# Get final hour of trading data
#====================================
input CloseTime = 1700;
input DurationHours = 1;

def durationSec = DurationHours * 60 * 60;
def secondsRemained = SecondsTillTime(closeTime);

# Get high for after hours, four to five
plot fourToFiveHigh = if secondsRemained >= 0 and secondsRemained <= durationSec then high else double.NaN;

# Get low for after hours, four to five
plot fourToFiveLow = if secondsRemained >= 0 and secondsRemained <= durationSec then low else double.NaN;


#===============================
# Get Final Hour High and Low
#===============================

def ahHigh = HighestAll(fourToFiveHigh);
def ahLow = LowestAll(fourToFiveLow);

addLabel(yes, ahHigh);
addLabel(yes, ahLow);
 

Ginu09

Member
---

EDIT: I solved this problem but am trying to compare previous highs against today's open and keep today's "open" static rather than always looking back to previous days' opens. Is there a way to do this?
 
Last edited:

JrE89

New member
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:

3DOqpxq.png


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
how can i get this without the vertical lines and words???
 
Hi everyone,

I hope this finds u well and in good health. I need help with adding a chart label of the opening price (name and price) as well as for high/low price.

here are the code for the opening price scripts im using.
Code:
input openingPMTime  = 0400.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input openingTime  = 0930.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.

def isDaily = If (GetAggregationPeriod() == AggregationPeriod.DAY, yes, no);

def isPreMarket = If (GetDay() == GetLastDay() and SecondsTillTime(openingPMTime) < 0, yes, no);
input LineWidth = 1;
def na = Double.NaN;

def isBelowDaily = If (GetAggregationPeriod() < AggregationPeriod.DAY, yes, no);
def isToday = If (GetDay() == GetLastDay() and SecondsFromTime(openingPMTime) >= 0, yes, no);
def day = GetDay();

def PMopenBar = day != day[1];
def PMOpen = if PMopenBar then open else PMOpen[1];

plot PlotPMOLine = if isToday and isBelowDaily then PMOpen else na;
     PlotPMOLine.SetDefaultColor(CreateColor(77, 166, 255));
     PlotPMOLine.SetLineWeight(LineWidth);
     PlotPMOLine.SetPaintingStrategy(PaintingStrategy.DASHES);
     PlotPMOLine.HideTitle();
     PlotPMOLine.HideBubble();


Here is the code for the yesterday high and low. Im looking to add a label for price and yesterday high / low on the chart

Code:
#Plot opening range high / low
input OpenRangeMinutes = 5;
input MarketOpenTime = 0930;
input ShowTodayOnly = yes;

def Today = if GetDay() == GetLastDay() 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);


Lastly, is it possible to combine these two scripts into 1?

Thank you
 

BenTen

Administrative
Staff member
Staff
VIP
@Tiredoflosing Here you go:

Code:
input openingPMTime  = 0400.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input openingTime  = 0930.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.

def isDaily = If (GetAggregationPeriod() == AggregationPeriod.DAY, yes, no);

def isPreMarket = If (GetDay() == GetLastDay() and SecondsTillTime(openingPMTime) < 0, yes, no);
input LineWidth = 1;
def na = Double.NaN;

def isBelowDaily = If (GetAggregationPeriod() < AggregationPeriod.DAY, yes, no);
def isToday = If (GetDay() == GetLastDay() and SecondsFromTime(openingPMTime) >= 0, yes, no);
def day = GetDay();

def PMopenBar = day != day[1];
def PMOpen = if PMopenBar then open else PMOpen[1];

def PlotPMOLine = if isToday and isBelowDaily then PMOpen else na;

AddLabel(yes, Concat("Opening = ", PlotPMOLine), color.orange);

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

def Today = if GetDay() == GetLastDay() then 1 else 0;

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

AddLabel(yes, Concat("Prev High = ", Yhigh), color.orange);
AddLabel(yes, Concat("Prev Low = ", Ylow), color.orange);
 

datatrends

New member
Here is what it looks like:

3DOqpxq.png

For anyone who wanted the above code, see below.

Code:
#Plot prior day high/low range#
input ShowTodayOnly = yes;
def Today = if GetDay() == GetLastDay() then 1 else 0;
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];


############################################################


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

rjb812

New member
Hi here is what I figured out to scan on PM for yesterday High break Out, same for low, just adjust it. In order to use it put this on the scanner on an intraday aggregation, like 10 min with ext hours.

Code:
def yRTH =if gettime()>regularTradingStart(GetYYYYMMDD()[1]) and gettime()<regularTradingEnd(GetYYYYMMDD()[1]) then 1 else 0;


def yh= if yrth[1]==0 and yrth==1 then high else
       if  yrth and high>yh[1] then  high else yh[1];

plot H =if close>yh then 1 else 0;
Hi @jruggiero - thank you for providing this solution. It works great!

I was wondering if it's possible to run a similar scan in pre-market to check for stocks that have broken previous WEEKLY highs / lows? Appreciate any input from more experienced members here.

Thanks for all you do, very much appreciated.
 

Similar threads

Top