FWIW, I am having the exact same problem, on a class that is just a few lines, that I was able to run just a minute ago. I tried deleting everything and then copying it back from notepad, but to no avail. This is so frustrating....
Code follows:
import clr
clr.AddReference("System")
clr.AddReference("QuantConnect.Algorithm")
clr.AddReference("QuantConnect.Indicators")
clr.AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Indicators import *
import numpy as np
from datetime import timedelta
from sklearn.linear_model import LinearRegression
class OptionsTest(QCAlgorithm):
def Initialize(self):
# Set the cash we'd like to use for our backtest
# This is ignored in live trading
self.SetCash(100000)
# Start and end dates for the backtest.
# These are ignored in live trading.
self.SetStartDate(2014,6,30)
self.SetEndDate(2014,7,1)
# Add assets you'd like to see
self.AddEquity("SPY", Resolution.Daily)
self.AddEquity("XIV", Resolution.Daily)
option = self.AddOption("SPY", Resolution.Daily)
option.SetFilter(-2, 2, timedelta(0), timedelta(182))
self.o_symbol=option.Symbol
## we sort the contracts to find at the money (ATM) contract with farthest expiration
def OnData(self, slice):
d1=self.History('SPY',21,Resolution.Daily)
d2=self.History('XIV',21,Resolution.Daily)
index=[]
for slice1,slice2 in zip(d1,d2):
index.append(slice1.Time.date())
data.append([np.float(slice1.Close),np.float(slice2.Close)])
df=pd.DataFrame(data,columns=['SPY','XIV'],index=pd.Series(index))
rr=df.diff().fillna(0)
lr=LinearRegression()
X=np.column_stack((rr['SPY'].values,rr['SPY'].values**2))
y=rr['XIV'].values
lr.fit(X,y)
resid=y-lr.predict(X)
resid=resid/resid.std()
for kvp in slice.OptionChains:
if kvp.Key != self.o_symbol:continue
chain = kvp.Value
contracts = sorted(sorted(chain,key = lambda x: abs(chain.Underlying.Price - x.Strike)),
key = lambda x: x.Expiry, reverse=True)
if len(contracts) == 0: continue
symbol = contracts[0].Symbol
if resid[-1]>1:
self.MarketOnCloseOrder(symbol, -1)
if resid[-1]<-1:
self.MarketOnCloseOrder(symbol, 1)