Do we have index options data available now for SPX? The data managers shows it's currently trading but after having tried numerous dates and filters, i'm not getting chains into OnData. Works for GOOG etc.
from clr import AddReference
AddReference("System")
AddReference("QuantConnect.Algorithm")
AddReference("QuantConnect.Common")
from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from datetime import timedelta
### <summary>
### This example demonstrates how to add options for a given underlying equity security.
### It also shows how you can prefilter contracts easily based on strikes and expirations, and how you
### can inspect the option chain to pick a specific option contract to trade.
### </summary>
### <meta name="tag" content="using data" />
### <meta name="tag" content="options" />
### <meta name="tag" content="filter selection" />
class BasicTemplateOptionsAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2017, 12, 1)
self.SetEndDate(2018, 1, 8)
self.SetCash(100000)
option = self.AddOption("SPX")
self.option_symbol = option.Symbol
# set our strike/expiry filter for this option chain
option.SetFilter(-100, +100, timedelta(0), timedelta(180))
# use the underlying equity as the benchmark
self.SetBenchmark("SPX")
def OnData(self,slice):
if self.Portfolio.Invested: return
for kvp in slice.OptionChains:
if kvp.Key != self.option_symbol: continue
chain = kvp.Value
# 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: continue
for c in contracts:
self.Log("contract: {0} strike: {1} right: {2} expiry: {3} open interest: {4} last price: {5} bid: {6} bid size: {7} ask: {8} ask size: {9}".format(str(c), c.Strike, c.Right, c.Expiry, c.OpenInterest, c.LastPrice, c.BidPrice, c.BidSize, c.AskPrice, c.AskSize))
symbol = contracts[0].Symbol
self.MarketOrder(symbol, 1)
self.MarketOnCloseOrder(symbol, -1)
def OnOrderEvent(self, orderEvent):
self.Log(str(orderEvent))