Lecture 23

# Example: Basic Pairs Trading Algorithm

## Introduction

This algorithm is a very simple educational example to go along with the Introduction to Pairs Trading Lecture. For a more advanced algorithm closer to something you could actually trade, please see later in the lecture series.

## Prerequisites

```"""
This is a basic pairs trading algorithm that uses the Optimize API.
WARNING: THIS IS A LEARNING EXAMPLE ONLY. DO NOT TRY TO TRADE SOMETHING THIS SIMPLE.
https://www.quantopian.com/workshops
https://www.quantopian.com/lectures

For any questions, email [email protected]
"""
import numpy as np
import pandas as pd
import quantopian.optimize as opt
import quantopian.algorithm as algo

MAX_GROSS_EXPOSURE = 1.0 # Set exposure constraint constant value for optimizer

def initialize(context):
"""
Called once at the start of the algorithm.
"""
schedule_function(check_pair_status, date_rules.every_day(), time_rules.market_close(minutes=60))

context.stock1 = symbol('ABGB')
context.stock2 = symbol('FSLR')
context.stocks = [context.stock1, context.stock2]

# Our threshold for trading on the z-score
context.entry_threshold = 0.2
context.exit_threshold = 0.1

# Create a variable to store our target weights
context.target_weights = pd.Series(index=context.stocks, data=0.0)

# Moving average lengths
context.long_ma_length = 30
context.short_ma_length = 1

# Flags to tell us if we're currently in a trade
context.currently_long_the_spread = False
context.currently_short_the_spread = False

def check_pair_status(context, data):

# For notational convenience
s1 = context.stock1
s2 = context.stock2

# Get pricing history
prices = data.history([s1, s2], "price", context.long_ma_length, '1d')

# Try debugging me here to see what the price
# data structure looks like
# To debug, click on the line number to the left of the
# next command. Line numbers on blank lines or comments
# won't work.
short_prices = prices.iloc[-context.short_ma_length:]

# Get the long mavg
long_ma = np.mean(prices[s1] - prices[s2])
# Get the std of the long window
long_std = np.std(prices[s1] - prices[s2])

# Get the short mavg
short_ma = np.mean(short_prices[s1] - short_prices[s2])

# Compute z-score
if long_std > 0:
zscore = (short_ma - long_ma)/long_std

# Our two entry cases
if zscore > context.entry_threshold and \
not context.currently_short_the_spread:
context.target_weights[s1] = -0.5 # short top
context.target_weights[s2] = 0.5 # long bottom
context.currently_short_the_spread = True
context.currently_long_the_spread = False

elif zscore < -context.entry_threshold and \
not context.currently_long_the_spread:
context.target_weights[s1] = 0.5 # long top
context.target_weights[s2] = -0.5 # short bottom
context.currently_short_the_spread = False
context.currently_long_the_spread = True

# Our exit case
elif abs(zscore) < context.exit_threshold:
context.target_weights[s1] = 0 # close out
context.target_weights[s2] = 0 # close out
context.currently_short_the_spread = False
context.currently_long_the_spread = False
record('zscore', zscore)

# Call the optimizer
allocate(context, data)

def allocate(context, data):
# Set objective to match target weights as closely as possible, given constraints
objective = opt.TargetWeights(context.target_weights)

# Define constraints
constraints = []
constraints.append(opt.MaxGrossExposure(MAX_GROSS_EXPOSURE))

algo.order_optimal_portfolio(
objective=objective,
constraints=constraints,
)
```

The lectures on this website are provided for informational purposes only and do not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor do they constitute an offer to provide investment advisory services by Quantopian.

In addition, the lectures offer no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances.