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.