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
Lowest Capacity Asset
from AlgorithmImports import *

class OptionTest(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 2, 7)
        self.SetEndDate(2022, 2, 8)
        self.SetCash(500000)
        ticker = 'SPY'
        equity = self.AddEquity(ticker, Resolution.Daily) # Add the underlying stock:        
        self.symbol = equity.Symbol

    def OnData(self,data):

        if not self.Portfolio.Invested:
            price = data[self.symbol].Close
            expiry = self.Time + timedelta(0)

            contracts = self.OptionChainProvider.GetOptionContractList(self.symbol, self.Time)

            calls = [symbol for symbol in contracts if symbol.ID.OptionRight == OptionRight.Call]

            calls_expiry_sorted = sorted(calls, key=lambda p: abs(p.ID.Date - expiry), reverse=False)

            try:

                closest_expiry = calls_expiry_sorted[0].ID.Date
      
                calls_expiry_filtered = [contract for contract in calls_expiry_sorted if contract.ID.Date == closest_expiry]

                calls_sorted = sorted(calls_expiry_filtered, key=lambda p: abs(p.ID.StrikePrice - price), reverse=False)
            
                call_sell = calls_sorted[0]
                call_buy = calls_sorted[1]

                self.Log ('underlying price = ' + str (price))
                     
                self.Log (call_sell)
                self.Log (str (call_sell.Value))
                self.Log (call_buy)
                self.Log (str (call_buy.Value))

                try:
                    self.MarketOrder(call_buy, 1)                
                except Exception as e:
                        self.Log (e)

                try:
                    self.MarketOrder(call_sell, -1)                 
                except Exception as e:
                    self.Log (e)

            except Exception as e:
                self.Log (e)