I am unable to figure out the problem in the code. Any help is appreciated.
def initialize(context):
schedule_function(rebalance,date_rule=date_rules.every_day(),
time_rule=time_rules.market_open(minutes=1))
def before_trading_starts(context):
fundamentals_df = get_fundamentals(
query(
fundamentals.operation_ratios.net_income_growth,
fundamentals.earnings_ratios.diluted_eps_growth,
fundamentals.valuation.market_cap,
fundamentals.valuation.shares_outstanding,
)
.filter( fundamentals.valuation.market_cap != None
)
.filter( fundamentals.operation_ratios.net_income_growth > 1.5
)
.filter( fundamentals.earnings_ratios.diluted_eps_growth > 1.2
)
.filter( fundamentals.valuation.shares_outstanding != None
)
.order_by( fundamentals.operation_ratios.net_income_growth.desc()
)
.limit(100)
)
context.stocks = fundamentals_df
update_universe(context.stocks)
def rebalance(context,data):
for stock in context.portfolio.positions:
if stock not in context.stocks:
order_target(stock,0)
def weights(context,stocks):
if len(stocks) == 0:
return 0
else:
weight = 0.99/len(stocks)
return weight
def handle_data(context,data):
for stock in context.stocks:
order_target_percent(stock,weights(context,context.stocks))
It gives an error saying : "'update_universe' only permitted within before_trading_start function"