I'm subscribing to securities with AddUniverse.
Trying to resolve this error by adding VXX manually triggers another similar error down the road.
This is my code:
import pandas as pd
from functools import partial
from QuantConnect.Securities.Option import OptionPriceModels
class ParticleCalibratedCoil(QCAlgorithm):
def Initialize(self):
'''
Parameters for adjusting
'''
self.numberOfLiquidStocks = 100 # Controls the number of stocks in play
'''
Backtesting variables
'''
self.SetStartDate(2018, 1, 1)
self.SetEndDate(2020, 1, 1)
self.SetCash(1000000)
'''
Algorithm variables
'''
self.UniverseSettings.Resolution = Resolution.Minute
self.AddUniverse(self.CoarseSelectionFilter)
self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw
self.indicators = {}
self.rankingOfAbsoluteIV = []
self.dayCounter = 0
self.Schedule.On(self.DateRules.EveryDay(), \
self.TimeRules.At(9, 40), \
self.EveryDayAfterMarketOpen)
def CoarseSelectionFilter(self, coarse):
'''
1. Sorts each element of the coarse object by dollar volume
2. Returns a list of coarse object, limited to top 100 highest volume
3. Returns a list of symbols we want to initialise into the algorithm
'''
self.sortedByDollarVolume = sorted(coarse, key=lambda c: c.DollarVolume, reverse=True)
self.topHundredMostLiquid = self.sortedByDollarVolume[:self.numberOfLiquidStocks]
return [stock.Symbol for stock in self.topHundredMostLiquid]
def OnSecuritiesChanged (self,changes):
for underlying in changes.AddedSecurities:
if underlying.Symbol.SecurityType != SecurityType.Equity: continue
option = self.AddOption(underlying.Symbol.Value, Resolution.Minute)
option.SetFilter(-5, +2, timedelta(30), timedelta(60))
option.PriceModel = OptionPriceModels.CrankNicolsonFD()
if not underlying.Symbol.Value in self.indicators:
self.indicators[underlying.Symbol.Value] = {"Volatility": self.STD(underlying.Symbol.Value, 240, Resolution.Daily)}
self.indicators[underlying.Symbol.Value]["CloseWindow"] = self.SMA(underlying.Symbol.Value, 1, Resolution.Daily)
# Warm up STD indicator
history = self.History([underlying.Symbol], 240, Resolution.Daily).loc[underlying.Symbol]
for idx, row in history.iterrows():
self.indicators[underlying.Symbol.Value]['Volatility'].Update(idx, row['close'])
for underlying in changes.RemovedSecurities:
self.RemoveSecurity(underlying.Symbol)
for symbol in self.Securities.Keys:
if symbol.SecurityType == SecurityType.Option and symbol.Underlying == underlying.Symbol:
self.RemoveSecurity(symbol)
def OnData(self, slice):
for chain in slice.OptionChains.Values:
# Filter for the first ATM contract
if chain.Contracts.Count < 1:
continue
atmContract = sorted(chain, key = lambda x: abs(x.UnderlyingLastPrice - x.Strike))[0]
impliedVolatility = atmContract.ImpliedVolatility
underlyingSymbol = atmContract.UnderlyingSymbol.Value
if self.indicators[underlyingSymbol]["Volatility"].IsReady:
self.Debug((underlyingSymbol, self.indicators[underlyingSymbol]["Volatility"].current.Value))
def EveryDayAfterMarketOpen(self):
self.dayCounter += 1
self.Debug(f"This is day {self.dayCounter} at {self.Time}")
P Chen
Not sure if this is your same issue but IIRC it had something to do with the ticker change of VXX/VXXB.
My workaround was to add both in Initialise:
self.alternative = [ 'VXX', 'vxx.1', ] for i in self.alternative: self.AddEquity(i, Resolution.Daily) self.vxx = "vxx.1" # "vxx.1" quantconnect permtick for VXX
Then in OnData:
def OnData(self, data): # use vxx.1 before 2019; "VXX" after if self.Time.year < 2019: self.vxx = "vxx.1" else: self.vxx = "VXX"
then reference self.vxx manually in your code.
JayJayD
There is a common misconception respect VXX. VXX didn't become VXXB, VXX was delisted.
When VXX was delisted, it was replaced by a new product with ticker VXXB, And to make the thing even more complex, the security VXXB was renamed to VXX!
The "new" VXX (former VXXB) is different security of the "old" VXX.
This is why if you ask LEAN today to get VXX it will return all data from the "new" VXX i.e., since February 2019.
If you want to retrieve the data from the old VXX you should ask for it using it's permtick (vxx.1).
Oscar Lee
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!