等级: 专业版
- 注册:
- 2021-5-20
- 曾用名:
|
def market_open(context):
#log.info('函数运行时间(market_open):'+str(context.current_dt.time()))
for ins in g.instruments:
if g.MappingReal[ins] != '':
IndexFuture = g.MappingIndex[ins]
RealFuture = g.MappingReal[ins]
end_date = get_CCFX_end_date(RealFuture)
if (context.current_dt.date() == end_date):
return
else:
g.LastRealPrice[RealFuture] = attribute_history(RealFuture,1,'1d',['close'])['close'][-1]
# 获取价格list
#if g.StatusTimer[ins] == 0:
g.PriceArray[IndexFuture] = attribute_history(IndexFuture,g.BackWindow+5,'1d',['close','open','high','low'])
if len(g.PriceArray[IndexFuture]) < 1:
return
else:
g.ClosePrice[ins] = g.PriceArray[IndexFuture]['close']
g.CurrentPrice[ins] = g.ClosePrice[ins][-1]
g.Price_DaysAgo[ins] = g.ClosePrice[ins][-g.BackWindow]
close = np.array(g.PriceArray[IndexFuture]['close'])
high = np.array(g.PriceArray[IndexFuture]['high'])
low = np.array(g.PriceArray[IndexFuture]['low'])
g.ATR[IndexFuture] = talib.ATR(high,low,close, g.BackWindow)[-1]
# 计算动量标记
if g.CurrentPrice[ins] > g.Price_DaysAgo[ins] :
g.Momentum[ins] = 1
elif g.CurrentPrice[ins] < g.Price_DaysAgo[ins] :
g.Momentum[ins] = -1
else:
g.Momentum[ins] = 0
#判断交易信号:动量g.Momentum[ins] == 1多头,-1为空头
if g.Momentum[ins] == 1 :
g.Signal[ins] = 1
elif g.Momentum[ins] == -1 :
g.Signal[ins] = -1
else:
g.Signal[ins] = 0
## 收盘后运行函数
def after_market_close(context):
log.info(str('函数运行时间(after_market_close):'+str(context.current_dt.time())))
# 得到当天所有成交记录
trades = get_trades()
for _trade in trades.values():
log.info('成交记录:'+str(_trade))
log.info('一天结束')
log.info('##############################################################')
## 交易模块
def Trade(context):
for ins in g.instruments:
RealFuture = g.MappingReal[ins]
if ins in g.Signal.keys() and RealFuture in g.LastRealPrice.keys():
if g.Signal[ins] == 1 and context.portfolio.long_positions[RealFuture].total_amount == 0:
if context.portfolio.long_positions[RealFuture].total_amount != 0:
log.info('空头有持仓:%s'%(RealFuture))
order_target(RealFuture,0,side='short')
order_target(RealFuture,g.TradeLots[RealFuture],side='long')
g.HighPrice[RealFuture] = g.LastRealPrice[RealFuture]
g.LowPrice[RealFuture] = False
log.info('正常买多合约:%s'%(RealFuture))
elif g.Signal[ins] == -1 and context.portfolio.short_positions[RealFuture].total_amount == 0:
if context.portfolio.short_positions[RealFuture].total_amount != 0:
log.info('多头有持仓:%s'%(RealFuture))
order_target(RealFuture,0,side ='long')
order_target(RealFuture,g.TradeLots[RealFuture],side='short')
g.LowPrice[RealFuture] = g.LastRealPrice[RealFuture]
g.HighPrice[RealFuture] = False
log.info('正常卖空合约:%s'%(RealFuture))
补充内容 (2022-3-13 09:51):
希望能翻译成pEL图表交易,把核心交易模块翻译下 |
|