Per multiple requests:
https://usethinkscript.com/threads/convert-tradingview-adr-v2-average-daily-range.15891/
i looked at the original code and converted it.
https://usethinkscript.com/threads/convert-tradingview-adr-v2-average-daily-range.15891/
i looked at the original code and converted it.
Code:
#adr_avg_daily_convert
#https://usethinkscript.com/threads/chatgpt-bard-other-ai-scripts-which-cant-be-used-in-thinkorswim.13822/page-8#post-128007
#https://www.tradingview.com/script/TcRklUf2-ADR-Average-Daily-Range-treypeng-v2/
#ADR - Average Daily Range [@treypeng] [v2]
#trespeng Oct 12, 2018
def na = double.nan;
def bn = barnumber();
#//@version=2
#// Had to use V2 of Pinescript for this, there are some odd quirks with open/close prices in v3. Not really sure what's going on there.
#study(title="ADR.v2 - Average Daily Range [@treypeng]", shorttitle="ADR [@treypeng]", overlay=true)
#length = input(defval=5, title="Length", type=integer, minval=1)
#offset = input(defval=0, title="Offset", type=integer, minval=0)
#use_av = input(defval="EMA", title="EMA/SMA", type=string)
#today_only = input(defval=false, title="Show Today Only", type=bool)
input length = 5;
input offset = 0;
input use_av = AverageType.EXPONENTIAL;
input today_only = no;
#// Used to calculate true range
#high0 = security(tickerid, 'D', high[offset])
#low0 = security(tickerid, 'D', low[offset])
#close1 = security(tickerid, 'D', close[1 + offset])
#open0 = security(tickerid, 'D', open)
def agg = aggregationperiod.day;
def high0 = high(period = agg)[offset];
def low0 = low(period = agg)[offset];
def close1 = close(period = agg)[offset];
def open0 = open(period = agg)[offset];
#datr = max(max(high0 - low0, abs(high0 - close1)), abs(low0 - close1))
#smooth = security(tickerid, 'D', use_av=="SMA" ? sma(datr, length) : ema(datr, length))
def datr = max(max(high0 - low0, absvalue(high0 - close1)), absvalue(low0 - close1));
def smooth = Movingaverage(use_av, datr, length);
#milliseconds_in_1day = 1000 * 60 * 60 * 24 * 1
#midnight_today = timenow - (timenow % milliseconds_in_1day)
# times (EST)
input start = 0930;
input end = 1600;
def daytime = if secondsfromTime(start) >= 0 and secondstillTime(end) > 0 then 1 else 0;
#leftborder = time >= midnight_today or (not today_only)
#rightborder = false //barstate.isrealtime
#p1 = plot(open0 + smooth, color=leftborder and not rightborder ? green : na, title='Upper ADR', linewidth=2, style=cross)
#p2 = plot(open0 - smooth, color=leftborder and not rightborder ? red : na, title='Lower ADR', linewidth=2, style=cross)
plot p1 = if daytime then (open0 + smooth) else na;
plot p2 = if daytime then (open0 - smooth) else na;
#
Last edited by a moderator: