| Overall Statistics |
|
Total Orders 2 Average Win 9.61% Average Loss 0% Compounding Annual Return 4.274% Drawdown 8.500% Expectancy 0 Start Equity 100000 End Equity 109608 Net Profit 9.608% Sharpe Ratio -0.224 Sortino Ratio -0.089 Probabilistic Sharpe Ratio 12.614% Loss Rate 0% Win Rate 100% Profit-Loss Ratio 0 Alpha -0.042 Beta 0.241 Annual Standard Deviation 0.09 Annual Variance 0.008 Information Ratio -0.934 Tracking Error 0.119 Treynor Ratio -0.084 Total Fees $0.00 Estimated Strategy Capacity $12000000.00 Lowest Capacity Asset SPX Y63ASX4ISXV2|SPX 31 Portfolio Turnover 0.02% |
from AlgorithmImports import *
class IndexOptionBuyAndHold(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2023, 1, 1) # Set start date
self.SetCash(100000) # Set initial cash
self.underlying = "SPX" # Underlying index (S&P 500 Index)
# Add index and its options
self.index = self.AddIndex(self.underlying)
self.option = self.AddIndexOption(self.underlying)
# Corrected SetFilter syntax
self.option.SetFilter(-10, 10, 30, 60)
self.bought = False # Track if we have bought an option
def OnData(self, data):
if self.bought or self.Portfolio.Invested:
return # Ensure we buy only once
# Get option chain
chain = data.OptionChains.get(self.option.Symbol, None)
if not chain:
return
# Select the nearest expiry and at-the-money (ATM) call option
contracts = sorted(chain, key=lambda x: (x.Expiry, abs(x.Strike - chain.Underlying.Price)))
if not contracts:
return
selected_contract = contracts[0] # Choose the best contract
# Place a market order to buy 1 contract
self.MarketOrder(selected_contract.Symbol, 1)
self.bought = True # Ensure we don't buy again