Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

Responsible algorithmic trading with downside deviation

Responsible algorithmic trading with downside deviation

October 26, 20244 min read

Matt and I talk a lot about "responsible" algorithmic trading.

What do we mean by that?

Well our goal is to build profitable algorithmic trading strategies.

And keep the profits!

That's why we focus on risk so much. It's hard enough to build profitable strategies, but giving away the profits?

Ouch.

In today's issue of the QS Newsletter (get the code), we are going to build a simple risk metric we use all the time. It's called downside deviation.

What You’ll Learn:

  1. Download historical stock price data and compute the mean return

  2. Use NumPy to build a function to compute downside deviation

  3. Compare it to standard deviation (which is normally used)

BONUS: Get the Python Code for EVERYTHING you see in this post

Disclaimer:

The information and educational material provided by Quant Science, LLC are for educational purposes only and should not be considered as financial advice or recommendations to purchase, hold, or sell any securities or other financial instruments. Before you proceed, please review our full disclaimer here.

Quant Science

Join the Quant Scientist Newsletter (and Get the Code)

Want exclusive access to our FULL codebase for this Quant Science tutorial plus dozens more?

Join thousands of aspiring Python quants here 👉

NEW: Free 5-Day Algorithmic Trading Course

5 Day Algorithmic Trading Course

Since you're here, you probably want to learn how to get started developing (profitable) algorithmic trading strategies and reinvest those profits.

Here are the steps:

  1. Find edge

  2. Analyze risk

  3. Backtest trading strategies

  4. Execute trades automatically

Easy right? Well, not exactly... Avoid the 5 biggest mistakes beginners make with our free, 5-day email course:

Click here to join our free 5-Day Algorithmic Trading Course 👉

Now on to the show...

Responsible algorithmic trading with downside deviation

Portfolio risk is the potential for financial loss and uncertainty about its extent. Downside deviation is a common measure of financial risk that measures the volatility of negative returns.

This measure gives a more accurate picture of the type of risk traders care about:

The risk of losing money.

Since volatility to the upside is not usually a concern, downside risk is used by traders to gauge the risks that lead to portfolio drawdown—a key worry.

Imports and set up

All we need is NumPy and yFinance.

Quant Science

Compute downside deviation

The calculation for downside deviation is straightforward.

Instead of using pandas methods (returns is a pandas Series), you’ll see how to use NumPy to concisely make the calculation.

Quant Science

The function first creates an empty array the same size as the returns input (less one to remove the NaNs).

Then we use the clip method to grab all the returns between negative infinity and 0.

From there, we square the returns, take the mean value, apply the square root, then annualize by multiplying by the square root of 252.

Let's compare downside deviation with it's cousin, standard deviation.

Quant Science

When comparing the downside deviation to the standard deviation of returns, it will be different. In the case of this example (at the time of writing) it’s 33% lower!

That’s because AAPL has been rallying over the time period. If you repeat the analysis for a trading portfolio or asset that has not been on a steady incline, your results will be different.

Congratulations!

You just took the first step in responsible algorithmic trading by learning a simple but effective risk metric—downside deviation.

But, there's more to learn in algorithmic trading:

  • Backtesting your portfolio construction algorithm to make sure the strategy will work in the future

  • Executing the trades automatically

  • Monthly rebalancing

  • Tracking your actual Profit and Loss

  • Incorporating Trading Fees

Are you interested in learning algorithmic trading strategies that maximize returns responsibly, help you manage risk, and grow your investments?

We implement 3 core trading strategies including portfolio, momentum, and spread trades that have worked in our favor in the past and continue to produce results for our students.

Join 400+ of us that are learning to apply python to algorithmic trading to grow investments.

Leo was up 11.5% in just 13 trading days.

Leo up 13pct

Alex was waiting 9 years for a course like this:

testimonial

Ready to make Algorithmic Trading Strategies that actually work?

There's nothing worse than going at this alone--

Learning Python is tough.

❌ Learning Trading is tough.

Learning Math & Stats is tough.

It's no wonder why it's easy to feel lost, make bad decisions, and lose money.

Want help?

Python for Algorithmic Trading Course

👉 Join 10,700+ future Quant Scientists on our Python for Algorithmic Trading Course Waitlist: https://learn.quantscience.io/python-algorithmic-trading-course-waitlist

button course waitlist

investingstockspythonalgorithmic tradingsoftwareffn
Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Matt Dancho

Matt is a Data Science expert with over 18 years working in business and 10+ years as a Data Scientist, Consultant, and Trainer. Matt has built Business Science, a successful educational platform with similar goals to Quant Science, but focused on developing Data Scientists in business, marketing, and finance disciplines.

Back to Blog

Start Your Journey To Becoming A Quant Today!

Join the Quant Scientist Newsletter

Gain access to exclusive tools that Wall Street's Elite don't want you to have. Don't miss the next issue...

Join 11,500+ Quant Scientists learning one article at a time

Join 11,500+ Quant Scientists learning one article at a time

© 2024 Quant Science - All Rights Reserved

Next Cohort Launch: Wednesday, January 15th at 10AM EST