# 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 positions**. 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, positions 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
6def get_ohlc():
7 response = requests.get(
8 'https://data.lemon.markets/v1/ohlc/d1/?isin=US0378331005 \
9 &from=2021-06-25T00:00:00&to=2021-11-14T00:00:00',
10 headers={"Authorization": "Bearer YOUR-API-KEY"})
11 results = response.json()['results']
12 return results
13def calculate_returns():
14 df = pd.DataFrame(get_ohlc())
15 # calculate returns based on closing price
16 df['r'] = df['c'].pct_change().fillna(method='bfill')
17 df.tail()
18 return df
19def plot_returns():
20 returns = calculate_returns()['r']
21 # plot returns
22 plt.hist(returns, bins=25, density=True, alpha=0.6, color='darkorange')
23 # plot normal distribution fitted to returns
24 mu, std = norm.fit(returns)
25 xmin, xmax = plt.xlim()
26 x = np.linspace(xmin, xmax, 100)
27 p = norm.pdf(x, mu, std)
28 plt.plot(x, p, 'k', linewidth=2)
29 plt.xlabel('Daily Tesla Returns')
30 plt.ylabel('Probability Density Function')
31 plt.show()
32plot_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 positions. (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
2import json
3def place_order(isin: str, side: str, quantity: int):
4 if trading_signal:
5 request = requests.post("https://paper-trading.lemon.markets/v1/orders/",
6 data=json.dumps({
7 "isin": isin,
8 "expires_at": "p7d",
9 "side": side,
10 "quantity": quantity,
11 "venue": "XMUN"}),
12 headers={"Authorization": "BEARER YOUR-API-KEY"})
13 print("Your trade was placed")
14 else:
15 print("Your trade was not placed")
16place_order("US0378331005", "buy", 1)
```

To run this code snippet, you need to fill in the your API key.

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.

**Positions 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. Positions analysis is not a one-and-done event: a good investor assesses their positions (or automates the process) regularly and implements necessary changes, such as a rebalancing or purchasing additional stocks to diversify appropriately. Note: your lemon.markets positions can be accessed via the Positions endpoint:

```
1import requests
2request = requests.get("https://paper-trading.lemon.markets/v1/positions/",
3 headers={"Authorization": "BEARER YOUR-API-KEY"})
4print(request.json())
```

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

```
1results :[
2 {
3 "isin": "",
4 "isin_title": "",
5 "quantity": ,
6 "buy_price_avg": ,
7 "estimated_price_total": ,
8 "estimated_price":
9 }
10]
```

**✋ 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 positions 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 position’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 positions 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 us 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

#### Blog 38 - The market maker explained

Market Makers are crucial to provide liquidity to stock exchanges. In this blog post, we talk about what Market Makers do and why they are useful.

#### 5 (+1) YouTube channels for FinTech enthusiasts

YouTube is a great way to learn about new things, including financial education or coding. Therefore, in this article we’d like to introduce you to 5 YouTube channels to level up your trading literacy.

#### A short introduction to derivatives

In this article, we'd like to introduce you derivatives - they come up in finance and leave a lot of people scratching their heads, though it's totally worth it! Curious about hearing what's the difference between investing and trading, the coherence between finance and weather & why the Greeks even appear here? We'll discuss who’s using them, what they are and how they can be valued in the following.

#### 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.