Introduction

Pairs trading is a classic approach to statistical arbitrage that has a long-standing track record. The traditional means of determining pairs trading has been through frequentist statistical cointegration tests, such as the Engle-Granger two-step test.

Here we present a more sophisticated Bayesian approach to pairs trading using probabilistic programming, a form of Bayesian machine learning. Unlike simpler frequentist cointegration tests, our Bayesian approach allows us to monitor the relationship between a pair of equities over time, which allows us to follow pairs whose cointegration parameters change steadily or abruptly. When combined with a simple mean-reversion trading algorithm, we demonstrate this to be a viable theoretical trading strategy, ready for further evaluation and risk management.

One potential source for good pairs trading candidates is the corporate supply chain. Finding a company’s actual suppliers and customers is often very difficult. Many companies do not want to reveal this information to their competitors. Leveraging hybrid data sources to deliver a complete picture of company supply chain ecosystems, the AlphaWave Data Corporate Supply Chain API enables investors to construct hedges and build statistical arbitrage strategies for a given corporate supply chain. Jupyter Notebooks detailing this analysis are also available on Google Colab and Github.

Software

For this project, we use several Python-based scientific computing technologies and make use of an equities backtesting engine along with the AlphaWave Data Corporate Supply Chain API.

import json
import requests
import pymc3 as pm
import numpy as np
import pandas as pd
import theano as th
import seaborn as sns
import sklearn.decomposition
import matplotlib.pyplot as plt
%matplotlib notebook
sns.set()
import warnings
warnings.filterwarnings('ignore')

Stock Selection for Pairs Trading

Since our goal is to build a pairs trading algorithm, one potential source of cointegrated stocks is the corporate supply chain. Using the AlphaWave Data Corporate Supply Chain API, we can get a list of suppliers and customers for a given stock symbol. To call this API with Python, you can choose one of the supported Python code snippets provided in the API console. Here is an example of how to invoke the API with Python Requests. You will need to insert your own x-rapidapi-host and x-rapidapi-key information in the code block below.

url = "https://corporate-supply-chain.p.rapidapi.com/api/v1/resources/supplychain"

querystring = {"ticker":"IBM"}
headers = {
    'x-rapidapi-host': "YOUR_X-RAPIDAPI-HOST_WILL_COPY_DIRECTLY_FROM_RAPIDAPI_PYTHON_CODE_SNIPPETS",
    'x-rapidapi-key': "YOUR_X-RAPIDAPI-KEY_WILL_COPY_DIRECTLY_FROM_RAPIDAPI_PYTHON_CODE_SNIPPETS"
    }
response = requests.request("GET", url, headers=headers, params=querystring)
print(response.text)

Output:

Image for post

# Create DataFrame.  
df = pd.DataFrame.from_dict(response.json())
df

Output:

Image for post

That’s it!

Now you have a list of suppliers and customers in a pandas DataFrame for a given stock symbol (IBM in this example). If you wish, you can further filter the list of suppliers and customers using fundamental data, technical indicators, or other sources of alternative data to get a list of good pairs trading candidates.

In this example, we will select IBM’s customer ABC to continue our pairs trading analysis.

#supply-chain #pair-trading #bayesian-machine-learning #stock-market #algorithmic-trading #algorithms

Bayesian Pairs Trading using Corporate Supply Chain Data
2.00 GEEK