Skip to content

Instantly share code, notes, and snippets.

@christhchild28
Created April 19, 2018 16:09
Show Gist options
  • Save christhchild28/e63181eab6ab7d137c0065188d920a09 to your computer and use it in GitHub Desktop.
Save christhchild28/e63181eab6ab7d137c0065188d920a09 to your computer and use it in GitHub Desktop.
The following is a function that implements the trading strategy using two airline stocks (American Airlines and United Airlines)
// Implements a Pair Trading Strategy
import numpy as np
def initialize(context):
"""
Called once at the start of the algorithm.
"""
# Every day we check the pair status
schedule_function(check_pairs, date_rules.every_day(), time_rules.market_close(minutes=60))
# Our Two Airlines
context.aa = sid(45971) #aal
context.ual = sid(28051) #ual
# Flags to tell us if we're currently in a trade
context.long_on_spread = False
context.shorting_spread = False
def check_pairs(context, data):
# For convenience
aa = context.aa
ual = context.ual
# Get pricing history
prices = data.history([aa, ual], "price", 30, '1d')
# Need to use .iloc[-1:] to get dataframe instead of series
short_prices = prices.iloc[-1:]
# Get the long 30 day mavg
mavg_30 = np.mean(prices[aa] - prices[ual])
# Get the std of the 30 day long window
std_30 = np.std(prices[aa] - prices[ual])
# Get the shorter span 1 day mavg
mavg_1 = np.mean(short_prices[aa] - short_prices[ual])
# Compute z-score
if std_30 > 0:
zscore = (mavg_1 - mavg_30)/std_30
# Our two entry cases
if zscore > 0.5 and not context.shorting_spread:
# spread = aa - ual
order_target_percent(aa, -0.5) # short top
order_target_percent(ual, 0.5) # long bottom
context.shorting_spread = True
context.long_on_spread = False
elif zscore < -0.5 and not context.long_on_spread:
# spread = aa - ual
order_target_percent(aa, 0.5) # long top
order_target_percent(ual, -0.5) # short bottom
context.shorting_spread = False
context.long_on_spread = True
# Our exit case
elif abs(zscore) < 0.1:
order_target_percent(aa, 0)
order_target_percent(ual, 0)
context.shorting_spread = False
context.long_on_spread = False
record('zscore', zscore)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment