Overall Statistics
Total Trades
3
Average Win
0%
Average Loss
0%
Compounding Annual Return
-3.898%
Drawdown
0.400%
Expectancy
0
Net Profit
-0.259%
Sharpe Ratio
-3.384
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.047
Beta
1.127
Annual Standard Deviation
0.009
Annual Variance
0
Information Ratio
-5.106
Tracking Error
0.009
Treynor Ratio
-0.026
Total Fees
$0.75
from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 10, 1)
        self.SetEndDate(2018, 10, 24)
        self.SetCash(100000)
        self.symbols = []
        for ticker in ["IBM", "AAPL", "EBAY"]:
            option = self.AddOption(ticker)
            self.symbols.append(option.Symbol)
            option.SetFilter(-3, +3, 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
                    for x in chain:
                        if x.Right == 0:
                            self.Log(str(x.Symbol.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 self.Portfolio[symbol].Invested: return
                    self.MarketOrder(symbol, 1)