Hi I just started today. I wrote a simple algorithm to start with Quantopian but seems there is some error. Where did I go wrong?
def initialize(context):
schedule_function(rebalance,date_rules.week_start(),time_rules.market_open(hours=1,minutes=0))
def getStocks(context):
number_of_stocks = 10
fund = get_fundamentals(
query(
fundamentals.asset_classification.sic,
fundamentals.valuation_ratios.pe_ratio,
fundamentals.income_statement.gross_profit
)
.filter(fundamentals.valuation_ratios.book_value_per_share<1.8
# put your filters here
)
.filter(fundamentals.valuation_ratios.pe_ratio<18
)
.filter(fundamentals.earnings_report.basic_eps>0)
.filter(fundamentals.income_statement.gross_profit>0)
.filter(fundamentals.operation_ratios.quick_ratio>1)
.filter(fundamentals.operation_ratios.current_ratio>2)
.order_by(
# sort your query
fundamentals.valuation.market_cap.DESC()
)
.limit(number_of_stocks)
)
context.fund = fund
context.stocks = fund.columns.values
def rebalance(context,data):
current_stock = context.portfolio.positions
for stock in current_stock:
if stock not in context.fund:
order_target_percent(stock,0)
new_weight = get_weight(context,context.stocks)
for stock in context.stocks:
if stock not in current_stock:
order_target_percent(stock, new_weight)
def get_weight(context, stock):
l = len(stock)
if l == 0:
return 0
else:
return 0.95/l
def handle_data(context,data):
"""
Called every minute.
"""
pass
Please help! Thank you!