Overall Statistics
Total Trades
4
Average Win
2.35%
Average Loss
0%
Compounding Annual Return
93.204%
Drawdown
1.700%
Expectancy
0
Net Profit
4.740%
Sharpe Ratio
9.967
Probabilistic Sharpe Ratio
99.989%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.871
Beta
-0.059
Annual Standard Deviation
0.08
Annual Variance
0.006
Information Ratio
-3.893
Tracking Error
0.116
Treynor Ratio
-13.575
Total Fees
$2.00
from datetime import timedelta
class OptionBot1(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 8, 3)
        self.SetEndDate(2020,8,28)
        self.SetCash(10000)
        self.spy = self.AddEquity("SPY", Resolution.Minute)
        self.option = self.AddOption("SPY",Resolution.Minute)
        self.symbol = self.option.Symbol
        self.option.SetFilter(-2,-1,timedelta(3),timedelta(7))
        self.SetBenchmark(self.spy.Symbol)
        
        self.options_sold = 0
        self.equities_sold = 0
        
    def OnData(self, data):
        for kvp in data.OptionChains:
            if kvp.Key != self.symbol:
                continue
            optionchain = kvp.Value
            put = [x for x in optionchain if x.Right == OptionRight.Put]
            price = optionchain.Underlying.Price
            # choose ITM contracts
            contracts = [x for x in put if price - x.Strike > 0]
            contracts = sorted(contracts, key = lambda x: x.Expiry, reverse = True)
            if len(contracts) == 0: 
                continue
            symbol = contracts[0].Symbol
            self.Sell(symbol, 1)
             
            
        self.Plot('Custom', 'Options Profit', sum([x.UnrealizedProfit for x in self.Portfolio.Values if x.Type == SecurityType.Option]))
        self.Plot('Custom2', 'Assets Held', len([x for x in self.Portfolio.Values if x.Invested]))
        # self.Plot('Custom2', 'Non-options Assets Held', len([x for x in self.Portfolio.Values if x.Invested and x.Type != SecurityType.Option]))
        
    def OnOrderEvent(self, orderEvent):
        if orderEvent.Symbol.SecurityType == SecurityType.Option:
            self.options_sold += -orderEvent.FillQuantity
            self.Log(f'Options Sold: {self.options_sold}')