from datetime import timedelta, datetime
import statsmodels.api as sm
import numpy as np
import pandas as pd
from Selection.FundamentalUniverseSelectionModel import FundamentalUniverseSelectionModel
from sklearn.decomposition import PCA
class SMAPairsTrading(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2015, 1 , 1 )
self.SetEndDate(2020, 9 , 1 )
self.SetCash(100000)
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.Universe.Index.QC500)
self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw
self.AddAlpha(PairsTradingAlphaModel())
self.SetPortfolioConstruction(EqualWeightingPortfolioConstructionModel())
self.SetExecution(ImmediateExecutionModel())
self.SetRiskManagement(MaximumDrawdownPercentPerSecurity(0.03))
self.SetBenchmark("SPY")
self.SetSecurityInitializer(self.CustomSecurityInitializer)
self.buy = pd.DataFrame()
self.sell = pd.DataFrame()
self.liquidate = pd.DataFrame()
def OnEndOfDay(self, symbol):
self.Log("Taking a position of " + str(self.Portfolio[symbol].Quantity) + " units of symbol " + str(symbol))
def CustomSecurityInitializer(self, security):
security.SetLeverage(1)
class PairsTradingAlphaModel(AlphaModel):
def __init__(self):
self.pair = []
self.period = timedelta(days=1)
def Update(self, algorithm, data):
List=[x.Symbol for x in self.pair]
history = algorithm.History(List, 61 ).close.unstack(level=0)
self.buy,self.sell,self.liquidate = self.GetIndexes( history)
Appd = []
for i in self.buy:
Appd.append(Insight.Price(i,self.period, InsightDirection.Up,None,None,None))#,None, None, None,0.02))
for i in self.sell:
Appd.append(Insight.Price(i,self.period, InsightDirection.Down,None,None,None))
for i in self.liquidate:
Appd.append(Insight.Price(i,self.period, InsightDirection.Flat,None,None,None))
return Insight.Group([ x for x in Appd])
def GetIndexes(self, history):
# Sample data for PCA
sample = history.dropna(axis=1).pct_change().dropna()
sample_mean = sample.mean()
sample_std = sample.std()
sample = ((sample-sample_mean)/(sample_std)) #Normalizing
# Fit the PCA model for sample data
model = PCA().fit(sample)
#Distributing eigenportfolios
EigenPortfolio = pd.DataFrame(model.components_)
EigenPortfolio.columns = sample.columns
# EigenPortfolio = EigenPortfolio/sample_std
EigenPortfolio = ( EigenPortfolio.T / EigenPortfolio.sum(axis=1) )
# Get the first n_components factors
factors = np.dot(sample, EigenPortfolio)[:,:1] # we want to replicate the market
# Add 1's to fit the linear regression (intercept)
factors = sm.add_constant(factors)
# Train Ordinary Least Squares linear model for each stock
OLSmodels = {ticker: sm.OLS(sample[ticker], factors).fit() for ticker in sample.columns}
# Get the residuals from the linear regression after PCA for each stock
resids = pd.DataFrame({ticker: model.resid for ticker, model in OLSmodels.items()})
# Get the OU parameters
shifted_residuals = resids.cumsum().iloc[1:,:]
resids = resids.cumsum().iloc[:-1,:]
resids.index = shifted_residuals.index
OLSmodels2 = {ticker: sm.OLS(resids[ticker],sm.add_constant(shifted_residuals[ticker])).fit() for ticker in resids.columns}
# Get the new residuals
resids2 = pd.DataFrame({ticker: model.resid for ticker, model in OLSmodels2.items()})
# Get the mean reversion parameters
a = pd.DataFrame({ticker : model.params[0] for ticker , model in OLSmodels2.items()},index=["a"])
b = pd.DataFrame({ticker: model.params[1] for ticker , model in OLSmodels2.items()},index=["a"])
e = (resids2.std())/(252**(-1/2))
k = -np.log(b) * 252
#Get the z-score
var = (e**2 /(2 * k) )*(1 - np.exp(-2 * k * 252))
num = -a * np.sqrt(1 - b**2)
den = ( 1-b ) * np.sqrt( var )
m = ( a / ( 1 - b ) )
zscores=(num / den ).iloc[0,:]# zscores of the most recent day
# Get the stocks far from mean (for mean reversion)
selected_buy = zscores[zscores < -1.5].dropna().sort_values()[:1]
selected_sell = zscores[zscores > 1.5].dropna().sort_values()[-1:]
selected_liquidate = zscores[abs(zscores) < 0.50 ]
# Return each selected stock
weights_buy = selected_buy.index
weights_sell = selected_sell.index
weights_liquidate = selected_liquidate.index
return weights_buy, weights_sell, weights_liquidate
def OnSecuritiesChanged(self, algorithm, changes):
self.pair = [x for x in changes.AddedSecurities]
Hello everybody !
I finally could rewrite my original messy code but each time I execute the algorithm it seems like orders are not fulfilled during the day but rather at the opening/closing of the market.It causes a real problem for the risk management.I tried to switch the universe from daily to hourly but it sounds like the GetIndexes function uses the 61 hourly datas rather than the 61 daily data.
How can keep the daily datas with in the same time executing the orders during the full trading hours and only selecting one stock (as the algorithm is originally doing) per day and not per hour ?
Thank You !!
Shile Wen
Hi Wawes2, To get daily resolution data in a history call, try algorithm.History(tickers, periods, Resolution.Daily). Please see the attached backtest for reference.As for why orders are filled using daily data, that is because orders are made after the market is closed (which is why we have the Close price for the current day).To make it so we only trade once per day, we can keep track of the current day, which is self.curr_day in the algorithm, and only execute the rest of the Update method when the current day changes. This can also be seen in the attached backtest.Best,
Shile Wen
Wawes23
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 QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect 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. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!