Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
1000000
End Equity
1000000
Net Profit
0%
Sharpe Ratio
0
Sortino 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
-2.539
Tracking Error
0.089
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
# region imports
from AlgorithmImports import *
# endregion

class HistoricalOptionGreek(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2024, 1, 1)
        self.set_end_date(2024, 7, 1)
        self.set_cash(1000000)

        # Use the security initializer to get the last known price so we can place orders after we add the option contract
        self.set_security_initializer(BrokerageModelSecurityInitializer(self.brokerage_model, FuncSecuritySeeder(self.get_last_known_prices)))

        # Adds the SPX and create the chain symbol 
        spx = self.add_index("SPX").symbol
        self.index_option_symbol = Symbol.create_canonical_option(spx, "SPXW", Market.USA, "?SPXW")

        # We will check for trades every day, so we open new trades after the option expires
        self.schedule.on(self.date_rules.month_start(spx), self.time_rules.after_market_open(spx, 1), self.trade)
    
    def trade(self):
        # Get all the contracts available for the day
        option_contract_list = self.option_chain_provider.get_option_contract_list(self.index_option_symbol, self.time)

        # Filter the OTM puts with expiry to one month
        expiry = min(option_contract_list, key=lambda symbol: abs((symbol.id.date - self.time).days - 30)).id.date
        underlying_price = self.securities[self.index_option_symbol.underlying].price
        puts = filter(lambda symbol: symbol.id.option_right == OptionRight.PUT and symbol.id.date == expiry and symbol.id.strike_price < underlying_price, option_contract_list)

        # Add the contract to calculate the delta
        deltas_by_symbol = {}
        for put in puts:
            call = Symbol.create_option(put.id.underlying.symbol, "SPXW", put.id.market, put.id.option_style, OptionRight.CALL, put.id.strike_price, put.id.date)
            deltas = self.indicator_history(Delta(option=put, mirror_option=call), [call, put, call.Underlying], 24, Resolution.HOUR)
            if deltas.count == 0:
                continue
            delta = list(deltas)[-1].current.value
            deltas_by_symbol[put] = delta

        # Select ATM Put (delta of -0.5)
        put = sorted(deltas_by_symbol.items(), key=lambda x: abs(x[1]+.50))[0][0]
        self.add_option_contract(put)