# Time Range Comparison For ThinkOrSwim

#### FutureTony

##### Well-known member
VIP
This is a bit of a different study. It takes a specific timeframe (eg. 9:30 to 10:30) and compares the current range of movement to the average range in that timeframe. It is not comparing the average daily range, just the range in that timeframe (for the duration of the chart). It prints this information in a label on the top left:

When the given 'range' has ended, the label will simply show the usual expected range on that timeframe. There is also an option to show 'targets' that would be the usual range +/- the open price. Not sure how useful those are just yet so they are hidden by default. This is intended for daytrading types and probably has little value on stocks and timeframes larger than a 4hr.

The open/close of the range are available via options. So if you normally trade from 2:30am to 4:00am, you can use those values. Default is 9:30am to 10:30am. You could even change to a larger range like 9am to noon.

I haven't seen a study that does something like this before so I'm all ears for ideas the community might have for this one.

Ruby:
``````# Created by @tony_futures
# Custom range script comparing average first hour for the length of the chart to current day

declare upper;
declare hide_on_daily;
input RoundLevel = 0;
input openStart = 0930;
input openEnd = 1030;
input alwaysShowCompare = no;

def openActive = SecondsTillTime(openEnd) > 0 and SecondsFromTime(openStart) >= 0;
Def ORActive = secondstilltime(openEnd)>0 AND secondsfromtime(openStart)>=0;
def RTHOpen = if ORActive and !ORActive[1] then open else RTHopen[1];
def ORHigh = if ORActive and !ORActive[1] then high else if ORActive and high > ORhigh[1] then high else ORhigh[1];

def ORlow = if ORActive and !ORActive[1] then low else if ORActive and low < ORlow[1] then low else ORlow[1];

def TodayRange = ORHigh - ORLow;

def endOfopen = !openActive and openActive[1];
def openRange = if endOfopen then TodayRange else Double.NaN;
def openCount = if endOfopen then openCount[1] + 1 else openCount[1];
def openCum = if endOfopen then openCum[1] + openRange else openCum[1];
def openAvg = openCum/openCount;

def OpenPercentage = Round((TodayRange / openAvg) * 100, RoundLevel);

def Today = GetLastDay() == GetDay();
AddLabel ((Today and ORActive) OR AlwaysShowCompare, "OR: " + Round (TodayRange , RoundLevel) + " vs Avg: " + round (openAvg,RoundLevel)+ " | " + round (OpenPercentage,RoundLevel) + "%", (if OpenPercentage <= 40 then Color.LIGHT_GREEN else if OpenPercentage >= 85 then Color.LIGHT_RED else Color.WHITE));
AddLabel(Today and !ORActive, "Expected Range: " + RoundDown(openAvg,RoundLevel), Color.WHITE);

def expHigh = (RTHOpen + openAvg);
def expLow = (RTHOpen - openAvg);

input showPossibleRange = no;
AddLabel(showPossibleRange and Today and !ORActive, "Possible High Range: " + RoundDown(expHigh, RoundLevel), Color.WHITE);
AddLabel(showPossibleRange and Today and !ORActive, "Possible Low Range: " + RoundUp(expLow, RoundLevel), Color.WHITE);``````

Last edited:
@FutureTony if you load the script that i gave you as sample for day trading range, it lets us shows the input of day as ATR so can we add that option? Also it shows that the avg is and what is today so for eg if you load the DTR script it show the label in green in green in this screenshot and white one is what you created. It might do the comparison during RTH if it will do what the green label is doing please let me know and if its not doing that can we make it to do it similar thing.

So whats the default length it is using? i think it is taking it from the numbers of days on the chart correct?

Thanks for all your hard work and time i really appreciate it.

@FutureTony if you load the script that i gave you as sample for day trading range, it lets us shows the input of day as ATR so can we add that option? Also it shows that the avg is and what is today so for eg if you load the DTR script it show the label in green in green in this screenshot and white one is what you created. It might do the comparison during RTH if it will do what the green label is doing please let me know and if its not doing that can we make it to do it similar thing.

So whats the default length it is using? i think it is taking it from the numbers of days on the chart correct?

Thanks for all your hard work and time i really appreciate it.
Yes, it uses the number of days on your current chart to calculate the average range. Once your range 'starts', it will show you that comparison. I hid the comparison before and after the time range ends as it is kind of meaningless at that point and only show the average range during that time period. I watched it yesterday and it worked well. Will be interesting to keep an eye on it in the future and see how it performs.

Hello can we show it after the range time as well and we can leave it closed before the time. Thanks!

Hello can we show it after the range time as well and we can leave it closed before the time. Thanks!
I've changed the code above so you can always show the highs and lows by switching it on or off. I think that is what you were after. Cheers!

Thanks i appreciate...are you on discord?

I tested it today however after 1030 it shuts off? Thanks

I tested it today however after 1030 it shuts off? Thanks
Yes, I am on discord. It shows the percentage of the range while inside the range you've selected. So...if you have it set from 9:30 to 10:30, it is showing you how close you are to the average range during that time. I found it weird to keep showing after the range was over. But...I can change it to keep it up all the time as well.

Can you add this as an optional thing where we choose to show later as yes or no? Whats your discord ?

I have a though on NQ can drop u detailed note on discord if you want to add me mine Fast6804.. Thanks and look forward hearing from you.

Can you add this as an optional thing where we choose to show later as yes or no? Whats your discord ?
I've added an option to always show in the updated code above. And I've sent you a friend request on discord. TTYL.

This is a really cool study/analysis. There must be some way to incorporate vix or relative volume into this equation to get an idea of whether the initial balance is likely to expand or contract from the average. I would imagine overnight volume, overnight range, vix (as some multiplier) and rth volume in Period A (first 30 minutes) would prove helpful.

So this area here that determines the average, what is the look back for the average calculation and is that something we can input manually rather than preset?
def endOfopen = !openActive and openActive[1];
def openRange = if endOfopen then TodayRange else Double.NaN;
def openCount = if endOfopen then openCount[1] + 1 else openCount[1];
def openCum = if endOfopen then openCum[1] + openRange else openCum[1];
def openAvg = openCum/openCount;

def OpenPercentage = Round((TodayRange / openAvg) * 100, RoundLevel);

So this area here that determines the average, what is the look back for the average calculation and is that something we can input manually rather than preset?
def endOfopen = !openActive and openActive[1];
def openRange = if endOfopen then TodayRange else Double.NaN;
def openCount = if endOfopen then openCount[1] + 1 else openCount[1];
def openCum = if endOfopen then openCum[1] + openRange else openCum[1];
def openAvg = openCum/openCount;

def OpenPercentage = Round((TodayRange / openAvg) * 100, RoundLevel);
The average is calculated based on the current chart data. So if you change to 20 days, you will get the 20 day average.

87k+ Posts
363 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

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?