Hey, anyone know how to access the strike price off a contract?
I'm using the following option chain provider and it logs the contract at the end. I was hoping to pull the strike price from the contract info and save the strike price to a list so if the price of my underlying approached my strike price I can make an adjustment.
Thanks in advance!
def OptionsFilterqqqcall(self, data):
''' OptionChainProvider gets a list of option contracts for an underlying symbol at requested date.
Then you can manually filter the contract list returned by GetOptionContractList.
The manual filtering will be limited to the information included in the Symbol
(strike, expiration, type, style) and/or prices from a History call '''
callcontractsqqq = self.OptionChainProvider.GetOptionContractList(self.symbolqqq, data.Time)
self.underlyingPriceqqq = self.Securities[self.symbolqqq].Price
# filter the out-of-money put options from the contract list which expire close to self.DTE num of days from now
qqqotm_calls = [i for i in callcontractsqqq if i.ID.OptionRight == OptionRight.Call and
i.ID.StrikePrice > self.CallOTM * self.underlyingPriceqqq and
(i.ID.Date - data.Time).days < self.CallDTE]
if len(qqqotm_calls) > 0:
# sort options by closest to self.DTE days from now and desired strike, and pick first
callqqqcontract = sorted(sorted(qqqotm_calls, key = lambda x: abs(x.ID.StrikePrice - self.underlyingPriceqqq)))[0]
if callqqqcontract not in self.callcontractsAddedqqq:
self.callcontractsAddedqqq.add(callqqqcontract)
# use AddOptionContract() to subscribe the data for specified contract
self.AddOptionContract(callqqqcontract, Resolution.Minute)
self.Log("QQQ Call contract {0}".format(callqqqcontract))
return callqqqcontract
else:
return str()
Mamun
My code might help, take a look at the Backtest code
Mamun
Or may be you are just looking for this?
David Eldringhoff
Yeah seems like that would work but it keeps throwing errors.
In my code I get an option from the options filter and it logs the option at the end:
QQQ Call contract QQQ 230112C00283000
It even has the strike price in that log, but I don't know how to pull it out. If I add your code to the end it gives an error:
Runtime Error: 'Symbol' object is not iterable
at OptionsFilterqqqcall
strikes = [x.ID.StrikePrice for x at callqqqcontract]
in main.py: line 144
at Buyqqqcall
self.callcontractqqq = self.OptionsFilterqqqcall(data)
in main.py: line 111
at OnData
self.Buyqqqcall(data)
at Python.Runtime.PythonException.ThrowLastAsClrException()
at Python.Runtime.PyObject.Invoke(PyTuple args in main.py: line 99
I also have this code up in OnData which loads in the short call contract and logs it as the following:
[<QuantConnect.Symbol object at 0x7fed3d0dfa80>]
If I try to pull the strike off of it the same way I get a similar error.
I cant figure out how to pull that dang strike price (in the form of a whole number) out of QQQ 230112C00283000
Mamun
Looks like your following code is not an array, you are just picking one or the first object or contract
if you want iterable then use it
in this way you will have an array of contracts, and now
that should work. Hope that helps
David Eldringhoff
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!