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
 

CashPrince

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

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
@princesly It should work with intraday timeframe (5m, 15m, etc). You can also adjust the ATR length.
 

CashPrince

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

ext99k

Member
@BenTen if possible, can you please consider updating the code to support the colored bars on mobile? right now they're either green or red indicating up/down moves, would really appreciate it! thanks.
 

droskill

New member
Hey all - just found this awesome community. I'm a new thinkorswim user coming from Amibroker. One indicator or calculation I'm missing (or having found yet in ToS) is PercentRank. It's built into a variety of platforms (TradingView, Amibroker, Tradestation, etc.). Amibroker describes it as such:

Code:
PercentRank( array, range )

INPUTS:
array - input data
range - lookback range
Returns percent rank (0...100) of the current element of the array within all elements over the specified range.

A value of 100 indicates that the current element of the array is the highest for the given lookback range, while a value of 0 indicates that the current value is the lowest for the given lookback range.

It is equivalent (but 2x faster) to:

function PercentRank2( Data, Periods)
{
   Count = 0;
  for ( i = 1; i <= Periods ; i++ )
   {
   Count += Data > Ref( Data, -i );
   }
  return 100 * Count / Periods;
}

Does this exist in ToS and I just haven't found it or would it require custom coding?

Any help greatly appreciated!
 

Clata54

New member
VIP
Hey all - just found this awesome community. I'm a new thinkorswim user coming from Amibroker. One indicator or calculation I'm missing (or having found yet in ToS) is PercentRank. It's built into a variety of platforms (TradingView, Amibroker, Tradestation, etc.). Amibroker describes it as such:

Code:
PercentRank( array, range )

INPUTS:
array - input data
range - lookback range
Returns percent rank (0...100) of the current element of the array within all elements over the specified range.

A value of 100 indicates that the current element of the array is the highest for the given lookback range, while a value of 0 indicates that the current value is the lowest for the given lookback range.

It is equivalent (but 2x faster) to:

function PercentRank2( Data, Periods)
{
   Count = 0;
  for ( i = 1; i <= Periods ; i++ )
   {
   Count += Data > Ref( Data, -i );
   }
  return 100 * Count / Periods;
}

Does this exist in ToS and I just haven't found it or would it require custom coding?

Any help greatly appreciated!

Droskill, were you ever able to figure this out? It is exactly what I am looking for as well for ToS.
 

Clata54

New member
VIP
I love this ATR Percentile Rank Indicator and would love to do something similar for both OBV and Implied Volatility. I tried tweaking the script and referencing those studies, but wasn't quite sure how to define OBV for this. Any help is greatly appreciated!
 

Clata54

New member
VIP
I think I finally figured it out!! Here is what I have so far for those interested. This Indicator Takes OnBalanceVolume and uses a different LookBack Period to calculate the Percentile Rank of OBV. Feel free to adjust, correct, or improve. I am still fairly new at ThinkScript.

Code:
#OBV_Percentile for ThinkorSwim Version 1.0
#
#VERSION
# 2021.02.17 V1.0
#This Indicator Takes OnBalanceVolume and uses a different LookBack Period
#to calculate the Percentile Rank of OBV
#

declare lower;

#INPUTS
input price = close;
input averageType = AverageType.WILDERS;

#LOGIC

#//OBV PercentileRank Calculations

def vol = OnBalanceVolume();
input TimePeriod = 252;

def data = if !IsNaN(vol) then vol else vol[-1];
def hi = Highest(data, TimePeriod);
def lo = Lowest(data, TimePeriod);
plot Percentile = (data - lo) / (hi - lo) * 100;
def lowend = Percentile < 25;
def highend = Percentile > 50;

input over_Bought1 = 98;
input over_Sold1 = 2;

Percentile.DefineColor("HighVolume", Color.RED);
Percentile.DefineColor("Normal", Color.YELLOW);
Percentile.DefineColor("LowVolume", Color.GREEN);
Percentile.AssignValueColor(if Percentile > over_Bought1 then Percentile.Color("HighVolume") else if Percentile < over_Sold1 then Percentile.Color("LowVolume") else Percentile.Color("Normal"));

#END OF OBV_Percentile for ThinkorSwim Version 1.0
 

droskill

New member
Droskill, were you ever able to figure this out? It is exactly what I am looking for as well for ToS.
Well I finally did - here's the code:

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:

droskill

New member
That pretty much is the full code - here's an example of it using a Simple Moving Average:

Code:
declare lower;

input length = 50;

def rb = movingaverage(length,close);

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

def percentRank = percentRankCount / length * 100;

plot PRankSimpleAvg = percentRank;
 

droskill

New member
@droskill it shouldn't show anything when I paste that code, thanks
Hmmm....ok here's a simpler example that does work - not sure why that isn't working. This ranks closing prices over the 100 days.

Code:
declare lower;

input length = 100;

def rc = close;

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

def percentRank = percentRankCount / length * 100;

plot PRankClose = percentRank;
 

Neel2020

New member
VIP
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?
 

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime
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:

TapthatAsk

Member
VIP
Code:
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)
@MerryDay Hola I was interested in seeing what this showed on a chart, added this label portion to the original script but kept getting the invalid statement, do you mind attaching the script with your updated version to show labels?
 

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.
Top