Overall Statistics
Total Trades
12
Average Win
3.61%
Average Loss
0%
Compounding Annual Return
129.941%
Drawdown
11.200%
Expectancy
0
Net Profit
23.540%
Sharpe Ratio
6.545
Probabilistic Sharpe Ratio
98.817%
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
1.069
Beta
0.175
Annual Standard Deviation
0.183
Annual Variance
0.034
Information Ratio
1.943
Tracking Error
0.235
Treynor Ratio
6.847
Total Fees
$6.00
from datetime import timedelta
class OptionBot1(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2020, 6, 1)
        self.SetEndDate(2020,9,1)
        self.SetCash(10000)
        self.spy = self.AddEquity("SPY", Resolution.Minute)
        self.option = self.AddOption("SPY",Resolution.Minute)
        self.option.ContractMultiplier = 1
        self.symbol = self.option.Symbol
        self.option.SetFilter(-2,0,timedelta(3),timedelta(7))
        self.SetBenchmark(self.spy.Symbol)
        self.options_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
            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('Multiplier', 'Multiplier', self.option.ContractMultiplier)
            self.Plot('Options Profit', 'Options Profit', sum([x.UnrealizedProfit for x in self.Portfolio.Values if x.Type == SecurityType.Option]))
            self.Plot('Assets Held', 'Assets Held', len([x for x in self.Portfolio.Values if x.Invested]))
            self.Plot('Non-options Assets Held', 'Non-options Assets Held', len([x for x in self.Portfolio.Values if x.Invested and x.Type != SecurityType.Option]))  
            self.Plot('Options Sold', 'Options Sold', self.options_sold)  
            self.Plot('Option Assets Held', 'Option 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}')