Back

Option Trading on Custom Data - OnSecuritiesChanged behaviour problems

Hello everyone,

I am struggling to build a backtesting model that take in custom data to go Long/Short Straddle. So far it does the following steps :

  • Grabs Equity Tickers from a CSV (Done.) 
  • Adds them to the Universe depending on the Timestamp (Done.)
  • Calls OnSecuritiesChanged to call another function that is looking for Weekly ATM Calls and Puts and trades them (Done.)
So everything should be fine but unfortunately I get "Runtime Error: Collection was modified; enumeration operation may not execute." No Trades.. Just failure of the algorithm. My understanding is that when looking at Options it adds them to the universe and therefore OnSecuritiesChanged is called again and we end up in an infinite loop... How to avoid that since we HAVE TO add the equities and options in order to get prices and trade them ? Here is the backtesting, your help or some guidelines would be highly appreciated. Thank you in advance,Terence. import decimal as d
import numpy as np
import pandas as pd
import math
import datetime
import json

class DropboxBaseDataUniverseSelectionAlgorithm(QCAlgorithm):

def Initialize(self):

self.UniverseSettings.Resolution = Resolution.Minute;

self.SetStartDate(2019,1,8)
self.SetEndDate(2019,1,30)
self.SetCash(100000)

spy = self.AddEquity("SPY", Resolution.Minute)
spy.SetDataNormalizationMode(DataNormalizationMode.Raw)


self.AddUniverse(StockDataSource, "my-stock-data-source", self.stockDataSource)
self.Schedule.On(self.DateRules.EveryDay("SPY"), self.TimeRules.BeforeMarketClose("SPY", 10), self.EveryDayBeforeMarketClose)


self.UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw

def stockDataSource(self, data): # This will grab for each date and hour the different tickers in the csv and add them to the universe
list = []
for item in data:
for symbol in item["Symbols"]:
list.append(symbol)
#self.Debug(str(self.Time))
#self.Debug(str(list))
return list


def TradeOptions(self,contracts, ticker):
# run CoarseSelection method and get a list of contracts expire within 5 days from now on
# and the strike price between rank -1 to rank 1, rank being the step of the contract

filtered_contracts = self.CoarseSelection(ticker, contracts, -1, 1, 0, 15)

if len(filtered_contracts) >0:

expiry = sorted(filtered_contracts,key = lambda x: x.ID.Date, reverse=False)[0].ID.Date # Take the closest expiry

# 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]
# sorted the contracts according to their strike prices
call_contracts = sorted(call,key = lambda x: x.ID.StrikePrice)
self.call = call_contracts[0]

for i in filtered_contracts:
if i.ID.Date == expiry and i.ID.OptionRight == 1 and i.ID.StrikePrice ==call_contracts[0].ID.StrikePrice:
self.put = i

''' Before trading the specific contract, you need to add this option contract
AddOptionContract starts a subscription for the requested contract symbol '''


# self.call is the symbol of a contract
self.AddOptionContract(self.call, Resolution.Minute)
self.AddOptionContract(self.put, Resolution.Minute)

if not self.Portfolio.Invested:
self.SetHoldings(self.call.Value, 0.1)
self.SetHoldings(self.put.Value ,0.1)

# Some Logging
#self.Debug("Strike Price : "+str(self.call.ID.StrikePrice))
#self.Debug("Expiry : "+str(self.call.ID.Date))
#self.Debug("Call Mid-Point : "+str(self.Securities[self.call].Price))

#self.Debug("IV : "+str(self.call.ImpliedVolatility))


else:
pass

def OnSecuritiesChanged(self, changes):
self._changes = changes

for security in changes.AddedSecurities:

self.Debug(security)
self.Debug(security.Symbol.SecurityType)

if security.Symbol.SecurityType == 1:
#self.SetHoldings(security.Symbol, 0.1)
stk = self.AddEquity(security.Symbol.Value, Resolution.Minute)
stk.SetDataNormalizationMode(DataNormalizationMode.Raw)

contracts = self.OptionChainProvider.GetOptionContractList(security.Symbol, self.Time.date()) # Get list of strikes and expiries
self.TradeOptions(contracts, security.Symbol.Value) # Select the right strikes/expiries and trade



def EveryDayBeforeMarketClose(self):
#self.Debug("############## Closing Position " + str(self.Time.date()) + " " + str(self.Time) + "############## ")
self.Liquidate()
self.Debug("Positions closed")

def CoarseSelection(self, underlyingsymbol, symbol_list, min_strike_rank, max_strike_rank, min_expiry, max_expiry):

''' This method implements the coarse selection of option contracts
according to the range of strike price and the expiration date,
this function will help you better choose the options of different moneyness '''


# filter 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]
self.Debug("Ticker Und : " + str(underlyingsymbol))
self.Debug("Nb of contract found : " + str(len(contract_list)))
self.Debug("Underlying price : "+str(self.Securities[underlyingsymbol].Price))

# find the strike price of ATM option
# It seems like sometimes OptionChainProvider.GetOptionContractList is bugging and returns nothing, so let's try/except
try :
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]

except:
self.Debug("NO CONTRACT RETURNED -------")
filtered_contracts = None


return filtered_contracts

class StockDataSource(PythonData):

def GetSource(self, config, date, isLiveMode):
url = "https://www.dropbox.com/s/2az14r5xbx4w5j6/daily-stock-picker-live.csv?dl=1" if isLiveMode else \
"https://www.dropbox.com/s/ofzgxsp2b27pkri/quantconnect_triggers.csv?dl=1"

return SubscriptionDataSource(url, SubscriptionTransportMedium.RemoteFile)

def Reader(self, config, line, date, isLiveMode):
#if not (line.strip() and line[0].isdigit()): return None

stocks = StockDataSource()
stocks.Symbol = config.Symbol

csv = line.rstrip(',').split(',') # rstrip is essential because quantconnect throws an empty element error (extra commas at the end of the csv)
if isLiveMode:
stocks.Time = date
stocks["Symbols"] = csv
else:
stocks.Time = datetime.datetime.combine(datetime.datetime.strptime(csv[0], "%Y%m%d"),
datetime.time(9, 31))
stocks["Symbols"] = csv[1:]
return stocks

 

  
Update Backtest







0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Dear Terence, 

Your understanding is correct. `OnSecuritiesChanged` handles every newly added/removed securities. It is the best practice to avoid adding/removing securities in `OnSecuritiesChanged`. One solution to the problem is to move the trading logic from `OnSecuritiesChanged` to the `OnData` method. Notes that once a symbol is registered to the universe, the data of that symbol starts to pump into `OnData`. Please see the attached backtest for implementation details. 

Additionally, I would suggest changing the `min_expiry` in `CoarseSelection` from 0 to 1 so that the strategy could avoid trading the options contract that expires on the same trading day. 

Please feel free to share more information on your strategy so that we can discuss further. 

Xin

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Hello Xin,

Thank you so much for your help. FYI, I added a few tweaks to make it work as expected : 

- Check in OnData if we did not already invest in the underlying symbol (I did not want if not self.Portfolio.invested since it blocked any investment after the first of the day) - my approach might not be optimal though...

- Remove SPY from OnData decision tree since I do not want to trade SPY (I am just using it to Schedule closing)

- RemoveSecurity after closing each day because I do not want to keep trading the underlyings the following day (it's an intra-day trigger-based trading algorithm)

 

=> Unfortunately, it seems like I cannot get the option trading working for more than one security per day. Indeed, I can have zero or up to 5 custom triggers (underlyings) per day and after logging I can see that QuantConnect does not return option prices after the first Option...

Here is my file : https://www.dropbox.com/s/ofzgxsp2b27pkri/quantconnect_triggers.csv?dl=1

Is it a known issue ? 

Thank you in advance for your help,

Terence.

0


Hi Terence,

Thank you for the follow-up question and for sharing information about your strategy.

Note that what passed into the `OnData` event handler is a data slice that contains all the data for a given time. So, the 'return' statement in `OnData` handler would skip all the remaining assets in the current `OnData` slice and move to the next period. However, I think your strategy only wants to skip the remaining statements in the loop and moves the control to the next asset. So, you may want to replace the 'return' with 'continue' statement. Please see my attached backtest and its Logs for more details. The strategy is able to trade more than one options contract per day. Please let me know if you have further questions.

Xin

1

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Update Backtest





0

The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.


Loading...

This discussion is closed