Overall Statistics
Total Trades
2
Average Win
0%
Average Loss
0%
Compounding Annual Return
2.739%
Drawdown
0.100%
Expectancy
0
Net Profit
0.058%
Sharpe Ratio
5.553
Loss Rate
0%
Win Rate
0%
Profit-Loss Ratio
0
Alpha
-0.014
Beta
0.041
Annual Standard Deviation
0.004
Annual Variance
0
Information Ratio
-9.862
Tracking Error
0.083
Treynor Ratio
0.506
Total Fees
$2.00
from datetime import timedelta

class BullputSpreadAlgorithm(QCAlgorithm):

    def Initialize(self):
        self.SetStartDate(2017, 5, 1)
        self.SetEndDate(2017, 5, 8)
        self.SetCash(600000)
        equity = self.AddEquity("GOOG", Resolution.Minute)
        option = self.AddOption("GOOG", Resolution.Minute)
        self.symbol = option.Symbol
    
        # set our strike/expiry filter for this option chain
        option.SetFilter(-7, 7, timedelta(30), timedelta(60))
        # use the underlying equity GOOG as the benchmark
        self.SetBenchmark(equity.Symbol)
        
    def OnData(self,slice):
        
        optionchain = slice.OptionChains
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            chains = i.Value
            contract_list = [x for x in chains]
        # if there is no contracts in this optionchain, pass the instance
        if (slice.OptionChains.Count == 0) or (len(contract_list) == 0): 
            return   
         # if there is no securities in portfolio, trade the options 
        if not self.Portfolio.Invested: 
            self.TradeOptions(optionchain)
        else:
            self.CheckProfit(self.put_low,self.put_high)
 
    def TradeOptions(self,optionchain):
    
        for i in optionchain:
            if i.Key != self.symbol: continue
            chain = i.Value
            # sorted the optionchain by expiration date and choose the furthest date
            expiry = sorted(chain,key = lambda x: x.Expiry, reverse=True)[0].Expiry
            # filter the put options from the contracts expires on that date
            put = [i for i in chain if i.Expiry == expiry and i.Right == 1]
            # sorted the contracts according to their strike prices 
            put_contracts = sorted(put,key = lambda x: x.Strike)    
            if len(put_contracts) == 0: continue
            # put option contract with lower strike
            self.put_low = put_contracts[0]
            # put option contract with higher strike
            self.put_high = put_contracts[-1]
            self.Buy(self.put_low.Symbol, 1)
            self.Sell(self.put_high.Symbol ,1)
    
    def CheckProfit(self, buy, sell):
        ## buy.AskPrice is the filled price of the long order. Similarly sell.BidPrice is the filled price of the short order.
        ## Quantity for short order is negative.
        ## self.Securities[buy.UnderlyingSymbol].Price is the current underlying price.
        ## Options contracts are for 100 shares, so multiply by 100
        ## and multiply the number of contracts that will be exercised
        original_investment =  (-buy.AskPrice) * 100 * self.Portfolio[buy.Symbol].Quantity + (-sell.BidPrice ) * 100 * self.Portfolio[sell.Symbol].Quantity
        profit = (-buy.AskPrice + max(buy.Strike - self.Securities[buy.UnderlyingSymbol].Price, 0)) * 100 * self.Portfolio[buy.Symbol].Quantity + \
            (-sell.BidPrice + max(sell.Strike - self.Securities[sell.UnderlyingSymbol].Price, 0)) * 100 * self.Portfolio[sell.Symbol].Quantity
        gain = 100 * profit / original_investment
        self.Log(f'buy.AskPrice = {buy.AskPrice}, sell.BidPrice = {sell.BidPrice}, buy strike = {buy.Strike}, sell strike = {sell.Strike}, underlying price = {self.Securities[sell.UnderlyingSymbol].Price}')
        self.Log(f'profit = {profit}')
        self.Log(f'gain = {gain}%')
        self.Log(f'original_investment = {original_investment}')
        profit2 = self.Portfolio[sell.Symbol].UnrealizedProfit + self.Portfolio[buy.Symbol].UnrealizedProfit
        self.Log(f'profit2 = {profit2}')
        
        
    
    def OnOrderEvent(self, orderEvent):
        self.Log(str(orderEvent))