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
Python is insane for finance! In this QS Newsletter (get the code), we are showing how to do algorithmic trading and quantitative finance data manipulations in Python 10X faster using a new library called Polars. Today, you learn:
What is Polars (and how does is make algorithmic trading signals 10X faster)?
Where can I use Polars in my workflow (and how can I screen 100X more stocks with it)
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.
Want exclusive access to our FULL codebase for this Quant Science tutorial plus dozens more? The code is in the QS018 Folder. Join here:
Are you interested in learning Algorithmic Trading with Python? Do you want to learn how to execute trades automatically, how to find edge, backtest trading strategies, analyze risk, then take your winning trades from Paper Account to Production (Live Trading)?
If the answer is Yes, then we have a NEW Free 5-Day Algorithmic Trading.
👉 Click here to join our free 5-Day Algorithmic Trading Course.
According to the polars
documentation:
The polars package for R gives users access to a lightning fast Data Frame library written in Rust. Polars’ embarrassingly parallel execution, cache efficient algorithms and expressive API makes it perfect for efficient data wrangling, data pipelines, snappy APIs, and much more besides. Polars also supports “streaming mode” for out-of-memory operations. This allows users to analyze datasets many times larger than RAM.
Why this is important to quants and algorithmic traders:
Polars is designed for fast data manipulation including grouped aggregations, rolling calculations, and expanding calculations, which are common in time series and financial analysis
Polars is super fast because it's built on top of Rust (a language that is as fast and in some cases faster than C++)
Polars is built for scale (it uses parallel execution, and handles data that is larger than memory)
You don't need anything other than Python to run it (Polars Rust implementation is transportable with a compiler so no extra build tools are required)
But how can we use Polars for algorithmic trading and quant finance?
That's where this tutorial will help.
Ok, let's dive in and see how to use Polars for algo trading signals and quant finance analysis. First, make sure to sign up for our Newsletter to get all of the code you see today.
The code below downloads stock price data for the 25 top holdings from Stanley Druckenmiller's Portfolio (Duquesne Capital Management, LLC) valued at $4.39 Billion Assets Under Management (AUM) as of when this article was produced.
Run this code from the "QS018-polars" Folder to download stock data from the 13F filing:
This produces a Polars data frame. Here's what the data looks like:
Key Points:
This Polars Data Frame is in the "wide" format. We will normally pivot the data into the "long format" to perform data analysis by group.
This looks like Pandas, but it's not. It's Polars. You can tell because the data types are listed along with the data shape. Also there is no index with polars data frames.
Rolling calculations are a staple for algorithmic trading for signal development. Unfortunately, they take a long time to process especially for many time series groups (in finance, each stock symbol is a group).
Fortunately, Polars is extremely fast. In fact, it's between 10X and 3500X faster than Pandas.
Run this code to convert the wide data into long format. We need long format so we can do grouped analysis (i.e. by stock symbol).
We can visualize the Stock Prices Over Time using 1 line of code in polars. Run this code:
Next, run this code to perform 10-day and 50-day moving averages for 25 stocks in a few milliseconds (about 10X faster than Pandas):
We can quickly visualize the trading signals for one of the stocks. Run this code to visualize NVDA:
You can see how fast the rolling averages were performed, and how quickly we can then develop trading signals with Polars.
Next, let's cover a common Quant Finance task...
A common task in Quant Finance is investigating the rolling Sharpe Ratio for tens of thousands of stocks. The rolling sharpe can be used as a screening tool or as a feature in financial machine learning models.
We can do this fast with polars. Run this code to calculate the returns by stock and then the 50-Day rolling Sharpe Ratio by stock:
Next, run this code to visualize the 50-Day rolling Sharpe Ratio for each of the 25 stocks.
There you have it-- An easy way to screen stocks by rolling Sharpe Ratio and add a rolling Sharpe Ratio to your Algorithmic Machine Learning models. With Polars we can scale this to 100,000s of assets.
Polars is insane for finance. We've used Polars to perform fast rolling calculations for algorithmic trading signals and to create scalable rolling sharpe ratios for screening and machine learning trading models. The best part-- We can scale this to 100,000+ stocks.
Are you interested in learning active investing strategies that use algorithms to 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.
Learn with 250+ of us that are learning to apply python to algorithmic trading to grow investments.
Leo was up 11.5% in just 13 trading days.
Alex was waiting 9 years for a course like this:
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?
👉 Join 10,700+ future Quant Scientists on our Python for Algorithmic Trading Course Waitlist: https://learn.quantscience.io/python-algorithmic-trading-course-waitlist
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