Hi everyone,
Whenever I try to backtest my algorithm in the minute mode I get this error
TypeError: Too much time spent in handle_data call.
Any help would be appreciated. Thanks!
import pandas
def initialize(context):
set_universe(universe.DollarVolumeUniverse(95, 100))
context.size = (context.portfolio.cash / 10)
#context.shorts = context.size * 0.5
#context.longs = context.size * 0.5
context.shorts = []
context.shorting= True
#set_do_not_order_list(security_lists.leveraged_etf_list)
#schedule_function(logic,date_rules.every_day(),time_rules.market_open(hours=0, minutes=1))
def handle_data(context, data):
high = history(252, "1d", "high")
low = history(252, "1d", "low")
rolling_max = pandas.stats.moments.rolling_max(high,251)
rolling_min = pandas.stats.moments.rolling_min(low,251)
for s in data:
price = data[s].price
pmin = rolling_min[s][-2]
pmax = rolling_max[s][-2]
ma5 = data[s].mavg(14)
ma10 = data[s].mavg(30)
ma50 = data[s].mavg(100)
ma200 = data[s].mavg(200)
current_position = context.portfolio.positions[s].amount
price = data[s].price
if (price > pmax) and (current_position == 0) and (ma10 > ma50) and (ma50 > ma200) :
if not get_open_orders():
order_target_value(s, context.size )
log.info("LONG " + str(s.symbol))
elif (price < ma5) and (current_position != 0) and s not in context.shorts :
order_target_value(s, 0)
log.info("sell " + str(s.symbol))
if context.shorting:
if (price < pmin) and (current_position == 0) and (ma10 < ma50) and (ma50 < ma200) :
# if s not in context.fundamentals:
if not get_open_orders() :
order_target_value(s, -context.size )
context.shorts.append(s)
log.info("short " + str(s.symbol))
elif (price > ma5) and (current_position != 0) and s in context.shorts :
order_target_value(s, 0)
log.info("recover " + str(s.symbol))
context.shorts.remove(s)
record('Leverage',context.account.leverage)