def handle_bar(context, bar_dict):
# ?????????????
# bar_dict[order_book_id] ?????????bar??
# context.portfolio ???????????????
# ??order_shares(id_or_ins, amount)??????
# TODO: ??????????
# ?????????sma??????????????????????ema????????????????????????????
prices = history_bars(context.s1, context.OBSERVATION,'1d','close')
# ?Talib??MACD?????????????????macd, signal ? hist
macd, signal, hist = talib.MACD(prices, context.SHORTPERIOD,
context.LONGPERIOD, context.SMOOTHPERIOD)
plot("macd", macd[-1])
plot("macd signal", signal[-1])
# macd ?????????signal?macd??????macd?????????????????macd???signal??????
# ??macd??????macd_signal
if macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0:
# ????portfolio??????
curPosition = context.portfolio.positions[context.s1].quantity
#????
if curPosition > 0:
order_target_value(context.s1, 0)
# ????????????????????
if macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0:
# ????
order_target_percent(context.s1, 1)
评论列表
文章目录