Drawdown Indicator for ThinkorSwim

evanevans

Active member
I have an obsession with stocks that have gorgeous charts. :) And one of the factors that makes a chart "gorgeous" to me, is that is has a strong trend coupled with low volatility and high performance. Some excellent examples are the daily charts on MSFT and LULU to name a few. Aside from pandemics and giant market pullbacks, those stocks have great resiliency, and when they consolidate, they go sideways, not down.

One of the ways you can visually see how resilient a stock is over time, is by checking it's drawdowns in %.

I searched around the web and forum for a ThinkScript indicator to show drawdown %, and could not find that anyone has created a drawdown % indicator. It's an excellent indication of risk and volatility. When you divide the return over time by the risk over the same time, you get the Sortino ratio, which is a measure of how much reward you are getting for your risk. So, being able to see drawdown in relationship with a stock which is ripping and roaring can help you know if it's a good or poor opportunity. No matter what timeframe you are on, being able to visualize it, can really help you assess if you want to get into the position or not.

Now, in my mind, there are two ways to display this.

One, is how you would see it on portfolio manager's risk charts. A zero line represents days where it's making new highs. Anytime price drops below the all time high, it becomes subtracted and shown in percent from high, in the indicator.

Two, is on the chart itself, as a cloud above any lower price action. Essentially, new highs dictate the new level, and anytime price goes under the recent high, a cloud level remains above it, almost like the stock is going underwater.

If anyone is keen on giving a try at coding this, I'm happy to work with you on it ... in concept. I am not a very skilled programmer. I could probably figure this out, and will try this weekend if no one steps forward, but it'd be great if anyone had the interest and talent to help code it.

Looking forward to seeing how far this goes!
 
Last edited:
Hey @germanburrito thank you so much for this, have been using it the past few weeks and I'm loving it. I am in the process of coding a strat but for the life of me I am not sure how to convert the y-axis into percent, is that possible? Sorry, I'm still learning bit by bit any help would be great.
well basically what the drawdown indicator does it it calculates the last hiighest high and how far the is since that last highest high. is not a close system because it can drop differently for every stock. the percentage of what would be the question because theres not bottom well i guess 0 would be the bottom but stocks dont usually move to zero.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

well basically what the drawdown indicator does it it calculates the last hiighest high and how far the is since that last highest high. is not a close system because it can drop differently for every stock. the percentage of what would be the question because theres not bottom well i guess 0 would be the bottom but stocks dont usually move to zero.
Hm, ok, I think I got a way to measure it by comparing to the previous lowest low or previous highest high, will let you know if I figure something out.
 
@Pensar
you created this "Drawdown Cloud" indicator with/for me awhile back. I have come back to it as it's proving useful for something new. I wondered if you have a little time to try and add a couple of things. I'm happy to send you some $ :)

Code:
#Drawdown Cloud with Label

def hh = if high > hh[1] then high else hh[1];
plot line = hh;
     line.setdefaultcolor(color.gray);
AddCloud(line,high,color.gray,color.gray);

def h = HighestAll(high);
def below = (h - close)/h;
addlabel(1, "% Below High = " + AsPercent(below), color.light_gray);

#end code

That code on an intraday chart looks like so:
9ZJEH23.png


These are the additions I'm curious if you could try:

1. Option to restart the drawing at either of the following: continuous (as it is now [from beginning of entire chart]), from prior day's AH, from premarket, from Market open
NOTE: If this proves hard, to have so many choices, let's just start with each "new day" (so midnight resets)

2. When a new high is made, draw a lower line (cloud) BELOW which is equal to the lowest low between the previous high that was just broke and where the high was broke. Here's an illustration of what that lower line would look like:

Z9s15S8.png


If we can start with doing that much, there are just a few more finesse addons we can do after that, to make it function a little better.

So basically, what we are creating here, is a pivot trade channel. I use this technique daily to make many winning trades. It really works.
 
@Pensar

This is what I've got so far, to make it start from midnight each day. Granted I am not great at coding. And it still isn't resetting the hh each midnight, it's just stopping from drawing it from close of market yesterday until midnight.

Code:
def start = if SecondsFromTime(0000) >= 0 then 1 else 0;
def end = if SecondsTillTime(1600) >= 0 then 1 else 0;

def hh = if high > hh[1] then high else hh[1];
plot line = if start and end then hh else Double.NaN;
     line.setdefaultcolor(color.white);
AddCloud(line,high,color.gray,color.gray);
 
Below is the script I've developed for the drawdown plot. Some of the code and design features are inspired by @germanburrito 's prior post.

This script plots the drawdown from peak in percentage form. There are user inputs for marking a drawdown level and designating if the peak and bottom should be calculated by either OHLC, etc. The default calculation for drawdown is close to close. Labels show the Current Drawdown as well as the Max Drawdown for the selected time frame. This works on all time frames.

TSLA one year chart shows current drawdown of -5.53% and max drawdown of -36.72% measured from close to close.
Yf0cIm5.png


Same chart with drawdowns measured from high to low with a marker at the -37%.
JWx8WFR.png


Code:
#2021-12-01 - v2
#Script by thinkorJim
#Calculates and Plots the % Drawdown from Peak

declare lower;

input MarkLevel = 0;
input peak = close;
input bottom = close;

def hh = if peak > hh[1] then peak else hh[1];

plot zeroline = 0;

plot markline = If(MarkLevel > 0 , MarkLevel * -1, MarkLevel);

plot drwdown = round((bottom - hh) / hh * 100 , 2);

AddCloud(drwdown, zeroline, Color.RED);
drwdown.SetDefaultColor(Color.YELLOW);

AddLabel(1, "Current Drawdown = " + AsPercent(drwdown/100), Color.LIGHT_GRAY);

AddLabel(1, "Max Drawdown = " + AsPercent(lowestall(drwdown/100)), Color.LIGHT_GRAY);
 
Last edited:
I really like having the drawdown plot since the charts scale (i.e. no zero bound). A chart showing a price starting at 50, peaking at 100, then dropping to 75 will visually appear to be in a 50% drawdown since it takes up half the screen, but it is really just a 25% drawdown. Likewise if prices were small then suddenly large, the price fluctuations and drawdowns looks smaller than they really were (e.g. AMZN from 1998 to today).

I revised the code to include a Label displaying the price level of the associated mark level specified by the user. By returning the price level, I could use this information to buy dips, place or trail a stop, and buy or sell a put option at that level.

For instance, lets say I want to dip buy on SPY. It had a max DD of -5.42% over the course of this year and I think it will approach that level again So I set my marker at -5% in order to buy the dip thinking price will reach this level. The price at the marked drawdown level is then displayed as 446.24, which tells me where to set my limit order for the dip buy.
0ScGcxN.png


Code:
#2021-12-03 - v1
#Script by thinkorJim
#Calculates and Plots the % Drawdown from Peak

declare lower;

input MarkLevel = 0;
input peak = close;
input bottom = close;

def MarkPrice = MarkLevel<>0;

def hh = if peak > hh[1] then peak else hh[1];

plot zeroline = 0;

plot markline = If(MarkLevel > 0 , MarkLevel * -1, MarkLevel);

plot drwdown = round((bottom - hh) / hh * 100 , 2);

AddCloud(drwdown, zeroline, Color.RED);
drwdown.SetDefaultColor(Color.YELLOW);

AddLabel(1, "Current Drawdown = " + AsPercent(drwdown/100), Color.LIGHT_GRAY);

AddLabel(1, "Max Drawdown = " + AsPercent(lowestall(drwdown/100)), Color.LIGHT_GRAY);

AddLabel(MarkPrice, "Price at Marked Drawdown = " + round(hh*(1+(markline/100)),2), Color.LIGHT_GRAY);
 
I really like having the drawdown plot since the charts scale (i.e. no zero bound). A chart showing a price starting at 50, peaking at 100, then dropping to 75 will visually appear to be in a 50% drawdown since it takes up half the screen, but it is really just a 25% drawdown. Likewise if prices were small then suddenly large, the price fluctuations and drawdowns looks smaller than they really were (e.g. AMZN from 1998 to today).

I revised the code to include a Label displaying the price level of the associated mark level specified by the user. By returning the price level, I could use this information to buy dips, place or trail a stop, and buy or sell a put option at that level.

For instance, lets say I want to dip buy on SPY. It had a max DD of -5.42% over the course of this year and I think it will approach that level again So I set my marker at -5% in order to buy the dip thinking price will reach this level. The price at the marked drawdown level is then displayed as 446.24, which tells me where to set my limit order for the dip buy.
0ScGcxN.png


Code:
#2021-12-03 - v1
#Script by thinkorJim
#Calculates and Plots the % Drawdown from Peak

declare lower;

input MarkLevel = 0;
input peak = close;
input bottom = close;

def MarkPrice = MarkLevel<>0;

def hh = if peak > hh[1] then peak else hh[1];

plot zeroline = 0;

plot markline = If(MarkLevel > 0 , MarkLevel * -1, MarkLevel);

plot drwdown = round((bottom - hh) / hh * 100 , 2);

AddCloud(drwdown, zeroline, Color.RED);
drwdown.SetDefaultColor(Color.YELLOW);

AddLabel(1, "Current Drawdown = " + AsPercent(drwdown/100), Color.LIGHT_GRAY);

AddLabel(1, "Max Drawdown = " + AsPercent(lowestall(drwdown/100)), Color.LIGHT_GRAY);

AddLabel(MarkPrice, "Price at Marked Drawdown = " + round(hh*(1+(markline/100)),2), Color.LIGHT_GRAY);
Hey great indicator! I copied and pasted the same code but I am not getting the same picture as you. It looks more like the previous indicator above. Am I doing something wrong - help
 
Hey great indicator! I copied and pasted the same code but I am not getting the same picture as you. It looks more like the previous indicator above. Am I doing something wrong - help
Who's previous indicator? If you mean mine, the only difference between this and the previous codes are the labels and the mark line. If you mean that SPY's current drawdown looks more like the TSLA drawdown picture from a prior date, it is due to SPY being in a current and prolonged drawdown. The "drawdown" is defined as the percentage below the prior high. Try running the indicator on the same symbol and time period as I show in the screenshots, which is 1-year daily through Dec 2021, and they will look the same.
 
Who's previous indicator? If you mean mine, the only difference between this and the previous codes are the labels and the mark line. If you mean that SPY's current drawdown looks more like the TSLA drawdown picture from a prior date, it is due to SPY being in a current and prolonged drawdown. The "drawdown" is defined as the percentage below the prior high. Try running the indicator on the same symbol and time period as I show in the screenshots, which is 1-year daily through Dec 2021, and they will look the same.
Thanks for the quick reply! Yes it was the previous one which is yours as well. The second one is not displaying the same amounts on the right side. On the right side the scale is 0 t0 -5. Mine is showing it from a year high I believe with numbers such -60 and so forth. Sorry I hope I'm making sense. Not sure if I need to go in and change something. Is it possible to share the link for the code instead of copying and pasting it?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
223 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