Overall Statistics
Total Trades
1
Average Win
0%
Average Loss
0%
Compounding Annual Return
-99.985%
Drawdown
31.200%
Expectancy
0
Net Profit
-20.760%
Sharpe Ratio
-0.775
Probabilistic Sharpe Ratio
14.445%
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
3.166
Beta
5.855
Annual Standard Deviation
1.288
Annual Variance
1.659
Information Ratio
-0.257
Tracking Error
1.118
Treynor Ratio
-0.17
Total Fees
$1.00
from datetime import timedelta

class BullCallSpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 1, 22)
        self.SetEndDate(2020, 2, 1)
        self.SetCash(10000)

        equity = self.AddEquity("GOOG", Resolution.Minute)
        equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
        option = self.AddOption("GOOG", Resolution.Minute)
        self.symbol = option.Symbol
    
        # set our strike/expiry filter for this option chain
        option.SetFilter(-7, 7, timedelta(30), timedelta(60))
        # use the underlying equity GOOG as the benchmark
        self.SetBenchmark(equity.Symbol)

    def OnData(self,slice):
        
        optionchain = slice.OptionChains
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            chains = i.Value
            contract_list = [x for x in chains]
        # if there is no contracts in this optionchain, pass the instance
        if (slice.OptionChains.Count == 0) or (len(contract_list) == 0): 
            return   
         # if there is no securities in portfolio, trade the options 
        if not self.Portfolio.Invested: 
            self.TradeOptions(optionchain) 
 
    def TradeOptions(self,optionchain):
    
        for i in optionchain:
            if i.Key != self.symbol: continue
            chain = i.Value
            # sorted the optionchain by expiration date and choose the furthest date
            expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry
            # filter the call options from the contracts expires on that date
            call = [i for i in chain if i.Expiry == expiry and i.Right == 0]
            # sorted the contracts according to their strike prices 
            call_contracts = sorted(call,key = lambda x: x.Strike)    
            if len(call_contracts) == 0: continue
            # call option contract with lower strike
            call_low = call_contracts[0]
            # call option contract with higher strike
            call_high = call_contracts[-1]
            self.Debug(f"{self.Time}: buy spread: {call_low.Expiry} {call_low.Strike} / {call_high.Strike}")
            self.Buy(call_low.Symbol, 1)
            self.Sell(call_high.Symbol ,1)
    
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))