Overall Statistics
Total Trades
1222
Average Win
0.33%
Average Loss
-0.65%
Compounding Annual Return
1.432%
Drawdown
8.600%
Expectancy
0.048
Net Profit
18.933%
Sharpe Ratio
0.286
Probabilistic Sharpe Ratio
0.270%
Loss Rate
31%
Win Rate
69%
Profit-Loss Ratio
0.51
Alpha
0.009
Beta
0.015
Annual Standard Deviation
0.037
Annual Variance
0.001
Information Ratio
-0.459
Tracking Error
0.149
Treynor Ratio
0.688
Total Fees
$1222.00
Estimated Strategy Capacity
$16000.00
Lowest Capacity Asset
SPXW 325H5FB4N52SU|SPX 31
Portfolio Turnover
0.13%
# weekend effect of the selling options
# open - Fri 15:55
# close - Mon 9:45
# delta - 0.25
# 1 contract / account size 100,000 USD

import pandas as pd
import talib as ta
import numpy as np
from AlgorithmImports import *


class BasicTemplateSPXWeeklyIndexOptionsAlgorithm(QCAlgorithm):
    def Initialize(self):
        self.SetStartDate(2011,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(-15, 15)
                                     .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, 55), self.Open)
        self.Schedule.On(self.DateRules.Every(DayOfWeek.Monday), self.TimeRules.At(9, 45), self.End)

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

            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 + 20
                    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 - 20
                    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