Algorithm Gone Haywire !

My algorithm is a test to overreaction hypothesis. It checks the stocks that overreacted yesterday and takes an opposite position . It equally allocates the portfolio to the overreacted stocks. I tested this algorithm with 50 stocks first and the algorithm was stable. But now it is giving crazy results. Please help.

15
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
from zipline.utils.tradingcalendar import get_early_closes
def initialize(context):
context.stocks = [sid(24),sid(2),sid(25006),sid(17080),sid(23536),sid(3246),sid(19990),sid(23227),sid(22613),sid(21916),sid(27923),sid(5121),sid(46979),sid(1900),sid(45450),sid(11901),sid(42118),sid(337),sid(27019),sid(3212),sid(14848),sid(12213),sid(42277),sid(4668),sid(26378),sid(21724),sid(13798),sid(1406),sid(45971),sid(47383),sid(22802),sid(41872),sid(24819),sid(5166),sid(26524),sid(46730),sid(3472),sid(2855),sid(33742),sid(21666),sid(24551),sid(45451),sid(42815),sid(43405),sid(21870),sid(2069),sid(20476),sid(41484),sid(42166),sid(10254),sid(16108),sid(33924),sid(14484),sid(24600),sid(6935),sid(39840),sid(42584),sid(20387),sid(26557),sid(10417),sid(26994),sid(41484),sid(34443),sid(24124),sid(24829),sid(5847),sid(35015),sid(8158),sid(8146),sid(4485),sid(1251),sid(44892),sid(3735),sid(44986),sid(11673),sid(13197),sid(45769),sid(26434),sid(4911),sid(16389),sid(46002),sid(5029),sid(20479),sid(20541),sid(24801),sid(17767),sid(40129),sid(47230),sid(26126),sid(17702),sid(1131),sid(39797),sid(27194),sid(13306),sid(34560),sid(5442),sid(43467),sid(44330),sid(22139),sid(903),sid(2069),sid(25510)]
#context.weight = 1.0/len(context.stocks)
context.length = 0
context.early_closes = {}
context.overreacted = {}
context.B_OR_S = {}
#set_benchmark(context.stocks)
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=1))
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=3))
schedule_function(sell_all,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=40))
for i in context.stocks:
start_date = i.security_start_date
end_date = i.security_end_date
context.early_closes[i] = get_early_closes(start_date,end_date).date
context.overreacted[i] = False
context.B_OR_S[i] = False

for i in context.stocks:
prev_ret = 0.0
retns = 0.0
if get_datetime().date() in context.early_closes[i]:
log.info('Early close: no trading')
else:
#if context.stock in data:
highs = history(bar_count=200, frequency='1d', field='high')
lows = history(bar_count=200, frequency='1d', field='low')
opens =  history(bar_count=5, frequency='1d', field='open_price')
close =  history(bar_count=5, frequency='1d', field='close_price')
#or s in data:
ret = ((highs-lows)/lows)*100
retstd = ret.std()
retavg = ret.mean()
over = retavg+retstd
negover = -1*over
prev_ret = ret[i][-2]
retns = (close[i][-2]-opens[i][-2])
# Insert your calculations here, and then depending
# on the result, set "buy" to True or False
if (retns > 0.0 and prev_ret > over[i]):
context.overreacted[i] = True
context.B_OR_S[i] = True
#order_target_percent(i,- context.weight)
#record(avg=retavg)
elif (retns < 0.0 and prev_ret < negover[i]):
context.overreacted[i] = True
context.B_OR_S[i] = False
#order_target_percent(i, context.weight)

count = 0
for i in context.stocks:
if(context.overreacted[i]):
count = count + 1
for i in context.stocks:
if(context.overreacted[i] and context.B_OR_S[i]):
order_target_percent(i, -(1.0/count))
elif(context.overreacted[i] and not context.B_OR_S[i]):
order_target_percent(i, (1.0/count))

def sell_all(context, data):
for i in context.stocks:
if context.portfolio.positions[i].amount != 0:
order_target_percent(i, 0)

def handle_data(context, data):
pass
There was a runtime error.
3 responses

This backtest worked just fine.

15
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
from zipline.utils.tradingcalendar import get_early_closes
def initialize(context):
context.stocks = [sid(24),sid(2),sid(25006),sid(17080),sid(23536),sid(3246),sid(19990),sid(23227),sid(22613),sid(21916),sid(27923),sid(5121),sid(46979),sid(1900),sid(45450),sid(11901),sid(42118),sid(337),sid(27019),sid(3212),sid(14848),sid(12213),sid(42277),sid(4668),sid(26378),sid(21724),sid(13798),sid(1406),sid(45971),sid(47383),sid(22802),sid(41872),sid(24819),sid(5166),sid(26524),sid(46730),sid(3472),sid(2855),sid(33742),sid(21666),sid(24551),sid(45451),sid(42815),sid(43405),sid(21870),sid(2069),sid(20476),sid(41484),sid(42166),sid(10254)]
#context.weight = 1.0/len(context.stocks)
context.length = 0
context.early_closes = {}
context.overreacted = {}
context.B_OR_S = {}
#set_benchmark(context.stocks)
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=1))
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=3))
schedule_function(sell_all,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=35))
for i in context.stocks:
start_date = i.security_start_date
end_date = i.security_end_date
context.early_closes[i] = get_early_closes(start_date,end_date).date
context.overreacted[i] = False
context.B_OR_S[i] = False

for i in context.stocks:
prev_ret = 0.0
retns = 0.0
if get_datetime().date() in context.early_closes[i]:
log.info('Early close: no trading')
else:
#if context.stock in data:
highs = history(bar_count=200, frequency='1d', field='high')
lows = history(bar_count=200, frequency='1d', field='low')
opens =  history(bar_count=5, frequency='1d', field='open_price')
close =  history(bar_count=5, frequency='1d', field='close_price')
#or s in data:
ret = ((highs-lows)/lows)*100
retstd = ret.std()
retavg = ret.mean()
over = retavg+retstd
negover = -1*over
prev_ret = ret[i][-2]
retns = (close[i][-2]-opens[i][-2])
# Insert your calculations here, and then depending
# on the result, set "buy" to True or False
if (retns > 0.0 and prev_ret > over[i]):
context.overreacted[i] = True
context.B_OR_S[i] = True
#order_target_percent(i,- context.weight)
#record(avg=retavg)
elif (retns < 0.0 and prev_ret < negover[i]):
context.overreacted[i] = True
context.B_OR_S[i] = False
#order_target_percent(i, context.weight)

count = 0
for i in context.stocks:
if(context.overreacted[i]):
count = count + 1
for i in context.stocks:
if(context.overreacted[i] and context.B_OR_S[i]):
order_target_percent(i, -(1.0/count))
elif(context.overreacted[i] and not context.B_OR_S[i]):
order_target_percent(i, (1.0/count))

def sell_all(context, data):
for i in context.stocks:
if context.portfolio.positions[i].amount != 0:
order_target_percent(i, 0)

def handle_data(context, data):
pass
There was a runtime error.

Make sure you test with default IB commissions too

3
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
 Returns 1 Month 3 Month 6 Month 12 Month
 Alpha 1 Month 3 Month 6 Month 12 Month
 Beta 1 Month 3 Month 6 Month 12 Month
 Sharpe 1 Month 3 Month 6 Month 12 Month
 Sortino 1 Month 3 Month 6 Month 12 Month
 Volatility 1 Month 3 Month 6 Month 12 Month
 Max Drawdown 1 Month 3 Month 6 Month 12 Month
from zipline.utils.tradingcalendar import get_early_closes
def initialize(context):
context.stocks = [sid(24),sid(2),sid(25006),sid(17080),sid(23536),sid(3246),sid(19990),sid(23227),sid(22613),sid(21916),sid(27923),sid(5121),sid(46979),sid(1900),sid(45450),sid(11901),sid(42118),sid(337),sid(27019),sid(3212),sid(14848),sid(12213),sid(42277),sid(4668),sid(26378),sid(21724),sid(13798),sid(1406),sid(45971),sid(47383),sid(22802),sid(41872),sid(24819),sid(5166),sid(26524),sid(46730),sid(3472),sid(2855),sid(33742),sid(21666),sid(24551),sid(45451),sid(42815),sid(43405),sid(21870),sid(2069),sid(20476),sid(41484),sid(42166),sid(10254)]
#context.weight = 1.0/len(context.stocks)
context.length = 0
context.early_closes = {}
context.overreacted = {}
context.B_OR_S = {}
#set_benchmark(context.stocks)
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=1))
date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=3))
schedule_function(sell_all,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close(minutes=35))
for i in context.stocks:
start_date = i.security_start_date
end_date = i.security_end_date
context.early_closes[i] = get_early_closes(start_date,end_date).date
context.overreacted[i] = False
context.B_OR_S[i] = False

for i in context.stocks:
prev_ret = 0.0
retns = 0.0
if get_datetime().date() in context.early_closes[i]:
log.info('Early close: no trading')
else:
#if context.stock in data:
highs = history(bar_count=200, frequency='1d', field='high')
lows = history(bar_count=200, frequency='1d', field='low')
opens =  history(bar_count=5, frequency='1d', field='open_price')
close =  history(bar_count=5, frequency='1d', field='close_price')
#or s in data:
ret = ((highs-lows)/lows)*100
retstd = ret.std()
retavg = ret.mean()
over = retavg+retstd
negover = -1*over
prev_ret = ret[i][-2]
retns = (close[i][-2]-opens[i][-2])
# Insert your calculations here, and then depending
# on the result, set "buy" to True or False
if (retns > 0.0 and prev_ret > over[i]):
context.overreacted[i] = True
context.B_OR_S[i] = True
#order_target_percent(i,- context.weight)
#record(avg=retavg)
elif (retns < 0.0 and prev_ret < negover[i]):
context.overreacted[i] = True
context.B_OR_S[i] = False
#order_target_percent(i, context.weight)

count = 0
for i in context.stocks:
if(context.overreacted[i]):
count = count + 1
for i in context.stocks:
if(context.overreacted[i] and context.B_OR_S[i]):
order_target_percent(i, -(1.0/count))
elif(context.overreacted[i] and not context.B_OR_S[i]):
order_target_percent(i, (1.0/count))

def sell_all(context, data):
for i in context.stocks:
if context.portfolio.positions[i].amount != 0:
order_target_percent(i, 0)

def handle_data(context, data):
pass
There was a runtime error.

How can I use set_universe in my algo ? Need some help in making changes.