ATR Percentile Rank Indicator for ThinkorSwim

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
@diazlaz was kind enough to help me port this version of ATR Percentile Rank from TradingView over to ThinkorSwim. I thought it was interesting.

CM ATR PercentileRank - Great For Showing Market Bottoms.

When Increased Volatility to the Downside Reaches Extreme Levels it’s Usually a Sign of a Market Bottom.

This Indicator Takes the ATR and uses a different LookBack Period to calculate the Percentile Rank of ATR Which is a Great Way To Calculate Volatility

Be Careful Of Using w/ Market Tops. Not As Reliable.


***Ability to Control ATR Period and set PercentileRank to Different Lookback Period

***Ability to Plot Histogram Just Showing Percentiles or Histogram Based on Up/Down Close

Fuchsia Lines = Greater Than 90th Percentile of Volatility based on ATR and LookBack Period.
Red Lines = Warning — 80-90th Percentile
Orange Lines = 70-80th Percentile

For this ToS version:
  • Magenta = Greater than 90th Percentile
  • Orange = 70-80th
  • Red = 80-90

C7U3IEF.png


thinkScript Code

Code:
#CM_ATR_Percentile for ThinkorSwim Version 1.0
#
#VERSION
# 2020.01.03 V1.0 @diazlaz - Initial Port
#
#LINK/CREDITS
#https://www.tradingview.com/script/D56mCdlz-CM-ATR-PercentileRank/
#https://www.tradingview.com/u/ChrisMoody/
#
#INSTRUCTION
#CM ATR PercentileRank - Great For Showing Market Bottoms.
#
#When Increased Volatility to the Downside Reaches Extreme Levels
#it’s Usually a Sign of a Market Bottom.
#
#This Indicator Takes the ATR and uses a different LookBack Period
#to calculate the Percentile Rank of ATR Which is a Great Way To
#Calculate Volatility
#

declare lower;

#INPUTS
input length = 5; #ATR Length
input length2 = 50; #no. of Bars the PercentileRank uses to Calculate % Values
input sn = no; #Show Normal Histogram? Uncheck = Histogram based on Up/Down Close
input paintBars = yes;
input showLabels = yes;

#LOGIC

#LABELS
AddLabel (showLabels, "ATR Percentile for ThinkorSwim Version 1.0", COLOR.ORANGE);

#//ATR and PercentileRank Calculations
def atr = Average(TrueRange(high, close, low), length);

def percentRankCount = fold i = 1 to length2 + 1 with count = 0
do
  if atr[0] > atr[i] then
    count + 1
  else
    count;

def percentRank = Round( percentRankCount / length2 * 100.0, 0);

def down = close < close[1];
def up = close > close[1];

#//Calculation for Showing Histogram based on Up/Down Close
def pctileRankFinal = if up then percentRank else if down then percentRank * -1 else Double.NaN;

#PLOTS
plot pATRRank = if(sn,percentRank,pctileRankFinal);
pATRRank.SetPaintingStrategy(paintingStrategy = PaintingStrategy.HISTOGRAM);
pATRRank.AssignValueColor(
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else
COLOR.DARK_GRAY
);
pATRRank.SetLineWeight(5);

#COLORBARS
AssignPriceColor(if paintbars then
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else COLOR.DARK_GRAY
else
COLOR.CURRENT
);

#END OF CM_ATR_Percentile for ThinkorSwim Version 1.0
 
Last edited by a moderator:
This looks promising. I will definitely test it out.
It would be nice to have an intraday study version that adjusts automatically. Just like the volume profile for ORB indicator.
 
@princesly It should work with intraday timeframe (5m, 15m, etc). You can also adjust the ATR length.
 
@BenTen, I see thanks. Where on the script can i rem out the coloring on the candlesticks? I ask because the screenshot you have here is exactly what i was expecting but the current script colors all the candlestick
 
Last edited:
PercentRank
Code:
declare lower;

def percentRankCount = fold i = 1 to length with count = 0
do
  if close[0] > close[i] then
    count + 1
  else
    count;

def percentRank = percentRankCount / length * 100;

plot PRank = percentRank;
 
Last edited by a moderator:
Today's range is at what percentile of the previous 252 days - I have been trying to get this by modifying the above code. But I am having a difficulty. Can someone please let me know what I am missing in the below code?

I basically changed this
input length = 1; #ATR Length
input length2 = 252; #no. of Bars the PercentileRank uses to Calculate % Values

Code:
#CM_ATR_Percentile for ThinkorSwim Version 1.0
#
#VERSION
# 2020.01.03 V1.0 @diazlaz - Initial Port
#
#LINK/CREDITS
#https://www.tradingview.com/script/D56mCdlz-CM-ATR-PercentileRank/
#https://www.tradingview.com/u/ChrisMoody/
#
#INSTRUCTION
#CM ATR PercentileRank - Great For Showing Market Bottoms.
#
#When Increased Volatility to the Downside Reaches Extreme Levels
#it’s Usually a Sign of a Market Bottom.
#
#This Indicator Takes the ATR and uses a different LookBack Period
#to calculate the Percentile Rank of ATR Which is a Great Way To
#Calculate Volatility
#

declare lower;

#INPUTS
input length = 1; #ATR Length
input length2 = 252; #no. of Bars the PercentileRank uses to Calculate % Values
input sn = no; #Show Normal Histogram? Uncheck = Histogram based on Up/Down Close
input paintBars = yes;
input showLabels = yes;

#LOGIC

#LABELS
AddLabel (showLabels, "ATR Percentile for ThinkorSwim Version 1.0", COLOR.ORANGE);

#//ATR and PercentileRank Calculations
def atr = Average(TrueRange(high, close, low), length);

def percentRankCount = fold i = 1 to length2 + 1 with count = 0
do
  if atr[0] > atr[i] then
    count + 1
  else
    count;

def percentRank = Round( percentRankCount / length2 * 100.0, 0);

def down = close < close[1];
def up = close > close[1];

#//Calculation for Showing Histogram based on Up/Down Close
def pctileRankFinal = if up then percentRank else if down then percentRank * -1 else Double.NaN;

#PLOTS
plot pATRRank = if(sn,percentRank,pctileRankFinal);
pATRRank.SetPaintingStrategy(paintingStrategy = PaintingStrategy.HISTOGRAM);
pATRRank.AssignValueColor(
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else
COLOR.DARK_GRAY
);
pATRRank.SetLineWeight(5);

#COLORBARS
AssignPriceColor(if paintbars then
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else COLOR.DARK_GRAY
else
COLOR.CURRENT
);

#END OF CM_ATR_Percentile for ThinkorSwim Version 1.0
 
Today's range is at what percentile of the previous 252 days - I have been trying to get this by modifying the above code. But I am having a difficulty. Can someone please let me know what I am missing in the below code?

I basically changed this
input length = 1; #ATR Length
input length2 = 252; #no. of Bars the PercentileRank uses to Calculate % Values

Code:
#CM_ATR_Percentile for ThinkorSwim Version 1.0
#
#VERSION
# 2020.01.03 V1.0 @diazlaz - Initial Port
#
#LINK/CREDITS
#https://www.tradingview.com/script/D56mCdlz-CM-ATR-PercentileRank/
#https://www.tradingview.com/u/ChrisMoody/
#
#INSTRUCTION
#CM ATR PercentileRank - Great For Showing Market Bottoms.
#
#When Increased Volatility to the Downside Reaches Extreme Levels
#it’s Usually a Sign of a Market Bottom.
#
#This Indicator Takes the ATR and uses a different LookBack Period
#to calculate the Percentile Rank of ATR Which is a Great Way To
#Calculate Volatility
#

declare lower;

#INPUTS
input length = 1; #ATR Length
input length2 = 252; #no. of Bars the PercentileRank uses to Calculate % Values
input sn = no; #Show Normal Histogram? Uncheck = Histogram based on Up/Down Close
input paintBars = yes;
input showLabels = yes;

#LOGIC

#LABELS
AddLabel (showLabels, "ATR Percentile for ThinkorSwim Version 1.0", COLOR.ORANGE);

#//ATR and PercentileRank Calculations
def atr = Average(TrueRange(high, close, low), length);

def percentRankCount = fold i = 1 to length2 + 1 with count = 0
do
  if atr[0] > atr[i] then
    count + 1
  else
    count;

def percentRank = Round( percentRankCount / length2 * 100.0, 0);

def down = close < close[1];
def up = close > close[1];

#//Calculation for Showing Histogram based on Up/Down Close
def pctileRankFinal = if up then percentRank else if down then percentRank * -1 else Double.NaN;

#PLOTS
plot pATRRank = if(sn,percentRank,pctileRankFinal);
pATRRank.SetPaintingStrategy(paintingStrategy = PaintingStrategy.HISTOGRAM);
pATRRank.AssignValueColor(
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else
COLOR.DARK_GRAY
);
pATRRank.SetLineWeight(5);

#COLORBARS
AssignPriceColor(if paintbars then
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else COLOR.DARK_GRAY
else
COLOR.CURRENT
);

#END OF CM_ATR_Percentile for ThinkorSwim Version 1.0
This is a clever concept - thank you. What would we add to this code if the label would actually show the value of the last measurement rather than just the name of the script?
 
This is a clever concept - thank you. What would we add to this code if the label would actually show the value of the last measurement rather than just the name of the script?
Code:
#CM_ATR_Percentile for ThinkorSwim Version 1.0
#
#VERSION
# 2020.01.03 V1.0 @diazlaz - Initial Port
#
#LINK/CREDITS
#https://www.tradingview.com/script/D56mCdlz-CM-ATR-PercentileRank/
#https://www.tradingview.com/u/ChrisMoody/
#
#INSTRUCTION
#CM ATR PercentileRank - Great For Showing Market Bottoms.
#
#When Increased Volatility to the Downside Reaches Extreme Levels
#it’s Usually a Sign of a Market Bottom.
#
#This Indicator Takes the ATR and uses a different LookBack Period
#to calculate the Percentile Rank of ATR Which is a Great Way To
#Calculate Volatility
#

declare lower;

#INPUTS
input length = 1; #ATR Length
input length2 = 252; #no. of Bars the PercentileRank uses to Calculate % Values
input sn = no; #Show Normal Histogram? Uncheck = Histogram based on Up/Down Close
input paintBars = yes;
input showLabels = yes;

#LOGIC

#LABELS
AddLabel (showLabels, "ATR Percentile for ThinkorSwim Version 1.0", COLOR.ORANGE);

#//ATR and PercentileRank Calculations
def atr = Average(TrueRange(high, close, low), length);

def percentRankCount = fold i = 1 to length2 + 1 with count = 0
do
  if atr[0] > atr[i] then
    count + 1
  else
    count;

def percentRank = Round( percentRankCount / length2 * 100.0, 0);

def down = close < close[1];
def up = close > close[1];

#//Calculation for Showing Histogram based on Up/Down Close
def pctileRankFinal = if up then percentRank else if down then percentRank * -1 else Double.NaN;

#PLOTS
plot pATRRank = if(sn,percentRank,pctileRankFinal);
pATRRank.SetPaintingStrategy(paintingStrategy = PaintingStrategy.HISTOGRAM);
pATRRank.AssignValueColor(
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else
COLOR.DARK_GRAY
);
pATRRank.SetLineWeight(5);

#COLORBARS
AssignPriceColor(if paintbars then
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else COLOR.DARK_GRAY
else
COLOR.CURRENT
);

#END OF CM_ATR_Percentile for ThinkorSwim Version 1.0

AddLabel(yes, percentRank,
if percentRank <= 70 then COLOR.GRAY else
if percentRank > 70 and percentRank < 80 then COLOR.ORANGE else
if percentRank >= 80 and percentRank <= 90 then COLOR.RED else
if percentRank >= 90 then COLOR.MAGENTA else
COLOR.DARK_GRAY);
 
Last edited:

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