Overall Statistics
Total Trades
15
Average Win
1.34%
Average Loss
-2.62%
Compounding Annual Return
-48.954%
Drawdown
10.600%
Expectancy
-0.568
Net Profit
-10.436%
Sharpe Ratio
-2.334
Loss Rate
71%
Win Rate
29%
Profit-Loss Ratio
0.51
Alpha
-0.519
Beta
0.014
Annual Standard Deviation
0.222
Annual Variance
0.049
Information Ratio
-1.654
Tracking Error
0.347
Treynor Ratio
-36.146
Total Fees
$3.25
from datetime import timedelta
import pandas as pd
import numpy as np


class BasicTemplateOptionsAlgorithm(QCAlgorithm):
 
    def Initialize(self):
        self.SetStartDate(2018, 1, 1) #Set algorithm start date
        self.SetEndDate(2018, 3, 1) #Set algorithm end date
        self.SetCash(50000) # Set strategy cash

        equity = self.AddEquity("GOOG", Resolution.Minute) # Add the underlying stock: Google
        option = self.AddOption("GOOG", Resolution.Minute) # Add all the option contracts corresponding to underlying stock
        self.symbol = option.Symbol # Get the symbol of the underlying asset

        ''' 
            If the market price of GOOG is S  
            Since the strike of GOOG options is spaced $2.5
            If the ATM option strike is K
            SetFilter choose the GOOG option contracts with strike price between (K-10,S+10) 
            and time to expiration between 0 to 90 days 
        '''
        option.SetFilter(0, +5, timedelta(0), timedelta(30))
        # use the underlying equity as the benchmark
        self.SetBenchmark(equity.Symbol)


    def OnData(self,slice):
        if self.Portfolio.Invested: return
    
        for i in slice.OptionChains:
            if i.Key != self.symbol: continue
            optionchain = i.Value 
            # print out the undelying price
            self.Log("underlying price:" + str(optionchain.Underlying.Price))
            # create a data frame to show the filtered contracts
            df = pd.DataFrame([[x.Right,float(x.Strike),x.Expiry,float(x.BidPrice), float(x.AskPrice), float(x.ImpliedVolatility)] for x in optionchain],
                              index=[x.Symbol.Value for x in optionchain],
                              columns=['type(call 0, put 1)', 'strike', 'expiry', 'ask price', 'bid price','implied volitility'])
                              
            
            self.Log(str(df))

            # we sort the contracts to find at the money (ATM) contract with farthest expiration
            contracts = sorted(sorted(optionchain, key = lambda x: abs(optionchain.Underlying.Price - x.Strike)),key = lambda x: x.Expiry, reverse=True)
            # buy the option with farthest expiration and sell it when the exchange closes
            if len(contracts) == 0: continue
            symbol = contracts[0].Symbol   
            self.MarketOrder(symbol, 1)
            self.MarketOnCloseOrder(symbol, -1)

    def OnOrderEvent(self, orderEvent):
        # print out the order details
        self.Log(str(orderEvent))