Average Daily Range Indicator for ThinkorSwim

Hello everyone!

Ive been looking for/trying to create a script that uses the Average Daily Range. I have the ADR script handy and will post it below. All I'm looking for is a script I can use with my scanner in TOS to let me know if price crosses Above/Below the Upper/Lower ADR, or if price is already above/below the lower/upper adr. Thank you in advance!

Code:
#Average Daily Range Levels
#By Vah for TOS, based on cristian.d CD_Average Daily Range Zones - highs and lows of the day

#The number of days you'd like average
input lenght = 5;

def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);

plot ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
plot ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);
 
Hi everyone, I have an existing ADR script that plots a daily high and low based on the stock's open and the previous 5-day trading range. I need a simple scan coded that will allow me to specify a threshold percentage (let's say .25%), and it would return all stocks that are currently within that range of either the high or low ADR value for that day.

Is there anyone who would be willing to help me out with that? I would greatly appreciate it.

Code:
#Average Daily Range Levels
#By Vah for TOS, based on cristian.d CD_Average Daily Range Zones - highs and lows of the day

#The number of days you'd like average
input lenght = 21;

def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);

plot ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
plot ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);
 
Last edited by a moderator:
Ruby:
input lenght = 21;
input Range = .25;
def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);
def ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
def ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);
def iRange = (ADR_H - ADR_L) * Range;
plot Result = close > ADR_H - iRange or
              close < ADR_L + iRange;

I think that's what you mean, let me know if there are any issues.
 
Ruby:
input lenght = 21;
input Range = .25;
def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);
def ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
def ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);
def iRange = (ADR_H - ADR_L) * Range;
plot Result = close > ADR_H - iRange or
              close < ADR_L + iRange;

I think that's what you mean, let me know if there are any issues.

Hey Joshua, thank you so much for your help! I added that code as a custom study and created a scan with it, but it doesn't seem to be working properly. I changed the input length from 21 to 5 and have the input range dialed down to .01 (which should return tickers that are currently trading just slightly above or below their upper or lower ADR line).

Any ideas?

Thanks again for your help, Joshua. I really appreciate it.
 
I contorted it into an upper chart study, it appears to be working, at least as far as I understand what you're saying. Which apparently I don't. Did you mean you just want it to be within a certain percentage of either of the two lines? Also, would the percentage be based on the actual raw price, or based on the percentage of the difference between the two lines?
 
Last edited:
Let me know if this is what you mean; you basically want the price to be at either of the two lines, but you want to give it a little bit of wiggle room so that it does not have to be exactly at the line to work. Is this correct?

hQ7MDtK.png
 
Last edited by a moderator:
Hi Joshua, thanks again for the help. Here's a 5m chart, and the 5-day ADR high/low are the gold and red dotted lines. I drew small green boxes to illustrate the adjustable threshold I'm interested in having - changing this setting would allow you to receive a little advance notice as a stock is approaching one of these levels.

Basically, the scan would actively provide a list throughout the day of the stocks that are within that specific threshold (the green areas I marked) for either the high or low ADR levels.

Hope that helps to clarify, Joshua. If not, please let me know. :coffee:

thinkorswim-f-KQEj-JWNFM.png
 
Ok, this is an upper chart study, just for testing purposes. Its far easier to get it working here than it is go through all the trouble of setting up the actual scan.

Ruby:
input lenght = 21;
input Thresh = .001;

def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);

plot ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
plot ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);

def iRange = (ADR_H - ADR_L) * Thresh;

plot HTT = ADR_H + iRange;
plot HTB = ADR_H - iRange;

plot LTT = ADR_L + iRange;
plot LTB = ADR_L - iRange;

plot Result = (close < HTT and close > HTB) or
         (close < LTT and close > LTB);
Result.setPaintingStrategy(paintingStrategy.BOOLEAN_ARROW_UP);
Result.setDefaultColor(COLOR.cyan);
Result.setlineWeight(5);

If that looks good in testing though, this would be the scan:

Ruby:
input lenght = 21;
input Thresh = .001;

def dayHigh = DailyHighLow(AggregationPeriod.DAY).Dailyhigh;
def dayLow = DailyHighLow(AggregationPeriod.DAY).DailyLow;
def dayrange = dayHigh - dayLow;
def AvgRange = average(DayRange, lenght);

def ADR_H = open(period = AggregationPeriod.DAY) + (AvgRange/2);
def ADR_L = open(period = AggregationPeriod.DAY) - (AvgRange/2);

def iRange = (ADR_H - ADR_L) * Thresh;

def HTT = ADR_H + iRange;
def HTB = ADR_H - iRange;

def LTT = ADR_L + iRange;
def LTB = ADR_L - iRange;

plot Result = (close < HTT and close > HTB) or
              (close < LTT and close > LTB);

This version will look for the the close to be with the threshold both above and below each of the ADR lines. So if it is set at 1 percent, the total width of each threshold will be 2 percent. But if you want the thresholds to straddle the ADR lines, so that 1 percent means half a percent above and half a percent below, change this line:

Ruby:
def iRange = ((ADR_H - ADR_L) * Thresh) / 2;
 
hi I was looking for this!
But when I change the scanner to for example 15min timeframe I receive this: Secondary period not allowed: Day.
Any Idea? @Joshua
 
hi I was looking for this!
But when I change the scanner to for example 15min timeframe I receive this: Secondary period not allowed: Day.
Any Idea? @Joshua
Scripts used in scans can not reference other aggregations, this particular script is hard coded to use the daily. I could possibly modify it to work independently of aggregation. This may sound crazy; but I have no idea what this does, or what it is for, or why you would use it. I only modified the code that was originally provided. I'll have to look into it. Though, I suppose you would just like it based off the high and low of the 15 minute bars, is this correct?
 
Well, if you want to keep it as the average daily range, and not actually convert it to the average 15 minute range, etc., then it should already work the way it is. All you have to do is view the symbols from the results in what ever aggregation you want. If the close on the daily is near the line, then the 5 minute or 15 minute candles will also be there too. Its all the same data. At least as far as I understand what you're asking.
 
hmmmm but the close is only once a day or just where the price is at that moment (so before the candle close)? because as you can see the candles can reach the ADR several times a day. @Joshua
 
While the bar is live, the close is where the price is at in the moment.

Edit: Scans will also update automatically every few minutes or so.
 
Last edited:
I just loaded it up an ran it myself for the first time, seems like its working on my end. How do you have it setup? Try widening the threshold, save it to a watch list, and set the scan alert to notify you when a symbol is added or removed. Symbols should populate and depopulate as the day goes on. Again, its not actually designed for your purpose though, but it should sort of work for you in a basic way.
 

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