Overall Statistics
Total Trades
22
Average Win
0.27%
Average Loss
-0.04%
Compounding Annual Return
-0.328%
Drawdown
0.300%
Expectancy
-0.222
Net Profit
-0.087%
Sharpe Ratio
-0.602
Loss Rate
91%
Win Rate
9%
Profit-Loss Ratio
7.56
Alpha
0.022
Beta
-1.386
Annual Standard Deviation
0.005
Annual Variance
0
Information Ratio
-4.136
Tracking Error
0.005
Treynor Ratio
0.002
Total Fees
$22.00
import numpy as np
from datetime import timedelta
import datetime

class BasicTemplateAlgorithm(QCAlgorithm):
    '''Basic template algorithm simply initializes the date range and cash'''

    def Initialize(self):
        '''Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.'''

        self.SetStartDate(2017, 1, 1)  #Set Start Date
        self.SetEndDate(2017,4, 7)    #Set End Date
        self.SetCash(100000)           #Set Strategy Cash
        # Find more symbols here: http://quantconnect.com/data
        self.underlyingsymbol = 'MSFT'
        self.AddEquity("MSFT", Resolution.Minute)
       
        
    def OnData(self, slice):
        if self.Time.date().weekday() == 0 and self.Time.hour==9 and self.Time.minute==31:
            self.subscribe_options()
        if self.Time.date().weekday() == 0 and self.Time.hour==9 and self.Time.minute==32:
            self.Buy(self.otm_call, 5)
        if self.Time.date().weekday() == 4 and self.Time.hour==15 and self.Time.minute==59:
            self.Liquidate()
        pass
    
  
    
    # using optionchainprovider
    def subscribe_options(self):
        contracts = self.OptionChainProvider.GetOptionContractList(self.underlyingsymbol, self.Time.date())
        if len(contracts) == 0 : return
        
        filtered_contracts = self.InitialFilter(self.underlyingsymbol, contracts, -5, 10, 0, 30)
        expiry = sorted(filtered_contracts, key = lambda x: x.ID.Date)[-1].ID.Date
        
        
        # filter the call options contracts
        call = [x for x in filtered_contracts if x.ID.OptionRight == 0 and x.ID.Date == expiry]
        self.otm_call = sorted(call, key = lambda x: x.ID.StrikePrice)[-1]
        
        for j in call:
            self.Log(str(j.ID.StrikePrice) +" "+ str(j.ID.Date.date()) )
        self.AddOptionContract(self.otm_call, Resolution.Minute)
        
        
        
    def InitialFilter(self, underlyingsymbol, symbol_list, min_strike_rank, max_strike_rank, min_expiry, max_expiry):
        
        ''' This method is an initial filter of option contracts
            based on the range of strike price and the expiration date '''
        
        
        if len(symbol_list) == 0 : return
        # fitler the contracts based on the expiry range
        contract_list = [i for i in symbol_list if min_expiry < (i.ID.Date.date() - self.Time.date()).days < max_expiry]
        # find the strike price of ATM option
        atm_strike = sorted(contract_list,
                            key = lambda x: abs(x.ID.StrikePrice - self.Securities[underlyingsymbol].Price))[0].ID.StrikePrice
        strike_list = sorted(set([i.ID.StrikePrice for i in contract_list]))
        # find the index of ATM strike in the sorted strike list
        atm_strike_rank = strike_list.index(atm_strike)
        try: 
            min_strike = strike_list[atm_strike_rank + min_strike_rank]
            max_strike = strike_list[atm_strike_rank + max_strike_rank]

        except:
            min_strike = strike_list[0]
            max_strike = strike_list[-1]
           
        filtered_contracts = [i for i in contract_list if i.ID.StrikePrice >= min_strike and i.ID.StrikePrice <= max_strike]

        return filtered_contracts