Building a crypto application using Polars (part 2): Creating technical indicators to predict future price movements

Romano Vacca
5 min readOct 3, 2022

--

In the second part of the series, we will create technical indicators with the data that we are able to retrieve with the code from the first post.

With these indicators, we will identify coins that are starting to break out of a certain range, meaning that a strong rise or decline in price is about to happen. The end result of the code of this article, is a list of names of coins and on which timeframe (e.g. 1 hour or 1 day) the breakout is happening. Also, a comparison will be made between the speed of the code in Polars and Pandas.

What we will be doing

The following indicators will be created in this post:

  • Average True Range
  • Keltner Channels
  • Bollinger Bands
  • TTM Squeeze

The first three indicators will be used to create the fourth one. Using the TTM Squeeze indicator on our data, we will get a list of coins that have just started to breakout of a certain price range for a specific timeframe, meaning that they are poised to strongly rise or decline in value.

These indicators will be implemented in both Polars and Pandas. However, the code in the post only shows the polars code, while the pandas code can be found in the repository.

Creating the indicators

In order to create these indicators, an indicator base class is created, such that the different indicators are forced to do things in the same way. This is a useful method to make sure that different functions behave the same way and you know what to expect from it.

By using the abstract class “Indicator”, every class that inherits from it, must implement the abstract methods from “Indicator”, otherwise an error will arise. The first indicator we create is Average True Range (ATR). This is a volatility based indicator, where a coin having a high ATR, experiences a higher volatility at that moment and vice versa.

Average True Range

The default timeframe used for ATR is 20, meaning it will take into account 20 datapoints from the given interval. Next to that, we have the mandatory functions we defined in the base class “Indicator”.

We can use the indicators by calling the “run” function, which calculates the ATR. This indicator is required to calculate the next one.

Keltner Channels

Keltner Channels is a volatility-based indicator as well, and is able to provide information about which way the trend of a stock/coin will go.

The upper and lower bands are typically set two times the average true range (ATR) above and below the EMA, although the multiplier can also be adjusted based on personal preference. source: investopedia

In the “run” function, we use the previously created ATR, to calculate the upper and lower boundaries of the Keltner Channels. When the price of an asset touches the upper channels, that is considered “bullish”, meaning the future is looking bright and it is likely to rise more. When it reaches the lower channels, it is considered “bearish”, and likely to decline more.

Bollinger Bands

Bollinger Bands® give traders an idea of where the market is moving based on prices. It involves the use of three bands — one for the upper level, another for the lower level, and the third for the moving average. source: investopedia

Just like the Keltner Channels, the Bollinger Bands have upper and lower levels, called “bands”.

TTM Squeeze

Combining these indicators, we can create one that we will use to predict future price movements: TTM Squeeze. This indicator is very popular and actually the #1 indicator on Tradingview, which is an online analysis platform for traders, which shows all different kinds of stock and crypto charts. It’s free to use up to 3 indicators.

The TTM Squeeze indicator measures both volatility and momentum to spot trading opportunities based on volatility changes in a security. The volatility component of the indicator signals potential breakouts after periods of low volatility. The momentum histogram indicates the likely direction of the breakout and can help to determine exit points. source: investopedia

The output of this indicator consists of two parts:

  • 0 or 1 for if a stock/coin is in a squeeze or breaking out (Implemented in this post)
  • A value that indicates the direction of the stock/coin and the intensity of that movement. (Will be created in the next post)

A squeeze means that the price movement of a coin has been consolidated in a tight range, e.g between 1.00 and 1.03, in the recent past. Breaking out means that after being in the same range for a while, the price movement is escaping out of the tight range, either positively or negatively.

When invoking the “run” method of the TTMSqueeze, the following happens:

  • Every file in the given directory is read
  • The data is transformed to the given interval
  • The ATR, Bollinger Bands, and Keltner Channels are calculated
  • A calculation to determine if a squeeze or breakout is starting

TTM Squeeze output

The following image shows a sample of the output

An overview of coins that are breaking out

The results could be interpreted as the following: PLA is breaking out when looking at the 1 hour interval, with the data fetched until the 27th of September 2022.

Notes: the datetime in the image above is when the code is executed, not from when the coin is actually breaking out. Next to that, currently we only check if a breakout is starting at this specific moment, not if it is already is in the middle of a breakout.

Performance

Polars duration: 6.663743019104004 seconds

Pandas duration: 48.86432099342346 seconds

Some context on the data used for this post:

  • Data from 28–03–2020 until 27–09–2022
  • Number of rows per file: 53000
  • Size per file: 7.1 MB
  • Intervals used: [“1h”, “2h”, “4h”, “8h”, “12h”, “1d”, “2d”, “3d”, “1w”]
  • 325 XXX/USDT pairs (total 2 GB data)

Code can be found here.

The benchmark above shows that the Polars version is +- 7 times (!!!) faster than the Pandas implementation.

Final remarks

In this post we created the first indicators and and started to see some of the advantages of using Polars. In the next post, we will complete the TTM Squeeze indicator and also implement the momentum component of the indicator, so we have more information on what direction the breakout is, and how strong that breakout is. Next to that, we will start creating trading strategies on top of the indicators, to see if we can optimise the predictions of future price movements.

--

--