One of the most important things you can do in order to maximize your probability of being a profitable trader is to minimize the time it takes to research, test, and implement trading strategies. Due to the fact that most trading ideas end up being worthless, it is of utmost importance to quickly discard them and move on to the next one.

This leads to the fact that it is better to, whenever possible, avoid creating your own modules and reinvent the wheel for every single trivial aspect of your pipeline. Instead, it’s best to make use of the rich existing ecosystem of extensively used python libraries that focus on different aspects of trading.

In this article, I’ll be covering the most relevant and interesting Python libraries for trading. I’ll list libraries that will help you in getting data, doing backtest, calculating technical indicators, and even interfacing with brokers.

What follows is a summary of the most relevant python libraries related to trading.

LibraryDescriptionFree/PaidAdvantagesDisadvantagesLink
yfinanceprice dataFree -well maintained
 -multiple datasets available (prices, fundamentals, etc)
 – Data might be unreliable
 – Unofficial library
Link
python-binancecryptocurrency tradingFree – Historical data (bars, trades, quotes)
 – Live quotes and trades via WebSocket
 – Unofficial libraryLink
finnhub-pythonprice and alternative dataBoth Free and Paid – Reliable historical prices
 – Multiple alternative datasets
 – Officially maintained by the vendor
 – Most interesting endpoints behind a paywallLink
pandas-tatechnical indicatorsFree – Large number of indicators available – Slower than ta-libLink
ta-libtechnical indicatorsFree – Fastest library available (backend in C) – Difficult installationLink
backtesting.pybacktesting frameworkFree – Intuitive event-driven approach
 – Actively maintained
 – Slower than vectorized alternatives
 – Limited to single asset strategies
Link
vectorbtbacktesting frameworkBoth Free and Paid – Easy to deploy to live-trading
 – Fast execution times
 – New features require a subscription
 – Opinionated syntax
Link

yfinance

The best free library for getting historical prices

Yahoo! Finance is, by all means, the most popular source of free data, and the python community has built an unofficial library that interfaces with the API used by the website.

Yahoo Finance allows getting 1-minute bars for the last 7 days, and daily bars for the entire history of a stock. In addition to this, the library has access to a handful of endpoints that are also very interesting, such as balance sheet data, option chain data, and basic information about a given company.

Although it is possible to access live data from Yahoo Finance from the most popular exchanges, such as Nasdaq and the New York Stock Exchange, it is by no means recommended to use this data feed for live trading with real money.

Advantages

  • Free source of data
  • Provides access to data for stocks, options, cryptocurrencies, balance sheets, and basic company information.
  • Popular and well-maintained library

Disadvantages

  • Unofficial library not developed by the data provider
  • Not reliable for live trading

You can install the library via pip using the following command:

pip install yfinance

You can read the documentation here

python-binance

Being the most popular centralized cryptocurrency exchange makes Binance the definitive source of data when it comes to cryptocurrencies. Despite the fact that the library is an unofficial wrapper of Binance’s API, it is well maintained and reliable.

In addition to providing access to historical data, you can also connect to Binance’s WebSockets and get real-time quotes and trades.

Since this library interfaces with an exchange, it also allows to place orders and get important account information, such as current holdings and previous trades.

Unlike stock prices, where most data vendors provide the NBBO (National Best Bid and Offer), cryptocurrency price data depends on the exchange.

Despite arbitrage algorithms efficiently removing most of the pricing differences between venues, discrepancies can arise from time to time. If all you need is daily data, this distinction is only a technicality, but a very important one if you’re testing intraday strategies.

Considering the volume of transactions on this exchange, it should be used as the default source of data. Having said that, you should definitely choose the exchange you use for trading as your source of data.

Advantages

  • Access to historical prices
  • Access to historical trades and quotes
  • Real-time quotes and trades via WebSocket connection
  • Allows creating orders and getting account info
  • Bot the library and the data are reliable

Disadvantages

  • Unofficial wrapper of the Binance API

You can install the library via pip using the following command:

pip install python-binance

You can read the documentation here

finnhub-python

Finnhub is an excellent source for free and reliable candlestick data, both for cryptocurrencies and stocks. The Finnhub-python library makes it very easy and convenient to access the large repertoire of financial information that Finnhub provides. These are just a few of the data points that are accessible through the library:

  • Stock fundamentals: market news, press releases, news sentiment, basic financial, insider transactions, SEC filings, and Dividends.
  • Stock estimates: price targets, revenue estimates, ESP estimates, EPS surprises, Earnings Calendar.
  • Stock prices: quotes, candles, trades, historical national best bid and offer.
  • ETFs & Indices: profile, holdings, sector, among others.
  • Bond profiles and prices
  • Technical Analysis: support/resistance, technical indicators, aggregate indicators.
  • Alternative data: social sentiment, patent news, senate lobbying, VISA applications, supply chain.

Although most of these datasets require a paid subscription, there are definitely enough free ones that make it worthwhile to create a free account. You can check all endpoints and whether they are free or paid here

Advantages

  • Access to reliable historical prices
  • Multiple datasets of relevant alternative data
  • Easy and convenient to use
  • Officially maintained by the data vendor

Disadvantages

  • Most of the endpoints require a paid subscription

You can install the library via pip using the following command:

pip install finnhub-python

You can read the documentation here

pandas-ta

The most convenient library for technical analysis

When it comes to calculating technical indicators, pandas-ta is the library that you should be using. Pandas-ta includes all of the most popular technical indicators, and most importantly, it is extremely easy to install, especially when compared to ta-lib.

Providing an exhaustive list of all the indicators covered by the library would not be of much value to the reader, but in order to give an idea, there are more than 21 technical indicators related just to moving averages.

According to a handful of users, pandas-ta is supposed to be slower than ta-lib when it comes to processing. Having said that, this library allows to interface to ta-lib and benefit from its processing speed (subject to ta-lib being also installed).

Advantages

  • Large repertoire of technical indicators available
  • Easy installation
  • Popular library with many contributors
  • The most convenient library for technical analysis
  • Allows to use ta-lib as the backend for calculations (for increasing execution speed)

Disadvantages

  • Slower than ta-lib

You can install the library via pip using the following command:

pip install pandas_ta

You can read the documentation here

ta-lib

The most popular library for technical indicators

There is no faster alternative to ta-lib for calculating technical indicators. This is due to the fact that this library is none other than a wrapper around a module written in the C programming language, making it computationally efficient.

Additionally, due to the fact that this was previously the only reliable library available, it is still the most widely used library for calculating technical indicators.

Having said that, installing ta-lib is difficult and requires some trial and error, making it very inconvenient to use. Unless you specifically require this library for some reason, I would choose to use its main alternative: pandas-ta.

The following video I recorded shows how to install this library and all of its dependencies:

Advantages

  • Most popular python library for technical indicators
  • Uses C programming language in the backend
  • Faster than most other free alternatives available

Disadvantages

  • Inconvenient installation

Backtesting.py

The most convenient backtesting engine

Backtesting.py is by far the most intuitive and easy-to-get started backtesting library available. This, coupled with the fact that it is very well documented, allows programmers to backtest their first strategy in just a matter of minutes.

Being an event-driven backtester allows this library to incorporate complex recursive logic that would be impossible to implement in a vectorized backtest.

The ease of use of this library comes with two disadvantages, that could become relevant depending on the type of strategy being tested.

First, the library does not allow to backtest portfolio, since it only allows for testing a single asset at once. Although there are workarounds, like expressing the price of one asset in terms of another in order to backtest a statistical arbitrage trading strategy, you won’t be able to include additional assets to the test.

Moreover, event-driven backtesters are slower than their vectorized counterparts. Backtesting.py is not a suitable library if you want to backtest thousands of iterations in a short period of time (VectorBT should be used instead).

Advantages

  • Actively maintained
  • Intuitive Event-Driven approach
  • Allows for very detailed and realistic features

Disadvantages

  • Does not allow deploying to live trading
  • Slower than vectorized alternatives
  • Does not allow for backtesting multiple assets simultaneously

If you want to get started using this library, check out the following video that I recorded!

If you want to research further on backtesting engines, check out the article that I wrote on this exact topic.

VectorBT

The fastest backtester available

Despite being a relatively new backtesting library, VectorBT has become one of the most widely used libraries for such endeavors.

As a consequence of being a vectorized backtester, it is extremely efficient, and outperforms all other freely available backtesters in any possible test and metric when it comes to execution speed.

On the other hand, one of the main disadvantages of this library is the fact that it far less intuitive than event-driven backtesters such as Backtesting.py.

Unlike other vector-based backtesting engines, VectorBT allows using recursive features such as trailing stop losses, among others. 

It is important to note that, although VectorBT is still being actively maintained by its developers, new features are only added to the newest releases of the PRO version.

Advantages

  • Fastest backtester available for Python
  • Actively maintained
  • Allows for very detailed and realistic features
  • Easy to deploy to live-trading

Disadvantages

  • Opinionated library
  • Only the PRO version incorporates new features
  • Unfriendly learning-curve

If you want to research further on backtesting engines, check out the article that I wrote on this exact topic.

alpaca-trade-api

Best library for live trading on Alpaca (broker)

As the name suggests, this library allows interfacing with Alpaca, a commission-free stocks and cryptocurrencies broker. This broker refers to itself as a “Developer-First API for Stocks and Crypto”, so it comes as no surprise that its library makes it incredibly easy to create complex algorithms for both paper and live trading.

In addition to being able to submit orders and get relevant account information, alpaca-trade-api also has endpoints to get historical bars, historical trades, live quotes, and live trades.

This library also allows developers to connect to Alpaca’s WebSockets in order to get the live stream of quotes and trades of a given asset. This is especially useful for intraday trading strategies, where updated pricing data is required.

Although it offers a limited free data plan, it will only be useful to test a tinker with ideas. The free subscription provides access to data from IEX (Investors Exchange), which only accounts for 2.4% of the total US market volume. As a consequence, the free plan is, by most definitions, suboptimal for trading strategies where optimal order execution based on live quotes is required.

Advantages

  • Officially maintained by the broker itself
  • Excellent documentation
  • Active community plenty
  • Plenty of tutorials and resources online
  • Easiest option for live-trading

Disadvantages

  • Free subscription only allows accessing data from IEX

Check out the following video and article that I recorded for showing how to live trade a strategy using Alpaca!

CCXT

The best exchange-agnostic library for cryptocurrency trading

Unlike other trading libraries listed on this article, CCXT has the major advantage of being able to interface with all major cryptocurrency exchanges.

Its major advantage is the fact that strategy creation and the chosen venue for trading are decoupled, allowing developers to switch between exchanges seamlessly. If an exchange suddenly provides more competitive fees, you will just have to set the API credentials of said venue, instantiate a new connection and continue trading with the same trading strategy.

It does have a major disadvantage, which cannot be understated: it only provides access to the REST APIs of the exchanges and not their WebSockets. As a consequence, this library is rendered useless for mid to high-frequency trading strategies. Websocket connections are available through their PRO version, which comes at the cost of a monthly subscription fee.

Additionally, if you’re certain about the exchange that you will use for live trading, you should choose to use their own library (if they have one). This is because all changes made on the API will be preemptively updated on the official library, whereas on CCXT it will be updated reactively (leading to downtimes).

Advantages

  • Allows to seamlessly switch between more than 100 cryptocurrency exchanges
  • Well documented
  • The most popular exchange agnostic trading library

Disadvantages

  • Only provides access to REST API endpoints
  • WebSocket connections require a PRO subscription

OpenBB Terminal

Natural Language Processing for AAPL Sentiment Analysis

OpenBB terminal is a project that aims at creating a free open source library that resembles a Bloomberg Terminal. It was initially called GameStonks Terminal because the project started during the GameStop short squeeze, but rebranded to its current name afterward.

OpenBB is by far one of the most valuable and active libraries currently being developed for the trading community.

In addition to incorporating features commonly used in econometrics, machine learning, and data science applied to financial markets, it is also an excellent source of data.

The terminal includes connections to multiple data vendors, both free and paid ones. You can check them all out in the documentation. Needless to say, it will be worth your time to install this library on your computer and check out its countless functionalities.

Advantages

  • Access to lots of alternative data sources
  • Access to asset pricing data
  • Excellent charting capabilities
  • Easy to use commands resembling a Bloomberg Terminal
  • Powerful data science, econometrics, and machine learning tools for exploratory analysis

Disadvantages

  • Documentation could be improved
  • Synthax is sometimes inconsistent across modules

Other Libraries

The objective of this article was not to provide an exhaustive list of trading-related python libraries, but a curated one. Having said that, there a plenty of resources that, due to being already well known or due to having better alternatives, were not individually mentioned. In the remaining section, I will provide a larger but less detailed of other libraries available that you should also check out if you haven’t already.

For completeness, I also included the previously mentioned libraries in these categories.

Other Data Science and Machine Learning Python libraries for trading

  • TensorFlow: free open-source machine learning library maintained by Google. Link
  • Keras: library that allows to easily interface with TensorFlow. Link
  • PyTorch: alternative to TensorFlow that has a more “Pythonic” OOP design in mind. Link
  • Sklearn: machine learning library that excels at quickly prototyping multiple most machine learning models commonly used in the industry. Link
  • Statsmodels: excellent library for statistical computations, such as descriptive statistics and inference analysis. Link
  • Numpy: library that allows to perform fast high-level computations on top of multi-dimensional arrays. Link
  • Pandas: data manipulations and analysis library built on top of Numpy. Link

Other Charting and Reporting Python Libraries for trading

  • Matplotlib: most popular charting library. Link
  • Seaborn: charting library aimed at statistical data visualization. Build on top of Matplotlib. Link
  • Bokeh: charting library with reporting capabilities for web browsers. Link
  • Streamlit: library for creating complex and interactive web dashboards. Link
  • Dash: allows the creation of interactive web dashboards. Alternative to Streamlit. Link

Relevant Backtesting Python Libraries for Trading

  • Backtesting.py: intuitive and easy event-driven backtesting library (best event-driven backtester). Link
  • VectorBT: vectorized backtesting library excellent for minimizing execution time (best vectorized backtester). Link
  • AlephNull: event-driven backtesting library that is no longer maintained. Link
  • Backtrader: backtesting library that allows for multi-asset strategies. Link
  • Bt: excellent backtesting library for testing portfolio strategies. Link
  • Pybacktest: a vector-based library that is no longer maintained nor developed. Link
  • PyAlgoTrade: unmaintained and limited backtesting library. Link
  • Zipline: popular and easy-to-use backtesting library with plenty of online resources and tutorials. Link

Relevant Python Libraries for Interfacing with Brokers

  • Alpaca-trade-api: official library for interfacing with Alpaca. Link
  • Ib_insync: unofficial library for interfacing with Interactive Brokers via IBGateway or TWS. Link
  • Td-ameritrade-python-api: unofficial library for interfacing with TDAmeritrade. Link
  • CCXT: exchange-agnostic trading library for interfacing with the most popular cryptocurrency exchanges. Link
  • Python-binance: unofficial library for interfacing with Binance. Link
  • Coinbase: official library for interfacing with Coinbase. Link

Error processing API data.

Categories:

Tags:

[convertkit form=4793161]

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *