Back

How To Keep an Updated Option Universe

Hi Everyone,

  I have been working of getting comfortable with the options backtesting.  I am currently trying to keep my optinos universe updated correctly.  The problem i am experiencing is that the Options universe keeps expanding as time goes on, even though the desired universe should stay fixed ( 5 underlyings ).

  I am using the CoarseSelectionFunction to select the top 5 equities by dollar volume.  The idea is that I want to also have the option chains corresponding to these underylings.  

  I use the AddOption function inside the CoarseSelectionFunction to accomplish this (its probably not the right way to do it).  

def CoarseSelectionFunction(self, coarse):
'''Take the top 5 by dollar volume using coarse'''
# sort descending by daily dollar volume
sortedByDollarVolume = sorted(coarse, \
key=lambda x: x.DollarVolume, reverse=True)

# add Options
for x in sortedByDollarVolume[:5]:
option = self.AddOption(x.Symbol.Value, Resolution.Minute)
option.SetFilter(-2, +2, timedelta(0), timedelta(180))
self.AddEquity(x.Symbol.Value, Resolution.Minute)

# we need to return only the symbol objects
return [ x.Symbol for x in sortedByDollarVolume[:5] ]

  As you can see in the attached backtest, in the Log, each week the number of items in the universe keeps going up. ( the number of symbols in the OPtionChains object increases ).  This is logical as each time through the universe filter new options are added but not removed.

  How am I supposed to keep the option universe up-to-date correctly?

Thanks

Update Backtest








You should manage the options subscriptions at OnSecurityChanged method.
Remove the subscription from the universe selection method:

def CoarseSelectionFunction(self, coarse):
'''Take the top 5 by dollar volume using coarse'''
# sort descending by daily dollar volume
sortedByDollarVolume = sorted(coarse, \
key=lambda x: x.DollarVolume, reverse=True)
# we need to return only the symbol objects
return [ x.Symbol for x in sortedByDollarVolume[:5] ]

And add it to OnSecurityChanged:

def OnSecuritiesChanged (self, changes):
for x in changes.AddedSecurities:
if x.Symbol.SecurityType != SecurityType.Equity: continue
option = self.AddOption(x.Symbol.Value, Resolution.Minute)
option.SetFilter(-2, +2, timedelta(0), timedelta(180))

for x in changes.RemovedSecurities:
if x.Symbol.SecurityType != SecurityType.Equity: continue
# Loop through the securities
# If it is an option and the underlying matches the removed security, remove it
for symbol in self.Securities.Keys:
if symbol.SecurityType == SecurityType.Option and symbol.Underlying == x.Symbol:
self.RemoveSecurity(symbol)

Note: I haven't tested this code yet. It is a suggestion that you can work on instead of waiting for a solution.

1

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.


This looks great, but I just put a debugging message at the top of the OnSecurityChanged function, and this function isn't being triggered at all.  So it ends up not adding any options to the universe.

0

OK yeah sorry I noticed you slightly mispelled the function name, it should be OnSecuritiesChanged.  The other part with a syntax error is this line:

if symbol.SecurityType == SecurityType.Option and symbol.Underlying == s.Symbol:

The variable s isn't defined.  I figured that was supposed to be x but changing s to x resulting is no securites ever being removed from the universe.  I think I'm almost there.

1

Sorry, there are typos: OnSecuritiesChanged and the s for x in line 12. (I edited the original)

1

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.


So the code seems to be working at expected.  I put Logs in there stating which securites the algo is trying to remove, and that code is executing, but you can see but the counter printout, every time through the OnData trading code (once per week) the number of option chains in the slice.OptionChains object keeps increasing... 

0


Update Backtest





0

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.


Loading...

This discussion is closed