def handle_bar(context, bar_dict):
# ?????????????
# bar_dict[order_book_id] ?????????bar??
# context.portfolio ???????????????
# ??order_shares(id_or_ins, amount)??????
# TODO: ??????????
stocks = set(context.to_buy)
holdings = set(get_holdings(context))
to_buy = stocks - holdings
holdings = set(get_holdings(context))
to_sell = holdings - stocks
for stock in to_sell:
if bar_dict[stock].is_trading:
order_target_percent(stock , 0)
to_buy = get_trading_stocks(to_buy, context, bar_dict)
buy_rsi = []
for stock in to_buy:
prices = history(context.SMAPERIOD+1,'1d','close')[stock].values
avg = talib.SMA(prices,context.SMAPERIOD)
if avg[-1] < prices[-1]:
buy_rsi.append(stock)
if len(buy_rsi) >0:
#cash = context.portfolio.cash
#average_value = 0.98 * cash / len(buy_rsi)
for stock in buy_rsi:
if bar_dict[stock].is_trading:
#order_value(stock ,average_value)
order_target_percent(stock,0.08)
评论列表
文章目录