In the Quantopian Open contest and real-money trading for winners, if your algo crashes, is it game over for you (although if you've made a profit, you get it). One thing I forgot to consider is that one or more of the securities in my algo could be de-listed. Here's some code that should manage de-listing. Questions/comments/improvements welcome.
def initialize(context):
# master list of securities
context.stocks_master = [sid(24),
sid(8229),
sid(3951),
sid(20387),
sid(21947)]
# list of available securities
context.stocks = []
def handle_data(context, data):
# build list of available securities from master list
context.stocks = []
for stock in context.stocks_master:
if stock.security_end_date >= get_datetime():
context.stocks.append(stock)
# number of available securities
num_stocks = len(context.stocks)
record(num_stocks = num_stocks)
# maintain equal-weight portfolio
pct = 1.0/num_stocks
for stock in context.stocks:
order_target_percent(stock, pct)