Overall Statistics
Total Trades
21
Average Win
4.75%
Average Loss
0%
Compounding Annual Return
0%
Drawdown
107.700%
Expectancy
0
Net Profit
-112.250%
Sharpe Ratio
-0.234
Probabilistic Sharpe Ratio
13.926%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0
Beta
0
Annual Standard Deviation
4.275
Annual Variance
18.272
Information Ratio
-0.234
Tracking Error
4.275
Treynor Ratio
0
Total Fees
$374.75
Estimated Strategy Capacity
$43000.00
Lowest Capacity Asset
SPXW Y51VSXBQ200E|SPX 31
# This is not a very good strategy.
#
from AlgorithmImports import *



### <summary>
### This example demonstrates how to add and trade SPX index weekly options
### </summary>


pos_size = 0.05      # percent of balance to invest

class BasicTemplateSPXWeeklyIndexOptionsAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2023, 1, 1)
        self.SetEndDate(2023, 2, 14)
        self.SetCash(100000)
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage, AccountType.Margin)


        self.spx = self.AddIndex("SPX", Resolution.Minute)


        # weekly option SPX contracts
        spxw = self.AddIndexOption(self.spx.Symbol, "SPXW")
        # set our strike/expiry filter for this option chain
        spxw.SetFilter(lambda u: (u.Strikes(-25, 25)
                                     .Expiration(0,0)
                                     .IncludeWeeklys()))

        self.spxw_option = spxw.Symbol
        self.day_low = 10000
        self.day_high = 0

        

        #self.AddRiskManagement(MaximumDrawdownPercentPerSecurity(2.0))  # fix stop loss at 200%
        #self.AddRiskManagement(MaximumUnrealizedProfitPercentPerSecurity(1))	# fixed take profit at 10%
        #self.AddRiskManagement(TrailingStopRiskManagementModel(0.05))	# trailing stop at 5%

        self.Schedule.On(self.DateRules.EveryDay(),
                 self.TimeRules.At(16,00,00),
                 self.OnEndOfDay)

    def OnData(self,slice):
        #day high and day low
        current_price = self.spx.Price
        if current_price < self.day_low:
            self.day_low = current_price
        if current_price > self.day_high:
            self.day_high = current_price
        
        ratio = current_price/self.day_high
        #self.Debug(current_price)
        #self.Debug(int(self.day_low))
        #self.Debug(ratio)

        if self.Portfolio.Invested: return
        if self.Time.hour <= 10: return 
        
        delta = 0.05

        chain = slice.OptionChains.GetValue(self.spxw_option)
        if chain is None:
            return
        call = [x for x in chain if x.Right == OptionRight.Call]
        put = [x for x in chain if x.Right == OptionRight.Put]

        # we sort the contracts to find contract with the right delta
        put_contract = sorted(put,key = lambda x: abs(abs(x.Greeks.Delta) - delta))
        call_contract = sorted(call,key = lambda x: abs(x.Greeks.Delta - delta))

        # if found, trade it
        if len(put_contract) == 0: 
            return
        else:
            if current_price <= 1.001*self.day_low:
                put_ = put_contract[0].Symbol
                self.SetHoldings(put_,-pos_size)

        if len(call_contract) == 0: 
            return
        else:
            if current_price >= 0.999*self.day_high:
                call_ = call_contract[0].Symbol
                self.SetHoldings(call_,-pos_size)
     

    def OnOrderEvent(self, orderEvent):
        self.Debug(str(orderEvent))
    
    def OnEndOfDay(self):
        self.day_low = 10000
        self.day_high = 0