Back

Option selection based on dynamic stock selection

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 !

 

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.


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

 
";