# DTR vs ATR Indicator for ThinkorSwim

C

##### New member
I was looking at the original thread for this script and they said, they were having some issue with values. Did you figure it out?

It looks great to me. An example of this is SFIX from yesterday. It shows a DTR of \$5.26.. That's accurate as the low from that day was 18.84 and the high was 24.10 (24.10 - 18.84 = 5.26) The percentage it outputs (which is what i would love to get in a scan) is 319% above it's ATR which is also accurate. Hope that helps.

C

##### New member
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);``````

V

#### VicD

##### Member
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:
C

#### convertiblejay

##### New member
@Shinthus does the percentage stop at 100% or can it go past it?

V

#### VicD

##### Member
@Shinthus does the percentage stop at 100% or can it go past it?
No it does not it's just basic division. SRNE shows 919.84%

C

#### convertiblejay

##### New member
No it does not it's just basic division. SRNE shows 919.84%

which one are you using? i used the one on the very first page posted by shin

V

#### VicD

##### Member
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:
V

#### VicD

##### Member
@BenTen thanks, could not remember what the actual thing was code vs quote.

C

#### convertiblejay

##### New member
@VicD Explain how to use it please

J

#### jay2

##### Member
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``````

T

##### Active member
2019 Donor
Could we get the daily range to work on a smaller time frame chart ?

#### jngy2k

##### Member
VIP
Code:
``````declare lower;

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

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

^ 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:
Z

#### zwedle

##### New member
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

VIP

Z

#### zwedle

##### New member
@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);

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"));``````

#### MerryDay

##### Active member
VIP

something like:

Code:
``````AddLabel(showlabel, "DayRange: " + Round(DR , 2) + " vs. ATR:" + Round(ATR, 2),
if DR < ATR then color.green else color.red);``````

Z

#### zwedle

##### New member
@MerryDay that's what I was trying to do, Thank you.

H

##### New member
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));``````

G

#### generic

##### New member
@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