How to get the ATR of the RTH of previous 10 days

Trismagistus

New member
How do I code the Average DAILY Range, from the previous 10 days, omitting data from extended hours?

The result should not depend on whether or not my chart style settings show or hide extended hours, nor on what intraday chart time settings I'm displaying.

The code doesn't need to be fancy or flexible. I don't need a plot, just the number. The simpler the better.

I've searched useThinkscript and the ltc.thinkorswim reference library but haven't found an example of this exact calculation.

Any help would be greatly appreciated.
 
Solution
How do I code the Average DAILY Range, from the previous 10 days, omitting data from extended hours?

The result should not depend on whether or not my chart style settings show or hide extended hours, nor on what intraday chart time settings I'm displaying.

The code doesn't need to be fancy or flexible. I don't need a plot, just the number. The simpler the better.

I've searched useThinkscript and the ltc.thinkorswim reference library but haven't found an example of this exact calculation.

Any help would be greatly appreciated.

your words are asking for 2 different things,
do you want ATR or a daily range average ?


this finds an average, of a daily range, over x days.
can choose to include current day or not.
it finds...
How do I code the Average DAILY Range, from the previous 10 days, omitting data from extended hours?

The result should not depend on whether or not my chart style settings show or hide extended hours, nor on what intraday chart time settings I'm displaying.

The code doesn't need to be fancy or flexible. I don't need a plot, just the number. The simpler the better.

I've searched useThinkscript and the ltc.thinkorswim reference library but haven't found an example of this exact calculation.

Any help would be greatly appreciated.

your words are asking for 2 different things,
do you want ATR or a daily range average ?


this finds an average, of a daily range, over x days.
can choose to include current day or not.
it finds the highest and lowest prices, for each day, during normal trading hours, for the symbol.
subtracts them to find the range.

can turn on test bubbles to see values, to verify the numbers

Code:
# xdays_range_avg_00c

#https://usethinkscript.com/threads/how-to-get-the-atr-of-the-rth-of-previous-10-days.15336/
#How to get the ATR of the RTH of previous 10 days

#----------------
# x days range avg
# get high , low, from last x days
# calc a range each day
# calc avg of the days ranges

def bn = barnumber();
def na = double.nan;

# define normal trading hours
def daytime = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and GetTime() < RegularTradingEnd(getYYYYMMDD());

# ----------------------
# firstlastdaybars01
# https://usethinkscript.com/threads/finding-the-first-and-last-bar-of-the-day-in-thinkorswim.526/
# Author: Kory Gill, @korygill
# Identify first bar of day and last bar of day on chart
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;
def lastBarOfDay = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBarOfDay[-1]) then 1 else 0;
# ----------------------

# day rev cnt
input days_back = 10;
input include_current_day = no;

def istoday = if GetLastDay() == GetDay() then 1 else 0;
def newday = if getday() != getday()[1] then 1 else 0;
def daycnt = if bn == 1 then 1 else if (!isnan(close) and newday) then daycnt[1] + 1 else daycnt[1];
def maxdays = highestall(daycnt);
def revdaycnt = maxdays - daycnt + 1;
def daystoomany = if days_back < 1 or days_back > maxdays then 1 else 0;

# ----------------------

# get highest and lowest on each day
def dayhi = if bn == 1 or newday then high
 else max(high, dayhi[1]);

def daylo = if bn == 1 or newday then low
 else min(low, daylo[1]);

input test2_hilo_lines = no;
plot zhi = if test2_hilo_lines then dayhi else na;
plot zlo = if test2_hilo_lines then daylo else na;

# write highest and lowest on each day , to last bar of day
def dayoff = if include_current_day then 0 else 1;
def dayzmax = (days_back + dayoff);
def dayzmin = dayoff;

def dayhi2 = if (revdaycnt <= (days_back + dayoff) and revdaycnt > dayoff) and lastBarOfDay then dayhi else 0;
def daylo2 = if (revdaycnt <= (days_back + dayoff) and revdaycnt > dayoff) and lastBarOfDay then daylo else 0;
def dayrng2 = dayhi2 - daylo2;

# calc avg of x day range
def rngavg = totalsum(dayrng2)/days_back;

addlabel(1, asdollars(rngavg) + " average of daily range, for past " + days_back + " days", color.yellow);


# ----------------------
# ----------------------


addlabel(daystoomany, " ONLY " + MAXDAYS + " DAYS ON CHART. enter a different number for days back", color.cyan);


input test3_first_last_bars_of_day = no;
AddChartBubble(
  test3_first_last_bars_of_day and firstBarOfDay,
  close,
  "First Bar of Day",
  Color.GREEN,
  yes);

AddChartBubble(
  test3_first_last_bars_of_day and lastBarOfDay,
  close,
  "Last Bar of Day",
  Color.GREEN,
  no);


input test3 = no;
addchartbubble(test3, low,
dayhi2 + "\n" +
daylo2 + "\n" +
dayrng2 + "\n" +
dayzmax + "\n" +
dayzmin + "\n" +
rngavg
, (if dayhi2 > 0 then color.yellow else color.gray), no);


input test1_day_cnt = no;
addchartbubble(test1_day_cnt, low,
newday + " new\n" +
daycnt + " cnt\n" +
revdaycnt + " rev\n" +
maxdays + " max\n" 
, color.yellow, no);


input test1 = no;
addchartbubble(test1, low, 
"day rng\n" +
#dayrng + "\n" +
"\n" +
"rng avg\n" +
" "
#dayrngavg
, color.yellow, no);

input test2_vert = no;
addverticalline(test2_vert and !daytime, "--");
# ----------------------
#
 
Solution

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