Overall Statistics
Total Trades
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Net Profit
0%
Sharpe Ratio
0
Probabilistic Sharpe Ratio
0%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
0
Annual Variance
0
Information Ratio
0
Tracking Error
0
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
class RetrospectiveTanAntelope(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2021, 4, 1)  # Set Start Date
        self.SetCash(100000)  # Set Strategy Cash
        
        self.SetSecurityInitializer(self.security_initializer)
        self.symbol = self.AddEquity("TSLA", Resolution.Minute).Symbol

    def security_initializer(self, security):
        if security.Type == SecurityType.Equity:
            security.SetDataNormalizationMode(DataNormalizationMode.Raw)
        elif security.Type == SecurityType.Option:
            security.SetMarketPrice(self.GetLastKnownPrice(security))
        

    def OnSecuritiesChanged(self, data):
        self.SellCall()
        self.BuyPut()
        self.Quit()
    
    def SellCall(self):
        contracts = self.OptionChainProvider.GetOptionContractList(self.symbol, self.Time)
        self.Debug(f"SellCall: {len(contracts)}")
        if len(contracts) == 0: return
        min_expiry = 0
        max_expiry = 40
        
        filtered_contracts = [i for i in contracts if min_expiry <= (i.ID.Date.date() - self.Time.date()).days <= max_expiry]
        call = [x for x in filtered_contracts if x.ID.OptionRight == 0] 
        
        if len(call) == 0: return
        # sorted the contracts according to their expiration dates and choose the ATM options
        price = self.Securities[self.symbol].Price
        self.contract = sorted(sorted(call, key = lambda x: abs(price - x.ID.StrikePrice)), 
                                        key = lambda x: x.ID.Date, reverse=True)[0]
      
        self.AddOptionContract(self.contract, Resolution.Minute)
        self.MarketOrder(self.contract, -1)

    
    def BuyPut(self):
        contracts = self.OptionChainProvider.GetOptionContractList(self.symbol, self.Time)
        self.Debug(f"BuyPut: {len(contracts)}")
        #contracts = self.OptionChainProvider.GetOptionChains(self.Symbol, self.Time.date())
        if len(contracts) == 0: return
        min_expiry = 0
        max_expiry = 40
        
        filtered_contracts = [i for i in contracts if min_expiry <= (i.ID.Date.date() - self.Time.date()).days <= max_expiry]
        put = [x for x in filtered_contracts if x.ID.OptionRight == 1] 
        
        if len(put) == 0: return
        price = self.Securities[self.symbol].Price
        # sorted the contracts according to their expiration dates and choose the ATM options
        self.contract = sorted(sorted(put, key = lambda x: abs(price - x.ID.StrikePrice)), 
                                        key = lambda x: x.ID.Date, reverse=True)[0]
      
        self.AddOptionContract(self.contract, Resolution.Minute)
        self.MarketOrder(self.contract, 1)