Created with Highcharts 12.1.2EquityJan 3Jan 5Jan 7Jan 9Jan 11Jan 13Jan 15Jan 17Jan 19Jan 21100,000
Overall Statistics
Total Orders
0
Average Win
0%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
0%
Expectancy
0
Start Equity
100000
End Equity
100000
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.479
Tracking Error
0.095
Treynor Ratio
0
Total Fees
$0.00
Estimated Strategy Capacity
$0
Lowest Capacity Asset
Portfolio Turnover
0%
from AlgorithmImports import *

class MSFTOptionPricing(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2024, 1, 1)  
        self.SetEndDate(2024, 1, 19)  
        self.SetCash(100000)  

        self.ticker = "MSFT"
        self.target_timestamp = "2024-01-18 15:00:00"

        # Define target strike prices and expiry date
        self.call_strike = 360
        self.put_strike = 320
        self.expiry_date = datetime(2024, 3, 15)

        # Add underlying asset
        self.underlying = self.AddEquity(self.ticker, Resolution.Hour).Symbol  
        
        # Add options
        option = self.AddOption(self.ticker, Resolution.Hour)
        option.SetFilter(-50, 50, 0, 90)  # Strike range and expiration within 90 days
        self.option_symbol = option.Symbol
        option.PriceModel = OptionPriceModels.CrankNicolsonFD()

    def OnData(self, slice: Slice):
        # Ensure we run this at the specified timestamp
        if self.Time.strftime("%Y-%m-%d %H:%M:%S") != self.target_timestamp:
            return

        self.Debug(f"Fetching option chain data at {self.Time}")

        chain = slice.OptionChains.get(self.option_symbol)
        if not chain:
            return

        # Find the target call and put options
        target_call = next((contract for contract in chain 
                            if contract.Right == OptionRight.Call and 
                            contract.Strike == self.call_strike and 
                            contract.Expiry == self.expiry_date), None)

        target_put = next((contract for contract in chain 
                           if contract.Right == OptionRight.Put and 
                           contract.Strike == self.put_strike and 
                           contract.Expiry == self.expiry_date), None)

        # Print results
        if target_call:
            call_price = self.Securities[target_call.Symbol].Price
            self.Debug(f"CALL | Time: {self.Time}, Strike: {self.call_strike}, Price: {call_price}, IV: {target_call.ImpliedVolatility}")

        if target_put:
            put_price = self.Securities[target_put.Symbol].Price
            self.Debug(f"PUT | Time: {self.Time}, Strike: {self.put_strike}, Price: {put_price}, IV: {target_put.ImpliedVolatility}")