# 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, "--");
# ----------------------
#