| Overall Statistics |
|
Total Trades 1842 Average Win 0.06% Average Loss -0.02% Compounding Annual Return -1.908% Drawdown 5.700% Expectancy -0.254 Net Profit -5.619% Sharpe Ratio -2.327 Probabilistic Sharpe Ratio 0.000% Loss Rate 79% Win Rate 21% Profit-Loss Ratio 2.54 Alpha -0.016 Beta 0.003 Annual Standard Deviation 0.007 Annual Variance 0 Information Ratio -0.74 Tracking Error 0.207 Treynor Ratio -6.253 Total Fees $1842.00 |
import math
class LongStraddleAlgorithm(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2018, 1, 1)
self.SetEndDate(2020, 12, 31)
self.SetCash(100000)
self.equity = self.AddEquity("SPY", Resolution.Minute)
self.equity.SetDataNormalizationMode(DataNormalizationMode.Raw)
self.SetSecurityInitializer(lambda x: x.SetMarketPrice(self.GetLastKnownPrice(x)))
self.underlyingsymbol = self.equity.Symbol
self.SetBenchmark(self.underlyingsymbol)
self.Schedule.On(self.DateRules.EveryDay(),self.TimeRules.At(15,30),self.close_pos)
self.lower_call_cost = 0
self.lower_put_cost = 0
self.higher_call_cost = 0
self.higher_put_cost = 0
def OnData(self,slice):
if not self.Portfolio.Invested:
contracts = self.OptionChainProvider.GetOptionContractList(self.underlyingsymbol, self.Time.date())
if self.Time.hour == 11 and self.Time.minute == 00:
self.TradeOptions(contracts)
if self.Portfolio.Invested and self.Time.hour >= 11:
#if price of underlying goes beyond the range of (- 2%) it will liquidate
'''if self.Securities[self.lower_put.Value].Price < 0.9 * self.lower_put_cost:
self.Liquidate(self.lower_put.Value)
self.Log("Lower Put Liquidated On " + str(self.Time))
if self.Securities[self.higher_call.Value].Price < 0.9 * self.higher_call_cost:
self.Liquidate(self.higher_call.Value)
self.Log("Higher Call Liquidated On " + str(self.Time))
if self.Securities[self.higher_put.Value].Price > 1.1 * self.higher_put_cost:
self.Liquidate(self.higher_put.Value)
self.Log("Higher Put Liquidated On " + str(self.Time))
if self.Securities[self.lower_call.Value].Price > 1.1 * self.lower_call_cost:
self.Liquidate(self.lower_call.Value)
self.Log("Lower Call Liquidated On " + str(self.Time))'''
if self.Portfolio.Invested and self.Time.hour >= 11 and self.Time.minute % 30 == 0 :
#if price of underlying goes beyond the range of (- 2%) it will liquidate
if self.Securities[self.lower_put.Value].Price >= 1.1 * self.lower_put_cost:
self.Liquidate(self.lower_put.Value)
self.Log("Lower Put Liquidated On " + str(self.Time))
if self.Securities[self.higher_call.Value].Price >= 1.1 * self.higher_call_cost:
self.Liquidate(self.higher_call.Value)
self.Log("Higher Call Liquidated On " + str(self.Time))
if self.Securities[self.higher_put.Value].Price < 0.9 * self.higher_put_cost:
self.Liquidate(self.higher_put.Value)
self.Log("Higher Put Liquidated On " + str(self.Time))
if self.Securities[self.lower_call.Value].Price < 0.9 * self.lower_call_cost:
self.Liquidate(self.lower_call.Value)
self.Log("Lower Call Liquidated On " + str(self.Time))
def TradeOptions(self,contracts):
# run CoarseSelection method and get a list of contracts expire within 0 to 8 days from now on
# and the strike price between rank -1 to rank 1
filtered_contracts = self.CoarseSelection(self.underlyingsymbol, contracts, -1, 1, 0, 2)
if filtered_contracts is None: return
expiry = sorted( filtered_contracts,key = lambda x: (x.ID.Date.date() - self.Time.date()).days )[0].ID.Date
# filter the call options from the contracts expire on that date
call = [i for i in filtered_contracts if i.ID.Date == expiry and i.ID.OptionRight == 0]
put = [i for i in filtered_contracts if i.ID.Date == expiry and i.ID.OptionRight == 1 ]
# sorted the contracts according to their ATM strike prices
call_contracts = sorted( call , key = lambda x : x.ID.StrikePrice )
put_contracts = sorted( put , key = lambda x : x.ID.StrikePrice )
if len(call_contracts) > 0 and len(put_contracts) > 0 :
self.higher_call = call_contracts[-1]
self.higher_put = put_contracts[0]
try:
self.lower_call = call_contracts[-1*math.floor(len(put_contracts)/4)]
self.lower_put = put_contracts[math.floor(len(put_contracts)/4)]
except:
self.lower_call = sorted(call, key = lambda x : abs(x.ID.StrikePrice - self.Securities[self.underlyingsymbol].Price))[0]
self.higher_put = sorted(put, key = lambda x : abs(x.ID.StrikePrice - self.Securities[self.underlyingsymbol].Price))[0]
self.AddOptionContract(self.lower_call, Resolution.Minute)
self.AddOptionContract(self.higher_call, Resolution.Minute)
self.AddOptionContract(self.lower_put, Resolution.Minute)
self.AddOptionContract(self.higher_put, Resolution.Minute)
self.Sell(self.lower_put.Value ,1)
self.Sell(self.higher_call.Value ,1)
self.Buy(self.higher_put.Value ,1)
self.Buy(self.lower_call.Value ,1)
self.lower_call_cost = self.Securities[self.lower_call.Value].Price
self.lower_put_cost = self.Securities[self.lower_put.Value].Price
self.higher_call_cost = self.Securities[self.higher_call.Value].Price
self.higher_put_cost = self.Securities[self.higher_put.Value].Price
def CoarseSelection(self, underlyingsymbol, symbol_list, min_strike_rank, max_strike_rank, min_expiry, max_expiry):
# 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
if len(contract_list) <= 0: return
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]
# filter the contracts based on the range of the strike price rank
filtered_contracts = [i for i in contract_list if i.ID.StrikePrice >= min_strike and i.ID.StrikePrice <= max_strike]
return filtered_contracts
def close_pos(self):
if self.Portfolio.Invested:
self.Liquidate()