Overall Statistics
Total Trades
20
Average Win
15.52%
Average Loss
-6.85%
Compounding Annual Return
61.316%
Drawdown
25.800%
Expectancy
0.306
Net Profit
9.294%
Sharpe Ratio
0.879
Loss Rate
60%
Win Rate
40%
Profit-Loss Ratio
2.26
Alpha
-0.199
Beta
50.191
Annual Standard Deviation
0.698
Annual Variance
0.487
Information Ratio
0.856
Tracking Error
0.698
Treynor Ratio
0.012
Total Fees
$40.50
import numpy as np
import pandas as pd
import datetime

from System import *
from QuantConnect import *
from QuantConnect.Algorithm import *
from QuantConnect.Data import *

class bugs(QCAlgorithm):

    def Initialize(self):

        self.SetStartDate(2018, 1, 1)   #Set Start Date
        self.SetEndDate(2018, 3, 10)    #Set End Date
        self.SetCash(25000)             #Set Strategy Cash
        
        # Find more symbols here: http://quantconnect.com/data
        equity = self.AddEquity("SPY", Resolution.Minute)
        option = self.AddOption("SPY", Resolution.Minute)
        self.symbol = option.Symbol
        self.ATMcall = None
        self.ATMput = None
        self.Allocate = 0.10
        
        # Scheduled event on Mondays at 3:45PM
        self.Schedule.On(self.DateRules.Every([DayOfWeek.Friday]), \
            self.TimeRules.At(15, 45), \
            self.SellTime)
                 
        option.SetFilter(lambda universe: universe.IncludeWeeklys().Strikes(0, 0).Expiration(datetime.timedelta(15), datetime.timedelta(20)))

    def OnData(self, slice):
        
       
        if not self.Portfolio.Invested:
            
            if slice != None:
                for i in slice.OptionChains:
                    if i.Key != self.symbol: continue
                    optionchain = i.Value
            
                    self.Log("underlying price:" + str(optionchain.Underlying.Price))
                    
                    puts = [x for x in optionchain if x.Right == 1]

                    # get closest ATM put
                    self.ATMput = sorted(puts, key = lambda x: x.Strike, reverse = True)[0]
                    self.Log('Put expiry: ' + str(self.ATMput.Expiry))
                    self.Log('Put Symbol: ' + str(self.ATMput.Symbol))
                    for symbol in self.Securities.Keys:
                        if self.Securities[symbol].Invested:
                            self.Log('Liquidating ' + str(symbol))
                    
                    self.Liquidate()
                    self.SetHoldings(self.ATMput.Symbol, self.Allocate)
                    self.Log('Purchased Put Option with ' + str(abs((self.ATMput.Expiry - self.Time).days)) + ' days to expiration')
       
    def SellTime(self):
        for symbol in self.Securities.Keys:
                        if self.Securities[symbol].Invested:
                            self.Log('Liquidating ' + str(symbol))
        self.Liquidate()