Back

Accessing options contracts

 

for kvp in slice.OptionChains:
    chain = kvp.Value   # option contracts for each 'subscribed' symbol/key 
    traded_contracts = filter(lambda x: x.Symbol in [self.this_option_symbol], chain)
for this_option in traded_contracts:
# Do stuff with this_option

 

There must be an easier way to access contracts from the options chain than have to use two for loops? Simply looking up the stock and specific option symbols as if they were dictionary objects doesn't appear to work. slice.OptionChains['stock_symbol'].contracts['options_symbol']

There's a link here for a github pull request that adds a similar kind of functionality but this doesn't appear in the docs. Or at least not in the docs that I've found? https://github.com/QuantConnect/Lean/pull/2615

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.


Hi! You've done the hard work of finding the pull request - if you'd also looked at the actual files changed, you would have found that the example options algorithm file (line #51) makes use of this functionality :) .

def OnData(self,slice):
if self.Portfolio.Invested: return

chain = slice.OptionChains.GetValue(self.option_symbol)
if chain is None:
return

# we sort the contracts to find at the money (ATM) contract with farthest expiration
contracts = sorted(sorted(sorted(chain, \
key = lambda x: abs(chain.Underlying.Price - x.Strike)), \
key = lambda x: x.Expiry, reverse=True), \
key = lambda x: x.Right, reverse=True)

# if found, trade it
if len(contracts) == 0: return
symbol = contracts[0].Symbol
self.MarketOrder(symbol, 1)

 

1

Cool, that looks like it should work! Thanks for pointing that out.

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