Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
-48.863%
Drawdown
0.300%
Expectancy
0
Net Profit
-0.153%
Sharpe Ratio
-11.225
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.386
Beta
27.941
Annual Standard Deviation
0.017
Annual Variance
0
Information Ratio
-12.058
Tracking Error
0.017
Treynor Ratio
-0.007
Total Fees
$0.50
from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

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

    def OnData(self,slice):
        if self.Portfolio.Invested: return
        for symbol in self.symbols:
            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)