ADR.v2 - Average Daily Range For ThinkOrSwim

halcyonguy

Moderator - Expert
VIP
Lifetime
Per multiple requests:
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;

#

LbE2fkN.jpg
 
Last edited by a moderator:

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