# 10 useful Python libraries & packages for automated trading

Hey! I’m Joanne, an intern at **lemon.markets**, and I’m here to share some invaluable Python libraries & packages to use when you’re working with financial data and automated trading. At lemon.markets, we provide the infrastructure for developers to build their own **brokerage experience at the stock market. **Whatever your product might look like, there’s usually one or more Python libraries that can do the legwork for you. So, instead of re-inventing the wheel, let’s have a look at which packages can facilitate your automated trading.

The focus here is on Python, but many of the featured libraries have either wrappers that allow them to be used in other languages, or have comparable alternatives. If you have additional suggestions, feel free to leave a comment below. 💭

I’ve split the trading process into three general steps: **manipulating (raw) data**, **performing technical analysis **and finally **assessing your portfolio**. There’s probably 100+ steps that can be inserted into this process, but as a starting point, we think this is a solid place to begin. It covers the ‘before’, the ‘during’ and the ‘after’ when it comes to implementing your strategy. If you’re struggling to find more steps, perhaps consider: data collection, data visualisation, paper trading, backtesting, machine learning, portfolio management…must I go on?

The process can look a little like this.

**Data Manipulation**

We make the assumption here that you’re collecting data before writing your trading strategy. Live market data, historical data, trading sentiment: it all falls within this category. And before you can perform any kind of manipulation, you need data to do it on. Naturally, the lemon.markets market data API can be used to retrieve historical market data. We’ll also be providing real-time market data in the near future (stay tuned!). But you’re not restricted to only market data, you can also, for example, scrape headlines from financial news sites to perform sentiment analysis. Regardless of where you obtain your data, you’ll notice that often your source won’t present the data in exactly the format you need: cue data manipulation tools.

**🐼 Pandas**

`1import pandas as pd`

No list of Python libraries for financial analysis (or really any kind of data-driven work) would be complete without the mention of Pandas. It’s a powerful data manipulation tool that works with data structures called Series (one-dimensional) and DataFrames (two-dimensional). It can be used to intelligently index data, merge and join different data sets and even perform computations. The documentation includes a10-minute guide to Pandas and DataCamp has a tutorial on using Python for Finance.

**🔢 NumPy**

`1import numpy as np`

NumPy, or the Numerical Python library, is*the*package when it comes to scientific computing in Python. Specifically, NumPy provides functions for linear algebra, Fourier transforms and random number generation. It’s widely used because it utilises vectorisation, which means it can turn a computation which might take 1000 cycles into one that takes 250 cycles. As developers, we’re always looking to reduce computational power where ever possible.

**👩🔬 SciPy**

`1import scipy as sp`

SciPy is the scientific library that builds on NumPy — it includes modules for statistics, optimisation, integration, linear algebra and more. Similar to NumPy, but with more functionality (which comes at a price: slower computation speed). Check out the documentation to see if it meets your requirements!

**📊 Matplotlib**

`1import matplotlib.pyplot as plt`

The Matplotlib library can be used to create static, animated and interactive visualisations in Python. There are a million reasons why you might like to visualise data in financial analysis. For example, you might want to measure the performance of a single stock (or basket of stocks) against an index like the S&P500. Or, you might want to construct a simple histogram of daily stock returns to determine (visually) whether they follow a normal distribution. Of course, this would need to be backed up by a statistical test, which can be done with the statsmodels library (coming up soon).

You’ll notice that the above four libraries are often used simultaneously in projects, and likely, in your use-case it’ll be the same situation. They integrate seamlessly. Many additional niche packages are built on top of these four packages, for example: PyNance. There’s lots of resources available regarding these libraries: to get started, here’s an introduction to NumPy and Pandas.

Let’s look at a small example of how all four libraries can be used for a simple returns visualisation (we plot a histogram of Tesla daily returns against a normal distribution curve):

```
1import requests
2import pandas as pd
3import numpy as np
4from scipy.stats import norm
5import matplotlib.pyplot as plt
6client_id: str = "your client id here"
7client_secret: str = "your client secret here"
8def get_token():
9 auth = requests.post("https://auth.lemon.markets/oauth2/token",
10 data={
11 "client_id": client_id,
12 "client_secret": client_secret,
13 "grant_type": "client_credentials"
14 })
15 token: str = "Bearer " + auth.json()['access_token']
16 return token
17def get_ohlc():
18 response = requests.get(
19 'https://paper-data.lemon.markets/v1/ohlc/d1/?isin=US0378331005
20 &from=2021-06-25T00:00:00&to=2021-08-24T00:00:00',
21 headers={"Authorization": get_token()})
22 results = response.json()['results']
23 return results
24def calculate_returns():
25 df = pd.DataFrame(get_ohlc())
26 # calculate returns based on closing price
27 df['r'] = df['c'].pct_change().fillna(method='bfill')
28 df.tail()
29 return df
30def plot_returns():
31 returns = calculate_returns()['r']
32 # plot returns
33 plt.hist(returns, bins=25, density=True, alpha=0.6, color='darkorange')
34 # plot normal distribution fitted to returns
35 mu, std = norm.fit(returns)
36 xmin, xmax = plt.xlim()
37 x = np.linspace(xmin, xmax, 100)
38 p = norm.pdf(x, mu, std)
39 plt.plot(x, p, 'k', linewidth=2)
40 plt.xlabel('Daily Tesla Returns')
41 plt.ylabel('Probability Density Function')
42 plt.show()
43plot_returns()
```

In this script, you’ll have to fill in the authorisation URL, paper trading URL and your own client ID and secret.

This script will return the following histogram:

Obviously, we do not have enough data points to conclude whether Tesla daily returns follow a normal distribution. We hope that this little example shows you what can be done with these data manipulation packages and our OHLC endpoint.

**Technical Analysis**

Your strategy may or may not employ technical analysis. If you’re somehow using historical price data to predict future price movement, then this falls under technical analysis. If you’re not, don’t worry, it’s not necessary in order to implement an automated trading strategy (but might be helpful nonetheless, so feel free to dive in).

**📈 TA-Lib**

`1import talib as ta`

TA-Lib, or Technical Analysis Library, can be used to perform technical analysis on financial data by calculating well-known technical indicators, such the Weighted Moving Average (WMA) or Relative Strength Index (RSI). It can also recognise candlestick patterns, such as the inverted hammer or homing pigeon, to name a few. These indicators might serve as buy or sell signals for your trading strategy.

**📉 Statsmodels**

`1import statsmodels.api as sm`

Python already includes a built-in statistics module, but the statsmodels package can be used for more in-depth statistical analysis. Say you want to construct an ARIMA model for historical price data in order to predict price movement in the future, then this library would be the tool to use. Or, your use-case might be more simple, such as conducting a Jarque-Bera test for normality of residuals after a regression.

**🙋 PyStan**

`1import stan`

Bayesian inference is used in financial modelling to assess return predictability and strategy risk (among other things). PyStan is the Python-adapted package to perform Bayesian inference. Note that you need to use a domain specific language based on C++ (called Stan), which makes this package a bit more difficult to use. However, it is very powerful in that it allows you to perform high-level statistical modelling, analysis and prediction.

**➗ f.fn()**

`1import ffn`

ffn is a library that extends Pandas, NumPy and SciPy and contains functions often used within the quantitative finance framework. For example, you can use it to calculate the risk parity weights given a DataFrame (🐼) of returns. If you’re not familiar with risk parity, it’s an investment management technique that determines how to allocate risk within a portfolio. (Have we mentioned that reading the documentation of financial-related libraries is a great way to get familiarised with new metrics?)

The above four libraries can be used to determine when, what and how much to buy or sell. Once these decisions are made, the lemon.markets trading API can be used to place your orders on the stock market. An order can be placed as follows:

```
1import requests
2def place_order(isin: str, side: str, quantity: int):
3 if trading_signal:
4 request = requests.post("https://paper-trading.lemon.markets/rest/v1/spaces/space-uuid/orders/",
5 data={"isin": isin, "valid_until": "2021-08-25T23:59:59", "side": side, "quantity": quantity},
6 headers={"Authorization": get_token()})
7 print("Your trade was placed")
8 else:
9 print("Your trade was not placed")
10place_order("US0378331005", "buy", 1)
```

To run this code snippet, you need to fill in the paper trading URL, Space UUID and define a get_token() function as we did in the previous example.

The boolean trading_signal indicates whether the trade should be placed or not (this is where the aforementioned libraries come in handy). However, in this script we have not defined it yet.

**Portfolio Assessment**

Once your strategy is finished and implemented, it’s important to measure its performance, not only by returns, but also by calculating e.g. the risk associated with it. Portfolio analysis is not a one-and-done event: a good investor assesses their portfolio (or automates the process) regularly and implements necessary changes, such as a rebalancing or purchasing additional stocks to diversify appropriately. Note: your lemon.markets portfolio can be accessed via the Portfolio endpoint:

```
1import requests
2def get_portfolio():
3 request = requests.get("https://paper-trading.lemon.markets/rest/v1/spaces/space-uuid/portfolio/",
4 headers={"Authorization": get_token()})
5 print(request.json())
```

Which would return each instrument in your portfolio in the following fashion:

```
1"instrument": {
2 "title": "",
3 "isin": "" },
4"quantity": 0,
5"average_price": "",
6"latest_total_value": ""
```

**✋ Empyrical**

`1import empyrical as em`

Empyrical can be used to calculate well-known performance and risk statistics, for example the Sharpe ratio, alpha and beta. These metrics might show how the portfolio performs in relation to the market and indicate whether structural changes should be made. It’s an open-source project initiated by the now-defunct Quantopian, however the GitHub repository remains somewhat active (fingers crossed it stays that way 🙏🏼).

**💼 PyFolio**

`1import pyfolio as pf`

PyFolio is quite similar to Empyrical in that it can create an image that reflects performance and risk analysis. It does this through a so-called tear sheet, which includes metrics such as the stability, maximum drawdown and kurtosis of your portfolio’s returns.

These ten Python libraries and packages should provide a good starting point for your automated trading journey. Integration with the lemon.markets API is possible at every step: market data can be retrieved for data manipulation, orders can be placed according to technical indicators and the portfolio can be accessed to do risk and performance assessments. We strive to make the API as transparent as possible, to give you, the developer, full control over your brokerage experience.

If you’re not already signed-up to lemon.markets, join our waitlist here, we’d love to have you! Connect with us by leaving behind a comment, sending us an email and joining our vibrant Slack community. You’re bound to pick up some additional tools and inspiration along the way.

See you soon,

Joanne 🍋

## You might also be interested in

#### Setting up your own Telegram bot to trade with the lemon.markets API (Part 1 of 2)

Hi! My name is Joanne and I’m part of the team at lemon.markets. I’ve been working on a fun use-case for our product for the past few weeks and I’m very excited to share it with you! There’s hundreds of use-cases for our product, from automated trading strategies to portfolio visualisation dashboards. Today, I’ll show you how you can connect the lemon.markets API to the Telegram API. Why? So you can have a personalised butler — ahem, bot — that can place trades with a few very simple messages.

#### Creating your own lemon.markets Dashboard using Flutter

Hey there. My name is Marius, and I am part of lemon.markets, an early-stage startup from the heart of Berlin that is working on an infrastructure that lets you build your own brokerage experience at the stock market. In this blog post, I will walk you through a project developed by our community member Melanie, where we will tackle the basics of building your own mobile-optimised Trading dashboard using Flutter. Does that sound like fun to you? Then let’s not waste any more time and get going.

#### Building a Mean Reversion Strategy with the lemon.markets API & hosting it in the cloud

In this post, I want to diveinto how you can use the lemon.markets API to build one of the most well-known trading strategies: the **Mean reversion Strategy**. And, to set you up for success, I will also walk you through the steps to **host your strategy up in the cloud **using Heroku, to make sure your program continues to run, even when your laptop is closed.

#### Find more resources to get started easily

Check out our documentation to find out more about our API structure, different endpoints and specific use cases.

#### Join lemon.markets community

Join our Slack channel to actively participate in our community, ask questions to other users and stay up to date at all times.

#### Interested in building lemon.markets with us?

We are always looking for great additions to our team that help us build a brokerage infrastructure for the 21st century.