Overall Statistics
Total Trades
4
Average Win
0%
Average Loss
0%
Compounding Annual Return
-8.041%
Drawdown
0.100%
Expectancy
0
Net Profit
-0.088%
Sharpe Ratio
-9.165
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
0.032
Beta
-5.739
Annual Standard Deviation
0.008
Annual Variance
0
Information Ratio
-9.895
Tracking Error
0.009
Treynor Ratio
0.013
Total Fees
$1.00
from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2018, 9, 1)
        self.SetEndDate(2018, 9, 4)
        self.SetCash(100000)
        self.symbols = []
        for ticker in ["IBM", "AAPL", "EBAY", "HDS"]:
            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)