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

class HipsterGreenHippopotamus(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2022, 9, 27)
        self.SetCash(100000000)
        self.underlying = self.AddIndex("SPX", Resolution.Minute).Symbol
        
        self.SetSecurityInitializer(MySecurityInitializer(self, self.BrokerageModel, FuncSecuritySeeder(self.GetLastKnownPrices)))
        
        self.Schedule.On(
                    self.DateRules.EveryDay('SPX'), 
                    self.TimeRules.BeforeMarketClose('SPX', 60), 
                    self.get_greeks
                    )
        
    def get_greeks(self):
        contracts = list(self.OptionChainProvider.GetOptionContractList(self.underlying, self.Time))
        contract = contracts[0]
        option = self.AddIndexOptionContract(contract, Resolution.Hour)
        for bar in self.History[TradeBar](contract, 4, Resolution.Hour):
            option.SetMarketPrice(bar)
        
        # EvaluatePriceModel
        contract_data = OptionContract(option, self.underlying)
        contract_data.Time = self.Time
        # None below is the Slice object. It's not used.
        result = option.EvaluatePriceModel(None, contract_data)
        iv = result.ImpliedVolatility
        greeks = result.Greeks
        delta = greeks.Delta
        self.Plot('Delta', '-', delta)

        # Alex: We don't need the following to calculate the greeks, 
        # since `contract_data` has their data
        price = option.Price
        underlying_price = option.Underlying.Price

        #Subscription and unsubscription example
        option = self.RemoveOptionContract(contract)

        option = self.AddIndexOptionContract(contract, Resolution.Hour)

        underlying_price = option.Underlying.Price
        price = option.Price

        assert self.Securities[contract].Price == 0
        self.MarketOrder(contract, 1)

        self.Plot('Delta', '-', delta)




        # ToDo: Get the option Greeks without OnData. Is it possible? (since we can obtain the price immediately)    



# Outside of the algorithm class
class MySecurityInitializer(BrokerageModelSecurityInitializer):

    def __init__(self, algorithm, brokerage_model: IBrokerageModel, security_seeder: ISecuritySeeder) -> None:
        super().__init__(brokerage_model, security_seeder)
        self.algorithm = algorithm
        self.symbols = []

    def Initialize(self, security: Security) -> None:
        # First, call the superclass definition
        # This method sets the reality models of each security using the default reality models of the brokerage model
        super().Initialize(security)
        if security.symbol.value in self.symbols:
            return
        self.symbols.append(security.symbol.value)