Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Help!! Can't find where is wrong :(

Can anyone help to find what's wrong with this code? I am green on python and Quantopian but need to submit a task for a class.
I finally get this code but it doesn't work :(
Thank you very much if you can help!!

import numpy as np
def initialize(context):
# Dictionary of stocks and their respective weights
context.stock_weights = {}
# Count of days before rebalancing
context.days = 0
# filter stocks
schedule_function(filt,
date_rule=date_rules.month_start(),
time_rule=time_rules.market_open())
schedule_function(close,
date_rule=date_rules.month_end(),
time_rule=time_rules.market_close(minutes=30))
schedule_function(rebalance,
date_rule=date_rules.every_day()
time_rule=time_rules.market_open())
schedule_function(stoploss,
date_rule=date_rules.every_day()
time_rule=time_rules.market_open())
schedule_function(record_positions,
date_rule=date_rules.every_day(),
time_rule=time_rules.market_close())

def filt(context, data):
num_stocks = 20
fundamental_df = get_fundamentals(
query(
# put your query in here by typing "fundamentals."
fundamentals.valuation_ratios.ev_to_ebitda,
fundamentals.asset_classification.morningstar_sector_code, fundamentals.valuation.enterprise_value, fundamentals.income_statement.ebit, fundamentals.income_statement.ebitda
)
.filter(fundamentals.valuation.market_cap > 2e9)
.filter(fundamentals.valuation_ratios.ev_to_ebitda > 0)
.filter(fundamentals.valuation.enterprise_value > 0)
.filter(fundamentals.asset_classification.morningstar_sector_code != 103)
.filter(fundamentals.asset_classification.morningstar_sector_code != 207)
#103: financial service, 207: utilities#
.filter(fundamentals.valuation.shares_outstanding != None)
.order_by(fundamentals.valuation_ratios.ev_to_ebitda.asc())
.limit(num_stocks)
)
context.stocks = [stock for stock in fundamental_df]
context.fundamental_df = fundamental_df[context.stocks]

def create_weights(stocks):
#Takes in a list of securities and weights them all equally
if len(stocks) == 0:
return 0
else:
weight = 1.0 / len(stocks)
return weight

def close(context, data):
for stock in context.portfolio.positions:
order_target_percent(stock, 0)

def rebalance(context, data):
hist = data.history(stock, 'price', 400, '1d')
sma_5 = hist[-5:].mean()
sma_20 = hist[-20:].mean()
sma_50 = hist[-50:].mean()
sma_200 = hist[-200:].mean()
sma_400 = hist.mean()
curposition = context.portfolio.positions[stock].amount
for stock in context.fundamental_df:
if sma_200 > sma_400:
if data.can_trade(stock) and sma_5 > sma_20 and curposition == 0:
order_target_percent(stock, weight)
elif data.can_trade(security) and sma_5 > sma_50 and curposition == 0:
order_target_percent(stock, weight)

def stoploss(context, data):
for stock in context.fundamental_df:
market_value=data.current(stock, 'price')
bought_value=context.portfolio.positions[stock].cost_basis
if market_value < bought_value*0.95 or market_value > bought_value*1.05:
order_target_percent(stock, 0)

def record_positions(context, data):
# track how many positions we're holding
record(num_positions = len(context.portfolio.positions))