Back to Community
Pairs trading algorithm

Hi, guys.

My second algorithm with pairs tradings strategy. The idea is simple, find two correlated stocks. Long the running slower one, and short the running faster one. Make the profit from the gap.

This test is based on COCA and PEPSI, and result shows the return is not extremely high, but very stable.

Clone Algorithm
607
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import pandas as pd
from collections import deque

def initialize(context):
    context.nobs = 30
    context.max_notional = 100000
    context.min_notional = -100000
    
    #currently we are running COCA& PEPSI
    context.stocks = [sid(4283), sid(5885)]
    context.ratio = deque([])
    context.strategy = 0

       
def handle_data(context, data):
    price0 = data[context.stocks[0]].price
    price1 = data[context.stocks[1]].price
    shares0 = context.portfolio.positions[context.stocks[0]].amount
    shares1 = context.portfolio.positions[context.stocks[1]].amount
    
    if len(context.ratio)<context.nobs:
        temp = price0/price1
        context.ratio.append(temp)
    else:
        beta=sum(context.ratio)/context.nobs
        if price0 > beta*1.02*price1 and context.strategy != 1:
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price1)
            order(context.stocks[1], num_shares)
            order(context.stocks[0], -1 * num_shares/beta )
            context.strategy = 1

        elif price0 < beta*0.98*price1 and context.strategy != 2 :
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price0)
            order(context.stocks[0], num_shares)
            order(context.stocks[1], -1 * num_shares*beta )
            context.strategy = 2

        context.ratio.popleft()
        temp=price0 / price1
        context.ratio.append(temp)
        record(gap=price0-beta*price1)
        record(shares0=shares0, shares1=shares1)

                
   
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.
12 responses

Same trick on the energy companies.

But question, how is the benchmark computed?

Clone Algorithm
607
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import pandas as pd
from collections import deque

def initialize(context):
    context.nobs = 30
    context.max_notional = 100000
    context.min_notional = -100000
    
    #currently we are running XOM & CVX
    context.stocks = [sid(8347), sid(23112)]
    context.ratio = deque([])
    context.strategy = 0

       
def handle_data(context, data):
    price0 = data[context.stocks[0]].price
    price1 = data[context.stocks[1]].price
    shares0 = context.portfolio.positions[context.stocks[0]].amount
    shares1 = context.portfolio.positions[context.stocks[1]].amount
    
    if len(context.ratio)<context.nobs:
        temp = price0/price1
        context.ratio.append(temp)
    else:
        beta=sum(context.ratio)/context.nobs
        if price0 > beta*1.03*price1 and context.strategy != 1:
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price1)
            order(context.stocks[1], num_shares)
            order(context.stocks[0], -1 * num_shares/beta )
            context.strategy = 1

        elif price0 < beta*0.97*price1 and context.strategy != 2 :
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price0)
            order(context.stocks[0], num_shares)
            order(context.stocks[1], -1 * num_shares*beta )
            context.strategy = 2

        context.ratio.popleft()
        temp=price0 / price1
        context.ratio.append(temp)
        record(gap=price0-beta*price1)
        record(shares0=shares0/1000, shares1=shares1/1000)

                
   
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Those are pretty great shares. Pair trading is a hot topic on Quantopian these days!

Disclaimer

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by Quantopian. In addition, the material offers 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.

So the benchmark is always SP500, not matter what kind of stocks I pick?

I mean to post my results in this discussion, but misplaced it. I changed this to run the GLD and GDX algorithms, and got okay results. How does the algorithm determine the determine the slower and faster running securities? I am new to python.

Thanks.

Hi, Josh

I don't think GLD and GDX is a good pair, their correlation is only 0.5189 based on the 2006 to 2013 historical data.
And for (PEP, KO) (XOM, CVX) are both above 0.9.

And What I did is using formular
gap = stock1- ratio*stock2
ratio = average(stock1/stock2) for historical data

when gap > 0, then stock1 running faster, and stock2 running slower.

Morten,

I don't see where in this source code says benchmark symbol is ^GSPC. I thought the benchmark is the S&P500 fund, SPY, not S&P500 itself, is that right?

-Huapu

Huapu,

You are right, it is SPY. And they've discussed it here.

This time, I tried the OLS fit but not average the ratio. It should be more sophisticated method. But as you can see the result is no big difference, or even worse

Clone Algorithm
607
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import pandas as pd
from collections import deque
import statsmodels.api as sm

def initialize(context):
    context.nobs = 30
    context.max_notional = 100000
    context.min_notional = -100000
    
    #currently we are running XOM & CVX
    context.stocks = [sid(4283), sid(5885)]
    context.strategy = 0
    context.his0=[]
    context.his1=[]
    
    set_commission(commission.PerTrade(cost=1.00))
    set_slippage(slippage.FixedSlippage(spread=0.00))
       
def handle_data(context, data):
    
    
    price0 = data[context.stocks[0]].price
    price1 = data[context.stocks[1]].price
    shares0 = context.portfolio.positions[context.stocks[0]].amount
    shares1 = context.portfolio.positions[context.stocks[1]].amount
    
    if len(context.his0)<context.nobs:
        context.his0.append(price0)
        context.his1.append(price1)
    else:
        beta=sm.OLS(context.his0, context.his1).fit().params[0]
        if price0 > beta*1.03*price1 and context.strategy != 1:
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price1)
            order(context.stocks[1], num_shares)
            order(context.stocks[0], -1 * num_shares/beta )
            context.strategy = 1

        elif price0 < beta*0.97*price1 and context.strategy != 2 :
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price0)
            order(context.stocks[0], num_shares)
            order(context.stocks[1], -1 * num_shares*beta )
            context.strategy = 2

        del context.his0[0]
        del context.his1[0]
        context.his0.append(price0)
        context.his1.append(price1)
        record(gap=price0-price1*beta)
        record(shares0=shares0/1000, shares1=shares1/1000)

                
   
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

And energy companies

Clone Algorithm
607
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import pandas as pd
from collections import deque
import statsmodels.api as sm

def initialize(context):
    context.nobs = 30
    context.max_notional = 100000
    context.min_notional = -100000
    
    #currently we are running XOM & CVX
    context.stocks = [sid(8347), sid(23112)]
    context.strategy = 0
    context.his0=[]
    context.his1=[]
    
    set_commission(commission.PerTrade(cost=1.00))
    set_slippage(slippage.FixedSlippage(spread=0.00))
       
def handle_data(context, data):
    
    
    price0 = data[context.stocks[0]].price
    price1 = data[context.stocks[1]].price
    shares0 = context.portfolio.positions[context.stocks[0]].amount
    shares1 = context.portfolio.positions[context.stocks[1]].amount
    
    if len(context.his0)<context.nobs:
        context.his0.append(price0)
        context.his1.append(price1)
    else:
        beta=sm.OLS(context.his0, context.his1).fit().params[0]
        if price0 > beta*1.05*price1 and context.strategy != 1:
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price1)
            order(context.stocks[1], num_shares)
            order(context.stocks[0], -1 * num_shares/beta )
            context.strategy = 1

        elif price0 < beta*0.95*price1 and context.strategy != 2 :
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price0)
            order(context.stocks[0], num_shares)
            order(context.stocks[1], -1 * num_shares*beta )
            context.strategy = 2

        del context.his0[0]
        del context.his1[0]
        context.his0.append(price0)
        context.his1.append(price1)
        record(gap=price0-price1*beta)
        record(shares0=shares0/1000, shares1=shares1/1000)

                
   
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

I modified Xin's pair trading strategy on KO and PEP. I used Bolinger band as the criteria for trading. The results are comparable. The main problem is that KO and PEP doesn't cointegrate and thus they are not very good pair trading candidates. The price ratio, beta, changes a lot with time and it is very difficult to follow the change. Pair trading would be more profitable with a better pair candidate.

Clone Algorithm
1585
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
import numpy as np
import pandas as pd
from collections import deque

R_P = 1 # refresh period in days
W_L = 11 # window length in days
lookback=22
def initialize(context):
    context.nobs = 10
    context.max_notional = 100000
    context.min_notional = -100000
    
    #currently we are running COCA& PEPSI
    context.stocks = [sid(4283), sid(5885)]
    context.ratio = deque([])
    context.strategy = 0
    context.daysToRecalibration = 0
    context.beta = 0

       
def handle_data(context, data):
    price0 = data[context.stocks[0]].price
    price1 = data[context.stocks[1]].price
    shares0 = context.portfolio.positions[context.stocks[0]].amount
    shares1 = context.portfolio.positions[context.stocks[1]].amount
    
    if len(context.ratio)<context.nobs or context.daysToRecalibration > 0:
        temp = price0/price1
        context.ratio.append(temp)
        log.info("ratio: {ratio}".format(ratio = sum(context.ratio)))
    #else:
        notional0 = (context.portfolio.positions[context.stocks[0]].amount*data[context.stocks[0]].price)/context.portfolio.starting_cash
        notional1 = (context.portfolio.positions[context.stocks[1]].amount*data[context.stocks[1]].price)/context.portfolio.starting_cash
        if context.daysToRecalibration == 0:
            context.beta=sum(context.ratio)/context.nobs
            context.daysToRecalibration = context.nobs
            context.ratio = deque([])
        log.info("beta = {beta}, price0 = {p0}, price1 = {p1}, diff = {diff}, notional0 = {not0}, notional1 = {not1}".format(beta = context.beta, p0 = price0, p1 = price1, diff = price0 - context.beta*price1, not0 = notional0, not1 = notional1))    
        context.daysToRecalibration -= 1
        rVal=getMeanStd(data, context.beta, context.stocks[0], context.stocks[1])
        if rVal is None:
            return  
        meanPrice,stdPrice = rVal
        h = ((price0 - context.beta*price1) - meanPrice)/stdPrice
        if h>2 and context.strategy != 1:
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price1)
            order(context.stocks[1], num_shares)
            order(context.stocks[0], -1 * num_shares/context.beta )
            context.strategy = 1

        elif h<-2 and context.strategy != 2 :
            order(context.stocks[0], -shares0)
            order(context.stocks[1], -shares1)
            num_shares = int(context.max_notional / price0)
            order(context.stocks[0], num_shares)
            order(context.stocks[1], -1 * num_shares*context.beta )
            context.strategy = 2
        
    record(strategy = context.strategy)
    record(diff = price0 - price1*context.beta, h = h)
        #record(port = context.portfolio.positions_value, cash = context.portfolio.cash)
    record(notional0 = notional0,
           notional1 = notional1)            

@batch_transform(window_length=W_L, refresh_period=R_P) 
def getMeanStd(datapanel, beta, sid0, sid1):
    prices = datapanel['price']
    meanPrice = np.mean(prices[sid0] - beta*prices[sid1])
    stdPrice=np.std(prices[sid0] - beta*prices[sid1])
    if meanPrice is not None and stdPrice is not None :
        return (meanPrice, stdPrice)
    else:
        return None
                
   
We have migrated this algorithm to work with a new version of the Quantopian API. The code is different than the original version, but the investment rationale of the algorithm has not changed. We've put everything you need to know here on one page.
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.