def handle_bar(context, bar_dict):
# ?????????????
# bar_dict[order_book_id] ?????????bar??
# context.portfolio ???????????????
# ??order_shares(id_or_ins, amount)??????
# TODO: ??????????
# ????????????loop?????????RSI??
for stock in context.stocks:
# ??????
prices = history_bars(stock, context.TIME_PERIOD+1, '1d', 'close')
# ?Talib??RSI?
rsi_data = talib.RSI(prices, timeperiod=context.TIME_PERIOD)[-1]
cur_position = context.portfolio.positions[stock].quantity
# ??????30%???????
target_available_cash = context.portfolio.cash * context.ORDER_PERCENT
# ?RSI???????????????
if rsi_data > context.HIGH_RSI and cur_position > 0:
order_target_value(stock, 0)
# ?RSI?????????????cash?????????
if rsi_data < context.LOW_RSI:
logger.info("target available cash caled: " + str(target_available_cash))
# ??????????? - 100shares?????ricequant ?order management system reject?
order_value(stock, target_available_cash)
评论列表
文章目录