Hello all,
I am new to this platform and to coding in general. I have been trying to learn by looking at some of the strategies I can relate to from the documentation/library. My goal is to backtest some of the strategies that I currently apply in the options markets, and the one posted below seems to be similar to one that I have been running in the past (screening stocks for alpha).
What I would like to do for starters is to add options to this strategy instead of buying the selected equities outright. This would be great for my backtesting goals, as I then could play around with the different options parameters, such as at the money vs deep in the money, or even out of the money strategies. I have tried to stitch together bits of code from the different tutorials without much luck (as seen below..).
It seems like the strategy below would not necessarily be the easiest to try this options approach with, but as I mentioned - this strategy was one of the few I found in the tutorial that traded "infrequently", as it rebalances per month in-line with the strategy I apply in the markets.
Preferably I would like the strategy to buy options for the securities the strategy selected on a at the money price, with expiration dates around 2-3 months out. It would then re-balance on a monthly basis by liquidating any existing options contracts and purchase new contracts based on the above for the selected securities that the strategy ranks by alpha.
I tried tweaking the code snippet below from one of the latest video-tutorials, but given my limited experience, I am not sure how I can fit it into the strategy.
________________________________________________________________________________________________________________________________
if self.contract is None: self.contract = self.GetContract() return if (self.contract.ID.Date - self.Time).days < 60: self.Liquidate(self.contract) self.RemoveSecurity(self.contract) self.contract = None return if not self.Portfolio[self.contract].Invested: self.SetHoldings(self.contract, 0.5) if self.Securities[self."security"].Price < self.contract.ID.StrikePrice * 1: self.Liquidate(self.contract) self.RemoveSecurity(self.contract) def GetContract(self): targetStrike = (self.Securities[self."selected security"].Price * 1) - (self.Securities[selected security"].Price * 1)%5 contracts = self.OptionChainProvider.GetOptionContractList(self."selected security", self.Time) calls = [x for x in contracts if x.ID.OptionRight == OptionRight.Put] calls = sorted( sorted(calls, key = lambda x: x.ID.Date, reverse = True), key = lambda x: x.ID.StrikePrice) calls = [x for x in calls if x.ID.StrikePrice == targetStrike] calls = [x for x in calls if 270 < (x.ID.Date - self.Time).days <= 60] if len(calls) == 0: return None self.AddOptionContract(calls[0], Resolution.Minute) return calls[0]____________________________________________________________________________________________________________________________ I hope someone in the community can provide some kind help on this. Thank you in advance.Best,Alice.
Ernest Shaggleford
Hi Alice,
I'd suggest reviewing all the options related samples, examples and templates in Lean to get a thorough understanding of applying option universes, filtering, chain selection, contract selection, symbol management, etc.
I'd suggest then building small prototype algos for experimenting and building experience. Also consider the implementation language.
Then once you have a good handle on this, build up the algo in stages, using a framework that suits the purpose of the algo, and then incrementally add functionality.
Best,
ES.
Lauren Roberts
Hi Alice,
I'd suggest starting in Jupyter so that you can visualize how options data is stored and accessed. As a beginner, starting with options contracts might be more complicated than you'd like.
Alice Lee
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!