Introduction

bta-lib stands for "backtrader ta-lib" or backtrader technical analysis lib. It is a Python implementation of standard technical analysis indicators and with it the framework to quickly prototype and develop new custom indicators.

YATALIB

Yet Another Technical Analysis LIBrary. That could well have been the name, and the simple reason to create this library, having "yet another". But neither ... nor.

The reasons:

  • Having a library which is easy to use and re-use

  • Offering a correct implementation of indicators

  • Achieving a readable implementation of indicators

  • Making it possible to quickly and easily develop new indicators

Some research was conducted on what the current technical analysis libraries written in Python and populating GitHub do actually offer and seeing broken implementations of the EMA, the RSI creating the library as a side project seemed the way to go.

See: I3 Indicators

Design

A first design was attempted by subclassing pandas structures (i.e.: DataFrame and Series) and it did actually work. But two things were clear:

  • pandas structures are not really meant to be subclssed

    Not only because of the development done here, but because the pandas documentation also states, that even if some subclassing support has been implemented, there are better alternatives, which are recommended over subclassing.

  • The amount of overhead needed to overcome subclassing pandas threatened being able to reach the easy-to-use status of the library (as an end user calculating indicators or as a developer of custom indicators)

The final chosen path was mixing composition and wrapping, to manage pandas.Series in the background and still be able to use almost directly and transparently the API provided by pandas.Series for the calculations.

Using the pandas.Series API is only usually clearly visible when developing basic indicator, because many indicators do simply rely on using previous indicators.

For example:

  • A DEMA has this formula: 2 * EMA(data, period) - EMA(EMA(data, period))

In that formula it is not evident where the pandas.Series API may, but it is:

  • The * multiplication is vectorized and uses the API
  • The same applies to -

Obviously the EMA, a more basic indicator, has previously been defined and implemented with more visible pandas.Series API operations.

With btalib implementing the DEMA can actually reflect the formula. For example:

from btalib import ema

# load your data into a dataframe named df

dema = 2.0 * ema(df, period=30) - ema(ema(df, period=30), period=30)

It is obvious that calculating the exponential moving average twice is not overly efficient. This would make more sense and can be better read

ema1 = ema(df, period=30)
dema = 2.0 * ema1 - ema(ema1, period=30)

Conclusion

Enjoy the library!