Overall Statistics
Total Trades
192
Average Win
0.36%
Average Loss
-0.41%
Compounding Annual Return
-2.465%
Drawdown
4.100%
Expectancy
-0.073
Net Profit
-2.922%
Sharpe Ratio
-0.953
Probabilistic Sharpe Ratio
0.596%
Loss Rate
51%
Win Rate
49%
Profit-Loss Ratio
0.89
Alpha
-0.018
Beta
-0.011
Annual Standard Deviation
0.018
Annual Variance
0
Information Ratio
0.335
Tracking Error
0.194
Treynor Ratio
1.509
Total Fees
$192.00
Estimated Strategy Capacity
$54000.00
Lowest Capacity Asset
SPXW 3253D60QQO2GE|SPX 31
Portfolio Turnover
1.07%
import pandas as pd
import talib as ta
import numpy as np
from AlgorithmImports import *


class BasicTemplateSPXWeeklyIndexOptionsAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2022,1, 1)
        #self.SetEndDate(2022, 1, 14)
        self.SetCash(100000)
        self.SetTimeZone(TimeZones.NewYork)
        self.SetBrokerageModel(BrokerageName.InteractiveBrokersBrokerage,AccountType.Margin)
        
        self.spx = self.AddIndex("SPX", Resolution.Minute)
        self.SetBenchmark("SPX")


        # 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(-30, 30)
                                     .Expiration(3,3)
                                     .IncludeWeeklys()))

        self.spxw_option = spxw.Symbol      
        self.trade_run = 0
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Friday), self.TimeRules.At(15, 50), self.Open)
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.At(9, 30), self.End)

 
    def OnData(self,slice):        
        try:
            if self.trade_run ==0:  
                return
            else:
                quantity = 1
                delta =0.5

            if self.call_contract == 1 and self.put_contract ==1:
                return
                    
            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
            call_contract = sorted(call,key = lambda x: abs(x.Greeks.Delta - delta))
            put_contract = sorted(put,key = lambda x: abs(abs(x.Greeks.Delta) - delta))
            
            
            if len(call_contract) == 0: 
                return
            else:
                if self.call_contract == 0:
                    call_ = call_contract[0].Symbol
                    strike = call_contract[0].Strike + 30
                    call_1 = sorted(call, key = lambda x: abs(abs(x.Strike)-strike))
                    call_1 = call_1[0].Symbol
                    
                    self.MarketOrder(call_, -quantity)
                    #self.MarketOrder(call_1, quantity)
                    self.call_contract = 1
            

            if len(put_contract) == 0:
                self.Debug('no put contract')
                return
            else:
                if self.put_contract == 0:
                    put_ = put_contract[0].Symbol
                    self.Debug('okay')
                    strike = put_contract[0].Strike + 30
                    put_1 = sorted(put, key = lambda x: abs(abs(x.Strike)-strike))
                    put_1 = put_1[0].Symbol
                    
                    self.MarketOrder(put_, -quantity)
                    #self.MarketOrder(put_1, quantity)
                    self.put_contract = 1
        except:
            pass

    def OnOrderEvent(self, orderEvent):
        self.Debug(str(orderEvent))
        self.Debug(self.spx.Price)
        self.Debug(self.Time)

    def End(self):
        self.Liquidate()
        self.trade_run = 0
 
    def Open(self):
        self.trade_run = 1
        self.call_contract = 0
        self.put_contract = 0