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

Pytimetk: 3 New Polars Functions

Technical Indicators with Polars: 20X Faster Than Pandas

February 03, 20245 min read

In this QS Newsletter (get the code), we are sharing some development updates on Pytimetk, a new Python library for time series analysis built on top of Pandas and Polars. Our objective today is to see how to share how you can create financial features (factors) blazingly fast with the polars engine. Today, you learn:

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.

Pytimetk: A new Python package for time series and financial analysis

Pytimetk

And here's what we are covering today: 3 New financial functions for 20X speed boost vs Pandas.

3 New Functions for Fast and Scalable Financial Analysis

Join the Quant Scientist Newsletter (and Get the Code)

Want exclusive access to our FULL codebase for this Quant Science tutorial plus dozens more? The code is in the QS0012 Folder. Join here:

Quant Scientist Newsletter - Get the Code

Interested in Algo Trading? Quick favor.

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 I have a Quick favor to ask - We're preparing for the next cohort of our new python for algorithmic trading course. If you can spare 60 seconds, we'd love to hear what would help make you a better trader from our course. Click here to enter your 60-second survey.

And you can join the waitlist for our next Cohort.

What is Pytimetk?

pytimetk

Pytimetk is a time series analysis package that makes time series easier, faster, and more enjoyable in Python. Full disclosure - Quant Science's co-founder (Matt Dancho) is the author. And he's been hard at work adding new Finance tools inside of Pytimetk.

Before I discuss that, let's talk speed. One of the novel features of Pytimetk is that it integrates a Polars backend (engine) for many time series and finance functions. Polars is between 3X and 1000X faster than Pandas for many tasks (see our speed comparisons here).

On rolling operations (very common in finance), our Polars engine is on average 10X faster than the Pandas engine.

So if you care about performance, then Pytimetk is your friend. (It's also easy to use, which makes it less painful to do financial and time series analysis)

Python Tutorial: New Pytimetk Finance Functions

Let's check out these 3 new functions today, shall we?

New Finance Functions in Pytimetk

The goal with our tutorial today is to kick the tires on 3 new finance functions that are inside the development version of Pytimetk (Version 0.3.0.9000). Get the code: It's in the QS012 folder.

Before you begin, make sure to install the development version of Pytimetk:

pip install git+https://github.com/business-science/pytimetk.git

Step 1: Load Libraries and Get the Stock Data

The first step in our analysis is to load the following libraries and setup our analysis parameters. Run this code:

Python Libraries

Get the code: It's in the QS012 folder.

The code produces the following data:

data

Step 2: MACD with Polars Backend

Next, we will use Pytimetk's augment_macd()Function to generate MACD features as new columns in the data frame. We will use the polars engine to get a speedup. Run this code:

Train Test Split

Get the code: It's in the QS012 folder.

The resulting data frame now has 3 new MACD features added:

Macd data

Step 3: Bollinger Bands with Polars Backend

Just like MACD, we can make Bollinger Bands with the augment_bbands() function. Note that now I'm adding multiple periods [20, 40, 60] to make multiple combinations of Bollinger Band Features. This adjust the rolling windows parameter used to make the bands. Run this code:

BBANDS

Get the code: It's in the QS012 folder.

We now have 9 new features:

BBANDS FEATURES

Get the code: It's in the QS012 folder.

Step 4: Chaining Feature Operations

Now that you have the hang of it, you can begin chaining features operations to quickly add many finance and time series features. Run this code:

Chaining Feature Operations

Get the code: It's in the QS012 folder.

Now you have 40+ features for running machine learning algorithms on your finance data:

Chaining Feature Operations Data

Get the code: It's in the QS012 folder.

Conclusion: Python is getting even better for Stock Analysis

Pytimetk is a new library. As of this writing, it's still under active development, so many of these functions are being added. We will keep you updated on progress. And we look forward to teaching them to you in our QS Algo Trading program.

Ready to take your investment game to the next level? Embracing Python for algorithmic trading can be a game-changer for your portfolio. If you're new to Python or want to sharpen your skills for financial analysis, our upcoming Python for Algorithmic Trading Course is the perfect opportunity. See you in our Python Algo-Trading course!

Are you feeling lost when trying to learn Algorithmic Trading?

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.

And all of this increases the likelihood you will fail (not to mention lose money in the process). Protect your future.

Python for Algorithmic Trading Course

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

button course waitlist

investingstockspythonalgorithmic tradingsoftwarepolars
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

Cohort 6 launches Wednesday, October 2nd at 10AM EST