Hello,
I am trying to us the option chain provider to to give me all options contracts that exist for a given underlying at the algorithm time. I then want to sort these contracts by those with an expiration date on the coming Friday. I then want to subscribe to all these contracts and add them to my universe. However, when I try to do math on my list of contracts that I am supposed to be subscribed to, I get an error saying that the list of contracts is empty. What is the correct way to subscribe to these contracts? Code attached.
Thanks,
Ian
import datetime
import numpy as np
class QuantumOptimizedCoreWave(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2019, 11, 29) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
self.equity = self.AddEquity("SPY", Resolution.Minute)
self.equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
def OnData(self, data):
contracts = self.OptionsFilter(data)
for contract in contracts:
if contract.Right == 1:
strike_prices = np.append(strike_prices, contract.Strike)
call_open_interest = np.append(call_open_interest, contract.OpenInterest)
if contract.Right == 0:
put_open_interest = np.append(put_open_interest, contract.OpenInterest)
put_minimum_OI = np.argmin(put_open_interest) # I am getting an argmin error on an empty list
def OptionsFilter(self, data):
selected = []
contracts = self.OptionChainProvider.GetOptionContractList(self.equity.Symbol, data.Time) # Gets every options contract available for the underlying symbol at that time
d = self.Time
while d.weekday() != 4: # Trying to find contracts with expiration on the coming Friday
d += datetime.timedelta(1)
# d = d + timedelta(7) # finds the contracts expiring in two weeks instead of one
for contract in contracts:
if contract.Expiry == d:
selected.append(contract) # Supposed to append contracts that expire the coming Friday
for contract in selected:
self.AddOptionContract(contract, Resolution.Minute) # Supposed to subscribe to all contracts in list
return selected # Supposed to return the list of selected contracts
Apollos Hill
Check here.
https://www.quantconnect.com/docs/data-library/options#Options-IntroductionThere are also code templates on the github . www.github.com/quantconnect
Ian Hamilton
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!