def handle_bar(context, bar_dict):
# ?????????????
# ??????????history_bars??????ndarray????????????
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)
# macd ?????????signal?macd?????????????????????????????????
if (macd[-1] - signal[-1] > 0 and macd[-2] - signal[-2] < 0):
sell_qty = context.portfolio.positions[context.s1].sell_quantity
# ?????????????????????
if (sell_qty > 0):
buy_close(context.s1, 1)
# ????
buy_open(context.s1, 1)
if (macd[-1] - signal[-1] < 0 and macd[-2] - signal[-2] > 0):
buy_qty = context.portfolio.positions[context.s1].buy_quantity
# ?????????????????????
if (buy_qty > 0):
sell_close(context.s1, 1)
# ????
sell_open(context.s1, 1)
评论列表
文章目录