Overall Statistics
Total Trades
3
Average Win
0%
Average Loss
0%
Compounding Annual Return
-12.384%
Drawdown
0.400%
Expectancy
0
Net Profit
-0.139%
Sharpe Ratio
-8.092
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.257
Beta
12.303
Annual Standard Deviation
0.011
Annual Variance
0
Information Ratio
-9.887
Tracking Error
0.01
Treynor Ratio
-0.007
Total Fees
$0.75
from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 12, 1)
        self.SetEndDate(2017, 12, 4)
        self.SetCash(100000)
        self.symbols = []
        for ticker in ["IBM", "AAPL", "EBAY"]:
            option = self.AddOption(ticker)
            self.symbols.append(option.Symbol)
            option.SetFilter(-2, +2, timedelta(0), timedelta(60))
            

    def OnData(self,slice):
        option_invested = [x.Key for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]

        for symbol in self.symbols:
            invested = [option for option in option_invested if option.Underlying == symbol.Underlying]
            if len(invested) > 0: return 

            for kvp in slice.OptionChains:
                if kvp.Key == symbol:
                    chain = kvp.Value
                    # we sort the contracts to find at the money (ATM) contract with farthest expiration
                    contracts = sorted(sorted(sorted(chain, 
                        key = lambda x: abs(chain.Underlying.Price - x.Strike)), 
                        key = lambda x: x.Expiry, reverse=True), 
                        key = lambda x: x.Right, reverse=True)
        
                    # if found, trade it
                    if len(contracts) == 0: continue
                    symbol = contracts[0].Symbol
                    if not self.Portfolio[symbol].Invested:
                        self.MarketOrder(symbol, 1)