I'm trying to run Pairs trading strategy for BTCUSD and XMRUSD. Since data is not freely available, I have re-formatted my existing data (manually imported from API) to LEAN format, according to this - https://www.quantconnect.com/lean/documentation/topic16.html
But, I have only trade data, no quote. So, when I run my algo, it's throwing errors about missing "_quote" data:
20190426 09:27:12.450 ERROR:: DefaultDataProvider.Fetch(): The specified file was not found: ../../../Data/crypto/bitfinex/minute/xmrusd/20190201_quote.zip
To me it seems like this should be runnable with trade data only. Or am I wrong? Is there something I need to configure explicitly in my strategy for it to not look for quote data?
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Indicators")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Brokerages import *
from QuantConnect.Data import BaseData
from QuantConnect.Data.Market import *
from QuantConnect.Securities import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
from sklearn import linear_model
import numpy as np
import pandas as pd
from scipy import stats
from math import floor
from datetime import timedelta
class Pairs1(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019,1,1)
self.SetEndDate(2019,1,31)
self.SetCash(10000)
self.numdays = 7 # set the length of training period
self.symbols = []
self.threshold = 1.
self.AddCrypto("BTCUSD", Resolution.Minute, Market.Bitfinex);
self.AddCrypto("XMRUSD", Resolution.Minute, Market.Bitfinex);
for i in self.symbols:
i.hist_window = RollingWindow[TradeBar](self.numdays)
def OnData(self, data):
if not (data.ContainsKey("BTCUSD") and data.ContainsKey("XMRUSD")): return
for symbol in self.symbols:
symbol.hist_window.Add(data[symbol])
price_x = pd.Series([float(i.Close) for i in self.symbols[0].hist_window],
index = [i.Time for i in self.symbols[0].hist_window])
price_y = pd.Series([float(i.Close) for i in self.symbols[1].hist_window],
index = [i.Time for i in self.symbols[1].hist_window])
if len(price_x) < 250: return
spread = self.regr(np.log(price_x), np.log(price_y))
mean = np.mean(spread)
std = np.std(spread)
ratio = floor(self.Portfolio[self.symbols[1]].Price / self.Portfolio[self.symbols[0]].Price)
# quantity = float(self.CalculateOrderQuantity(self.symbols[0],0.4))
if spread[-1] > mean + self.threshold * std:
if not self.Portfolio[self.symbols[0]].Quantity > 0 and not self.Portfolio[self.symbols[0]].Quantity < 0:
self.Sell(self.symbols[1], 100)
self.Buy(self.symbols[0], ratio * 100)
elif spread[-1] < mean - self.threshold * std:
if not self.Portfolio[self.symbols[0]].Quantity < 0 and not self.Portfolio[self.symbols[0]].Quantity > 0:
self.Sell(self.symbols[0], 100)
self.Buy(self.symbols[1], ratio * 100)
else:
self.Liquidate()
def regr(self,x,y):
regr = linear_model.LinearRegression()
x_constant = np.column_stack([np.ones(len(x)), x])
regr.fit(x_constant, y)
beta = regr.coef_[0]
alpha = regr.intercept_
spread = y - x*beta - alpha
return spread