Relative Opening Range Stats For ThinkOrSwim

rlohmeyer

Active member
Thanks to the rather brilliant code by @korygill for locating the first and last bar of the day located HERE and @Svanoy for relative volume located HERE I was able to cobble together a relative opening range stats indicator, which I find helpful in trading the 30 minute opening range, which I find is reliably significant to day trade as support or resistance.

Below is an image of the Relative Opening Range Stats. And below that is the code. Notice on the chart the location of todays Opening Range and how price traded at this area (4/6/2022 Fed Day).

Z0Mj93h.jpg


Code:
#first bar of day range
#Code Logic: Kory Gill, @korygill
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
input ag = aggregationPeriod.five_MIN;
def rng = if firstBarOfDay then round(high(period = ag)-low(period = ag),2) else rng[1];

#avg range of x days back
#Code Logic @Svanoy
input Number_Of_Values = 5;
def Value = rng;
def NotToday = GetDay() <> GetLastDay();
def cond = (secondsFromTime(0930)==0) and NotToday;
def ValueCount = if cond then ValueCount[1] + 1 else ValueCount[1];
def bar = BarNumber();
def BarNum = if !IsNaN(close) and BarNumber()>0 then bar else BarNum[1];
def VBar = HighestAll(BarNum);
def FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(ValueCount, -b);

def ValueRangeTotal;
if BarNumber() == 0 {ValueRangeTotal = 0;}
else if FinalCount - ValueCount < Number_Of_Values and cond
 {ValueRangeTotal = ValueRangeTotal[1] + Value;
} else {ValueRangeTotal = ValueRangeTotal[1];}

def RangeAverage = if (ValueRangeTotal /  Number_Of_Values) > 0 and (FinalCount - ValueCount) <= 0 then (ValueRangeTotal /  Number_Of_Values) else Double.NaN;

def Average = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(RangeAverage, -c);

addlabel(yes,"OR Stats: " + " Bar Agg|" + ag/60000 +" Min" + "  Bars In Avg|"+ Number_Of_Values +"  AOR|$"+round(average,2) + "   OR|$" + round(rng,2),color.LIME);
 
Thanks to the rather brilliant code by @korygill for locating the first and last bar of the day located HERE and @Svanoy for relative volume located HERE I was able to cobble together a relative opening range stats indicator, which I find helpful in trading the 30 minute opening range, which I find is reliably significant to day trade as support or resistance.

Below is an image of the Relative Opening Range Stats. And below that is the code. Notice on the chart the location of todays Opening Range and how price traded at this area (4/6/2022 Fed Day).

View attachment 14221
Something wrong with this part of code.

addlabel(yes,"OR Stats: " + " Bar Agg|" + ag/60000 +" Min" + " Bars In Avg|"+ Number_Of_Values +" AOR|$"+round(average,2) + " OR|$" + round(rng,2),color.LIME)
 
Something wrong with this part of code.

addlabel(yes,"OR Stats: " + " Bar Agg|" + ag/60000 +" Min" + " Bars In Avg|"+ Number_Of_Values +" AOR|$"+round(average,2) + " OR|$" + round(rng,2),color.LIME)
A "Something wrong" post does not provide enough information to say where you went astray.

This shared chart link has the indicator applied to it successfully.

Import this link to add the chart and indicator to your app: http://tos.mx/v6OloIU
Click here for --> Easiest way to load shared links
gpGa2yD.png
 
Last edited:
Can this study be converted into watchlist coloumn? i find it very useful to know which stock is "tradeable" based on their volatility in the first 15 mins compare to avg of orb 5 days ago. thank you.
 
Can this study be converted into watchlist coloumn? i find it very useful to know which stock is "tradeable" based on their volatility in the first 15 mins compare to avg of orb 5 days ago. thank you.
Sure, you can.
Obviously, you need to create a separate column script for each of the stats in the label.

Here is the script for an AOR column, copy it and create additional script for OR
by changing the last line:
plot AOR = round(average,2);
to:
plot OR = round(rng,2) ;

AOR Watchlist Column Script:
Ruby:
#first bar of day range
#Code Logic: Kory Gill, @korygill
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
input ag = aggregationPeriod.five_MIN;
def rng = if firstBarOfDay then round(high(period = ag)-low(period = ag),2) else rng[1];

#avg range of x days back
#Code Logic @Svanoy
input Number_Of_Values = 5;
def Value = rng;
def NotToday = GetDay() <> GetLastDay();
def cond = (secondsFromTime(0930)==0) and NotToday;
def ValueCount = if cond then ValueCount[1] + 1 else ValueCount[1];
def bar = BarNumber();
def BarNum = if !IsNaN(close) and BarNumber()>0 then bar else BarNum[1];
def VBar = HighestAll(BarNum);
def FinalCount = fold b = 0 to VBar while !IsNaN(GetValue(close, -b)) do GetValue(ValueCount, -b);

def ValueRangeTotal;
if BarNumber() == 0 {ValueRangeTotal = 0;}
else if FinalCount - ValueCount < Number_Of_Values and cond
 {ValueRangeTotal = ValueRangeTotal[1] + Value;
} else {ValueRangeTotal = ValueRangeTotal[1];}

def RangeAverage = if (ValueRangeTotal /  Number_Of_Values) > 0 and (FinalCount - ValueCount) <= 0 then (ValueRangeTotal /  Number_Of_Values) else Double.NaN;

def Average = fold c = 0 to VBar while !IsNaN(GetValue(close, -c)) do GetValue(RangeAverage, -c);
plot AOR = round(average,2);
 

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