Overall Statistics
Total Trades
268
Average Win
0.42%
Average Loss
-0.25%
Compounding Annual Return
103.588%
Drawdown
4.400%
Expectancy
0.660
Net Profit
24.307%
Sharpe Ratio
5.508
Probabilistic Sharpe Ratio
99.688%
Loss Rate
38%
Win Rate
62%
Profit-Loss Ratio
1.68
Alpha
0.642
Beta
0.1
Annual Standard Deviation
0.118
Annual Variance
0.014
Information Ratio
3.6
Tracking Error
0.158
Treynor Ratio
6.515
Total Fees
$327.95
Estimated Strategy Capacity
$82000000.00
Lowest Capacity Asset
SPY R735QTJ8XC9X
import numpy as np
import pandas as pd
import datetime as datetime
import statsmodels.formula.api as sm
# from pandas import datetime
import statsmodels.tsa.stattools as ts
from pair import *
from QuantConnect import *
from QuantConnect.Algorithm import *

class PairsTrading(QCAlgorithm):

    def __init__(self):
        self.symbols = ['SPY', 'VXX']
        self.data_resolution = 1 # one minute?
        self.num_bar = 23400 # 6.5*60*60/(self.data_resolution) # 3-month period, used for pair selection criteria
        self.one_month = 11700 # 6.5*20*60/(self.data_resolution) 
        self.selected_num = 1
        self.pair_num = 1
        self.count = 0 # counts number of datapoints received
        self.pair_list = []
        self.selected_pair = []
        self.trading_pairs = []
        self.generate_count = 0
        self.data_count = 0

    def Initialize(self):
        self.SetStartDate(2021,10,1)
        self.SetEndDate(2022,1,20)
        self.SetCash(50000)
        self.BIC = float(self.GetParameter("BIC"))              # -3
        self.pair_threshold = float(self.GetParameter("pair_threshold")) # -0.7
        self.open_size = float(self.GetParameter("open_size")) # 2.25
        self.close_size = float(self.GetParameter("close_size")) # 1.75
        self.stop_loss = float(self.GetParameter("stop_loss")) # 20
        self.close_size_b = self.open_size + (self.open_size - self.close_size)


        for i in range(len(self.symbols)):
            equity = self.AddEquity(self.symbols[i],Resolution.Second).Symbol
            self.symbols[i] = equity
            self.symbols[i].prices = []
            self.symbols[i].dates = []
            self.Debug(equity)

    def generate_pairs(self):
        
#        self.Debug('generating pairs')

        for i in range(len(self.symbols)):
            for j in range(i+1,len(self.symbols)):
                self.pair_list.append(pairs(self.symbols[i],self.symbols[j]))
        
#        for x in range(len(self.pair_list)):
#            self.Debug('pairs list before: %s'%(str(self.pair_list[x])))

        self.pair_list = [x for x in self.pair_list if x.cor <= self.pair_threshold]
        
        # self.pair_list.sort(key = lambda x: x.cor, reverse = True)

#        if len(self.pair_list) > self.pair_num:
#            self.pair_list = self.pair_list[:self.pair_num]
        
#        for x in range(len(self.pair_list)):
#            self.Debug('pairs list after: %s'%(str(self.pair_list[x])))

    def OnData(self,data):
        if not self.Securities[self.symbols[0]].Exchange.ExchangeOpen:
            return
        #else:
        #    self.Debug('exchange open')

        #data aggregation
 #       if self.data_count <= self.data_resolution:
 #           self.data_count +=1
#            self.Debug('data count < resolution')
 #           return
        
        # refill the initial df
        if len(self.symbols[0].prices) < self.num_bar:
#            self.Debug('df not full')

            for i in self.symbols:
                if data.ContainsKey(i) is True:
                    i.prices.append(float(data[i].Close))
                    i.dates.append(data[i].EndTime)
                else:
                    self.Debug('%s is missing'%str(i))
                    self.symbols.remove(i)
            self.data_count = 0
            return
#        else:
#            self.Debug('df full')
        
        # generate pairs
        if self.count == 0 and len(self.symbols[0].prices) == self.num_bar:
            if self.generate_count == 0:
                for i in self.symbols:
                    i.df = pd.DataFrame(i.prices, index = i.dates, columns = ['%s'%str(i)])

                self.generate_pairs()
                self.generate_count +=1

#            self.Debug('pair list length:'+str(len(self.pair_list)))
            
        # correlation selection
            for i in self.pair_list:
                i.cor_update()
#                self.Debug(i.cor)
                
        # update the dataframe and correlation selection
#            self.Debug(str(len(self.pair_list[0])))
            if len(self.pair_list) != 0:
                for i in self.pair_list:
                    i.df_update()
                    i.cor_update()
            else:
                generate_count = 0
                return
            
            self.selected_pair = [x for x in self.pair_list if x.cor <= self.pair_threshold]
#            self.selected_pair = self.pair_list
            
        # cointegration selection
            for i in self.selected_pair:
                i.cointegration_test()

            self.selected_pair = [x for x in self.selected_pair if x.adf <= self.BIC]
#            self.selected_pair.sort(key = lambda x: x.adf)

            if len(self.selected_pair) == 0:
                self.Debug('no selected pair')
                self.count += 1
                return

#            self.selected_pair = self.pair_clean(self.selected_pair)

            for i in self.selected_pair:
                i.touch = 0
#                self.Debug(str(i.adf) + i.name)

            if len(self.selected_pair) > self.selected_num:
                self.selected_pair = self.selected_pair[:self.selected_num]

            self.count +=1
            self.data_count = 0
            return

        #update the pairs
#        self.Debug('updating pairs')
        if self.count != 0 and self.count < self.one_month:

            num_select = len(self.selected_pair)

            for i in self.pair_list:
                if data.ContainsKey(i.a) is True and data.ContainsKey(i.b) is True:
                    i.price_record(data[i.a],data[i.b])
                else:
                    self.Debug('%s has no data'%str(i.name))
                    self.pair_list.remove(i)
        ## selected pairs

            for i in self.selected_pair:
                i.last_error = i.error
#                self.Debug(i.last_error)

            for i in self.trading_pairs:
                i.last_error = i.error

        ## enter
#            self.Debug('ready to trade')
            for i in self.selected_pair:
                price_a = float(data[i.a].Close)
                price_b = float(data[i.b].Close)
                
                i.error = price_a - (i.model.params[0] + i.model.params[1]*price_b)
                if (self.Portfolio[i.a].Quantity == 0 and self.Portfolio[i.b].Quantity == 0) and i not in self.trading_pairs:
                    if i.touch == 0:
                        if i.error < i.mean_error - self.open_size*i.sd and i.last_error > i.mean_error - self.open_size*i.sd:
                            i.touch += -1
                        elif i.error > i.mean_error + self.open_size*i.sd and i.last_error < i.mean_error + self.open_size*i.sd:
                            i.touch += 1 
                        else:
                            pass
                    elif i.touch == -1:
                        if i.error > i.mean_error - self.open_size*i.sd and i.last_error < i.mean_error - self.open_size*i.sd:
                            self.Debug('long %s not long %s'%(str(i.a),str(i.b)))
                            i.record_model = i.model
                            i.record_mean_error = i.mean_error
                            i.record_sd = i.sd
                            self.trading_pairs.append(i)
                            self.SetHoldings(i.a, 2/(len(self.selected_pair)))
                            #self.SetHoldings(i.b, 0.3/(len(self.selected_pair))) 
                            i.touch = 0
                    elif i.touch == 1:
                        if i.error < i.mean_error + self.open_size*i.sd and i.last_error > i.mean_error + self.open_size*i.sd:
                            self.Debug('short %s not short %s'%(str(i.a),str(i.b)))
                            i.record_model = i.model
                            i.record_mean_error = i.mean_error
                            i.record_sd = i.sd
                            self.trading_pairs.append(i)
                            #self.SetHoldings(i.b, -0.3/(len(self.selected_pair)))
                            self.SetHoldings(i.a, -2/(len(self.selected_pair)))
                            i.touch = 0
                    else:
                        pass
                else:
                    pass

            # close
            for i in self.trading_pairs:
                if data.ContainsKey(i.a) and data.ContainsKey(i.b):
                    price_a = float(data[i.a].Close)
                    price_b = float(data[i.b].Close)
                    i.error = price_a - (i.record_model.params[0] + i.record_model.params[1]*price_b)
                    if ((i.error < i.record_mean_error + self.close_size * i.record_sd and i.last_error > i.record_mean_error + self.close_size*i.record_sd)
                       or (i.error > i.record_mean_error - self.close_size * i.record_sd and i.last_error < i.record_mean_error - self.close_size*i.record_sd)):
                        self.Debug('close %s'%str(i.name))
                        self.Liquidate(i.a)
                        self.Liquidate(i.b)
                        self.trading_pairs.remove(i)
                    elif i.error < i.record_mean_error - (self.stop_loss*i.record_sd) or i.error > i.record_mean_error + self.stop_loss*i.record_sd:
                        self.Debug('close %s to stop loss'%str(i.name))
                        self.Liquidate(i.a)
                        self.Liquidate(i.b)
                        self.trading_pairs.remove(i)
                    else:
                        pass
    
            self.count +=1
            self.data_count = 0
            return

        if self.count == self.one_month:
            self.count = 0
            self.data_count = 0
            return
import numpy as np
import pandas as pd
import datetime as datetime
import statsmodels.formula.api as sm
import statsmodels.tsa.stattools as ts
from QuantConnect import *
from QuantConnect.Algorithm import *

class pairs(object):

    def __init__(self,a,b):
        self.a = a
        self.b = b
        self.name = str(a) + ':' + str(b)
        self.df = pd.concat([a.df,b.df],axis = 1).dropna()
        self.num_bar = self.df.shape[0]
        self.cor = self.df.corr().ix[0][1]
        self.error = 0
        self.last_error = 0
        self.a_price = []
        self.a_date = []
        self.b_price = []
        self.b_date = []

    def cor_update(self):
        self.cor = self.df.corr().ix[0][1]

    def cointegration_test(self):
        self.model = sm.ols(formula = '%s ~ %s'%(str(self.a),str(self.b)), data = self.df).fit()
        self.adf = ts.adfuller(self.model.resid,autolag = 'BIC')[0]
        self.mean_error = np.mean(self.model.resid)
        self.sd = np.std(self.model.resid)

    def price_record(self,data_a,data_b):
        self.a_price.append(float(data_a.Close))
        self.a_date.append(data_a.EndTime)
        self.b_price.append(float(data_b.Close))
        self.b_date.append(data_b.EndTime)

    def df_update(self):
        new_df = pd.DataFrame({str(self.a):self.a_price,str(self.b):self.b_price},index = [self.a_date]).dropna()
        self.df = pd.concat([self.df,new_df])
        self.df = self.df.tail(self.num_bar)
        for i in [self.a_price,self.a_date,self.b_price,self.b_date]:
            i = []