Overall Statistics
from datetime import timedelta

class shortAndTrackOptionExample(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2016, 1, 1)
        self.SetEndDate(2016, 1, 10)
        self.SetCash(100000)

        option = self.AddOption("GOOG",Resolution.Minute)
        self.option_symbol = option.Symbol

        # set our strike/expiry filter for this option chain
        option.SetFilter(-2, +2, timedelta(0), timedelta(180))
        
    def OnData(self,slice):
        if not self.Portfolio.Invested:
            for i in slice.OptionChains:
                chain = i.Value
                put = [i for i in chain if i.Right == OptionRight.Call]
                contracts =  sorted(sorted(put, key=lambda x: x.Expiry, reverse=False), 
                                                key=lambda x: abs(x.UnderlyingLastPrice - x.Strike))
                atm_contract = contracts[0]                
                
                # Sell ATM Call Option Contract
                if len(contracts) == 0: continue
                symbol = contracts[0].Symbol
                self.MarketOrder(symbol, -1)
                
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))
        
    def OnEndOfDay(self):
        # Get list of option contracts in portfolio
        option_invested = [x.Key.Value for x in self.Portfolio if x.Value.Invested and x.Value.Type==SecurityType.Option]
        # Print to log
        for contract in option_invested:
            quantity = self.Portfolio[contract].Quantity
            lastPrice = self.Securities[contract].Price
            self.Log("Contract: " + str(contract) + " - Quantity: " + str(quantity) + " - Last Price: " + str(lastPrice))