Overall Statistics
Total Trades
3
Average Win
0.19%
Average Loss
0%
Compounding Annual Return
7.342%
Drawdown
0.200%
Expectancy
0
Net Profit
0.386%
Sharpe Ratio
3.902
Loss Rate
0%
Win Rate
100%
Profit-Loss Ratio
0
Alpha
0.027
Beta
0.107
Annual Standard Deviation
0.016
Annual Variance
0
Information Ratio
-2.066
Tracking Error
0.124
Treynor Ratio
0.567
Total Fees
$1.25
from datetime import timedelta

class BasicTemplateOptionsAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 01, 01)
        self.SetEndDate(2017, 01, 22)
        self.SetCash(100000)
        equity = self.AddEquity("IBM", Resolution.Minute)
        option = self.AddOption("IBM", Resolution.Minute)
        self.symbol = option.Symbol
        self.expiry_start = 0

        # set our strike/expiry filter for this option chain
        option.SetFilter(-3, +3, timedelta(0), timedelta(20))
        # use the underlying equity as the benchmark
        self.SetBenchmark(equity.Symbol)
        self.call = "IBM" # Initialize the call contract
        
        
    def OnData(self,slice):
        
        
        if not self.Portfolio[self.call].Invested and self.Time.hour != 0 and self.Time.minute == 1:
            self.TradeOptions(slice) 
        
        # if the option contract expires, start to get the price at the expiry     
        if slice.Delistings.Count > 0:
            if [x.Key == self.call for x in slice.Delistings] and self.Time.hour == 0:
                self.expiry_start = 1
                self.price_at_expiry = []
                
                
        if self.expiry_start == 1:
            self.price_at_expiry.append(self.Securities[self.call].AskPrice)
            if len(self.price_at_expiry) == 6.5 * 60:
                self.Log("contract price at the expiry" + str(self.price_at_expiry))
                self.expiry_start = 0


    def TradeOptions(self,slice):
  
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            chain = i.Value
            call = [x for x in chain if x.Right == 0] # filter the call options contracts
            # sorted the contracts according to their expiration dates and choose the ATM options
            contracts = sorted(sorted(call, key = lambda x: abs(chain.Underlying.Price - x.Strike)), 
                                            key = lambda x: x.Expiry, reverse=True)
    
            if len(contracts) == 0: return    
            contract = contracts[0] 
            self.call = contract.Symbol
            self.Sell(self.call, 1) 
            if self.Portfolio["IBM"].Quantity == 0:
                self.Buy("IBM",100)