DTR vs ATR Indicator for ThinkorSwim

If anyone's interested. I found out how to scan for the percentage. It was quite easy. All the praise goes to the original creator of this script but here's the code to be able to scan by %

Code:
# Custom ATR Plot by 7of9 for BRT
# edited 3/20/19
input AtrAvgLength = 14;

def ATR = WildersAverage(TrueRange(high(period = aggregationPeriod.DAY), close(period = aggregationPeriod.DAY), low(period = aggregationPeriod.DAY)), AtrAvgLength);

def TodayHigh = Highest(high(period = aggregationPeriod.DAY), 1);
def TodayLow = Lowest(low(period = aggregationPeriod.DAY), 1);

def DTR = TodayHigh - TodayLow;

plot DTRpct = Round((DTR / ATR) * 100, 0);
 
Great script. Can you tell me how I would set it to calculate the first 5, 15, 0r 30 minutes of open as part of ATR? So the opening range as percent of ATR? Tried hacking it together with an OTR scrips and getting 0's

Code:
# ------ START CODE

input ATRPeriod = 20; #Hint ATRPeriod: The ATR period for previous days, default 14.

def period = AggregationPeriod.DAY;
def ATR = MovingAverage(AverageType.WILDERS, TrueRange(high(period = period), close(period = period), low(period = period)), ATRperiod);
def showATralways = yes;



# inputs
input orStartTime = 0930;
input orEndTime = 1000;
input periodOTR = aggregationPeriod.THIRTY_MIN;
input showOnlyToday = yes;
input showCloud = yes;


# constants
def na = double.nan;
def hi = high(period = periodOTR);
def lo = low(period = periodOTR);
defineGlobalColor("Cloud", color.gray);

# opening range time logic
def isOr = secondstilltime(orEndTime) > 0
    and secondsfromtime(orStartTime) >= 0;
def today = (!showOnlyToday or getday() == getlastday())
    and secondsfromtime(orStartTime) >= 0 and !isNAN(close);

# opening range levels logic
rec orhi =
    if orhi[1] == 0
        or !isOr[1]
        and isOr
    then hi
    else if isOr
        and hi > orhi[1]
    then hi
    else orhi[1];

rec orlo =
    if orlo[1] == 0
        or !isOr[1]
        and isOr
    then lo
    else if isOr
        and lo < orlo[1]
    then lo
    else orlo[1];

def OTRrng = Round((hi - lo),2);
AddLabel(showATRalways, "DayRng: " + OTRrng + " vs. ATR: " + Round(ATR,2) + " (" + Round(((OTRrng/ATR)*100),2) +"%)" , color.green);

# ------- END CODE
 
Last edited by a moderator:
Ok. So this is a messy code but it does what it needs to do. Please feel free to edit. This script is the Daily Opening Range of ATR. Those of you that are momentum traders will see the reason for it.

Code:
# ------ START CODE

input ATRPeriod = 20; #Hint ATRPeriod: The ATR period for previous days, default 14.

def period = AggregationPeriod.DAY;
def ATR = MovingAverage(AverageType.WILDERS, TrueRange(high(period = period), close(period = period), low(period = period)), ATRperiod);
def showATralways = yes;



# inputs
input orStartTime = 0930;
input orEndTime = 1000;
input periodOTR = aggregationPeriod.THIRTY_MIN;
input showOnlyToday = yes;
input showCloud = yes;


# constants
def na = double.nan;
def hi = high(period = periodOTR);
def lo = low(period = periodOTR);


# opening range time logic
def isOr = secondstilltime(orEndTime) > 0
    and secondsfromtime(orStartTime) >= 0;
def today = (!showOnlyToday or getday() == getlastday())
    and secondsfromtime(orStartTime) >= 0 and !isNAN(close);


# opening range levels logic
rec OR100 =
    if OR100[1] == 0
        or !isOr[1]
        and isOr
    then hi
    else if isOr
        and hi > OR100[1]
    then hi
    else OR100[1];

rec OR0 =
    if OR0[1] == 0
        or !isOr[1]
        and isOr
    then lo
    else if isOr
        and lo < OR0[1]
    then lo
    else OR0[1];


def range = OR100 - OR0;



def OTRrng = range;
AddLabel(showATRalways, "DORng: " + OTRrng + " vs. ATR: " + Round(ATR,2) + " (" + Round(((OTRrng/ATR)*100),2) +"%)" , color.green);




# ------- END CODE
 
Last edited by a moderator:
Would like to add horizontal lines (High/Low levels) to follow the code below on the chart. Is it possible?

Code:
# ATR Daily Range
# tomsk
# 12.16.2019

# V1.0 - 12.16.2019 - tomsk   - Initial release ATR Daily Range
# V1.1 - 11.15.2019 - tomsk   - Added performance relative to ATR range

# Displays ATR High/Low thresholds relative to daily open

input length = 14;
input averageType = AverageType.WILDERS;

def o = open(period = AggregationPeriod.DAY);
def h = high(period = AggregationPeriod.DAY);
def l = low(period = AggregationPeriod.DAY);
def c = close(period = AggregationPeriod.DAY);
def R = (c - l) / (h - l);
def ATRD = MovingAverage(averageType, TrueRange(h, c, l), length);

AddLabel(1, "ATR Daily High/Low Level = [ " + Round(o+ATRD,2) + " / " + Round(o-ATRD,2) + " ]", Color.PINK);
AddLabel(1, "Current Close = " + close + " [ " + AsPercent(R) + " ]", Color.YELLOW);

# End ATR Daily Range
 
Code:
declare lower;

input length = 14;
input averageType = AverageType.WILDERS;

plot ATR = MovingAverage(averageType, TrueRange(high, close, low), length);
ATR.SetDefaultColor(GetColor(8));

AddLabel(yes, Concat("ATR=", ATR), Color.YELLOW);

^ What is the addLabel script I need to write in addition to the code provided that gives the calculation of ATR (length provided input) as a % of current closest price?

nm figured it out

AddLabel (yes, "ATR: " + Round((ATR / close) * 100, 1) + "%", Color.WHITE);
 
Last edited:
Can someone tell me what I'd write into the stock script to make the label turn green from red once the DTR surpasses the ATR?

Thank you
 
@MerryDay not exactly.

chada456's comes close with this part of the code:
#AddLabel(yes, “DATR: “+ATR, if ATR > 1.00 then Color.GREEN else if ATR > .50 then Color.YELLOW else Color.WHITE);
AddLabel(yes, Concat(“DATR= ”, ATR), Color.YELLOW);


Do you know how I could add that color rule into this indicator?

Code:
input ATRLength = 14;
input averagetype = AverageType.WILDERS;
input BasePeriod = AggregationPeriod.DAY;
input showlabel = yes;

def ATR = MovingAverage (averagetype, TrueRange(high(period = BasePeriod)[1], close(period = BasePeriod)[1], low(period = BasePeriod)[1]), ATRLength);

def Today_High = Highest(high(period = BasePeriod)[0], 1);
def Today_Low = Lowest(low(period = BasePeriod)[0], 1);

def DR = Today_High - Today_Low;

AddLabel(showlabel, "DayRange: " + Round(DR , 2) + " vs. ATR:" + Round(ATR, 2));

plot Momentum = close - close[ATRLength];
Momentum.DefineColor("Positive", Color.UPTICK);
Momentum.DefineColor("Negative", Color.DOWNTICK);
Momentum.AssignValueColor(if Momentum >= 0 then Momentum.Color("Positive") else Momentum.Color("Negative"));
 
@zwedle Did you try adding an if statement to the AddLabel line in your study ?

something like:

Code:
AddLabel(showlabel, "DayRange: " + Round(DR , 2) + " vs. ATR:" + Round(ATR, 2),
          if DR < ATR then color.green else color.red);
 
Can anyone help me upgrade this label indicator?

I've been using the DTR vs. ATR label below which is just OKAY in that it's only somewhat useful to know the information it provides.

What would make it more useful would be to know if the ATR is INCREASING or DECREASING over the period used in the formula (5 days in this script).

The challenge is to add "increasing" or "decreasing" to the label, where ATR is rising or falling.

Any ideas on how to tackle this problem?

Code:
# Custom ATR Plot by 7of9 for BRT
# edited 3/20/19

declare upper;

input AtrAvgLength = 14;

def ATR = WildersAverage(TrueRange(high(period = aggregationPeriod.DAY), close(period = aggregationPeriod.DAY), low(period = aggregationPeriod.DAY)), AtrAvgLength);

def TodayHigh = Highest(high(period = aggregationPeriod.DAY), 1);
def TodayLow = Lowest(low(period = aggregationPeriod.DAY), 1);

def DTR = TodayHigh - TodayLow;

def DTRpct = Round((DTR / ATR) * 100, 0);

AddLabel (yes, "DTR " + Round (DTR , 2) + " vs. ATR " + round (ATR,2)+ " = " + round (DTRpct,0) + "%", (if DTRpct <= 70 then Color.GREEN else if DTRpct >= 90 then Color.RED else Color.ORANGE));
 
@hydroflask Do another for decreasing and put them into conditions for AddLabel.

def increasing = if ATR > ATR[1] > ATR[2] > ATR[3] > ATR[4] then 1 else Double.Nan
 

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