Hi everyone,
I have an also that dynamically selects a few large cap stocks daily. Out of those, a few calculations are made to select 1 stock that I like. Then, at 9h33 AM, I get and filter all the options of that stock with this code:
def my_options(self):
self.Debug("%%%%%%%%%%%%% my_options {}".format(self.Time))
for sid in self.buyList:
contracts = self.OptionChainProvider.GetOptionContractList(sid, self.Time.date())
self.underlyingPrice = self.Securities[sid].Price
self.otm_calls = [i for i in contracts if i.ID.OptionRight == OptionRight.Call and
i.ID.StrikePrice - self.underlyingPrice > 0 and
15 < (i.ID.Date - self.Time).days < 45]
if len(self.otm_calls) > 0:
contract = sorted(sorted(self.otm_calls, key = lambda x: x.ID.Date),
key = lambda x: x.ID.StrikePrice - self.underlyingPrice, reverse=True)
if len(contracts) > 0:
self.AddOptionContract(contracts[0], Resolution.Minute)
self.Debug(contracts[0].Value)
self.SetHoldings(contract[0], 0.1)
That code generates this error:
Runtime Error: In Scheduled Event 'EveryDay: 9.53', ValueError : operands could not be broadcast together with shapes (8,2) (0,0) ValueError : operands could not be broadcast together with shapes (8,2) (0,0) (Open Stacktrace)
which I beleive is caused by this: self.AddOptionContract(contracts[0], Resolution.Minute)
if I run the code without this line, I get this error:
307788 | 10:47:54:
%%%%%%%%%%%%% my_options 2011-02-02 09:33:00
307789 | 10:47:54:
DGX 110219C00022500
307791 | 10:47:54:
Backtest Handled Error: DGX UU9HR896NECM|DGX R735QTJ8XC9X not found in portfolio. Request this data when initializing the algorithm.
Just wondering what I am missing.
Again, I couldnt find any documentation on selecting options dynamically.
Thanks a lot in advance !
Jack Simonson
Hi Stephane,
Could you attach a backtest of your algorithm? Even if you need to comment out some code to complete a backtest that can be attached, it would be immensely helpful to get a complete picture of your code, especially since the error stems, in part, from a Scheduled Event.
Stephane b
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!