Top Python trading packages for 2021

Shyam BV
Code Sprout
Published in
5 min readJul 3, 2021

Introduction

If you have followed my previous articles, I have been making trades for quite some time. However, being a technical and investor, I try to use python packages that simplify and automates my trades. In this article, I will cover top packages which I use in 2021. Some packages will be heavily focused on the US market.

Photo by MayoFi on Unsplash

Robin-stocks

Personally, this is one of my favorite packages, and I use it a lot. Robin-stocks package can connect to the below ones.

  • Robinhood — Famous US stock brokerage
  • TD Ameritrade — US stock brokerage
  • Gemini — Crypto stock exchange

Installation is pretty standard from PyPi.

pip install robin_stocks# For 2FA
pip install pyotp

Robinhood API Functions

Robinhood(referral link) is one of the top brokerages used by many millennials(Apart from recent backlash).

To login into robin_stocks, you needed your credentials. If you have 2FA, it will generate an OTP.

import robin_stocks as r
login = r.login('email','password')
Buying in Robinhood

Alpaca

Currently, most brokerage firms offer zero trading fees. However, not all brokerage firms have an API option to trade. Alpaca provides free trading with python API to trade. Once you create an account, you will have paper trading and live trading options. We can test the strategies in paper trading and implement them in live trading. It is just a key change for live trading.

If you can have a local environment, you can install the pip package. Once installed, you can select Paper trading or Live trading.

Based on your selection, you can get the API and secret key.

Now, these keys will be used in our code.

import alpaca_trade_api as tradeapiapi = tradeapi.REST('xxxxxxxx', 'xxxxxxxxxx',base_url='https://paper-api.alpaca.markets', api_version='v2',)
Trade with Alpaca

Pyalgotrade

You have come up with a mind-blowing algorithm for trading. How can you test your algorithm? Backtesting

Backtesting is one of the strategies to test your algorithms. It goes over the historical data and tests your algorithm. Going back in time and testing your strategy is a little tricky. pyalgotrade package would come in handy if you wanted to try the strategy.

PyAlgoTrade has 6 main components:

  • Strategies
  • Feeds
  • Brokers
  • DataSeries
  • Technicals
  • Optimizer

Below are the explanation of the main components.

Strategies: These are the classes that you define that implement the trading logic. When to buy, when to sell, etc.

Feeds: These are data-providing abstractions. For example, you’ll use a CSV feed that loads bars from a CSV (Comma-separated values) formatted file to feed data to a strategy. Feeds are not limited to bars. For example, there is a Twitter feed that allows incorporating Twitter events into trading decisions.

Brokers: Brokers are responsible for executing orders.

I am planning to write a complete article on backtesting. Follow for more updates.

Backtest the algorithm

Yahoo finance

Yahoo finance has been a quick goto utility for a while. It is easier to download historical data without much hassle. No complicated API keys are required.

pip install yfinance
Fetching historical data

On seeing the data, I wish I bought TSLA in 2010 :(

Although you can get the historical prices, it will not get you real-time data. So it is good only to some extend.

Polygon

If you wanted real-time data, polygon shines. It creates a WebSocket and sends you real-time prices of the stock. It is amazingly fast and good. However, if you wanted real-time data, you have to pay the price for it.

Irrespective of real-time data, getting a decent amount of near real-time data is a good package on the arsenal.

Finviz

I came to know about Finviz some months. It is an amazing package with a ton of information. And the entire data is free! I am sure they will wrap a freemium model in the future. Just use it until it is free.

You can get quotes for different stocks and get more details about a stock. In the below screenshot, I have downloaded all the stocks on Nasdaq and S&P500.

Data from Finviz

Finviz will allow you to focus on any particular industry. There are many more functions in Finviz. I would highly recommend checking it out.

Finmarketpy

Most of the other packages we have seen are related to the equities market. However, this package focuses on the forex market. Mainly it focuses on backtesting on forex with your algorithm.

It has integrations with other data providers such as Quandl. So you can quickly source the data from different data sources providers.

Check out more at this link https://github.com/cuemacro/finmarketpy

Conclusion

We have seen some top python packages for technical trading. There are a ton of other trading packages; however, many of them are not appropriately maintained. If you know of any other packages which I have missed, please add them in the comments.

Get Code

Please subscribe to my newsletter to get the complete working code for my articles and other updates.

--

--