等级: 新手上路
- 注册:
- 2023-4-14
- 曾用名:
|
如下为我策略的开仓模块
#********************************************开仓模块*******************************************************
#开多
if datalow[-1] >lybbi and lydif[-1] > lydea[-1]:
if context.lyportfoliowcj.buy_quantity==0:
if context.lyportfoliowcj.sell_quantity==0:
unit = getkcss(context,dataclose[-1]);
orderid= buy_open(context.lystockcode, context.ordertype, volume =unit,serial_id = 3); # 市价开多 返回订单ID 根据此ID去查订单
WriteTxtLog(context,"当前BBI值为{0},ma1:{1},ma2:{2},ma3:{3},ma4:{4},当前收盘价{5},dif:{6},dea:{7},lymacd:{8},当前持多仓{9},当前持空仓{10}".
format(str(lybbi),str(ma1[-1]),str(ma2[-1]),str(ma3[-1]),str(ma4[-1]),str(dataclose[-1]),str(lydif[-1]),str(lydea[-1]),str(lymacd[-1]),str(context.lyportfoliowcj.buy_quantity),str(context.lyportfoliowcj.sell_quantity)));
if orderid == -1:#下单失败
WriteTxtLog(context,"开多:多单下单失败");
else:
InitParams(context);
#time.sleep(10);
lykdorderinfo = get_orders_id(orderid,context.account);
if lykdorderinfo == None:
WriteTxtLog(context,"开多:获取开多下单信息失败,单号{0}".format(orderid));
else:
lykdorderinfo = get_orders_id(orderid,context.account);
if lykdorderinfo != None:
WriteTxtLog(context,"开多:多单下单成功,订单号{0},手数{1},下单价格{2}".
format(lykdorderinfo.order_id,str(lykdorderinfo.quantity),str(lykdorderinfo.price)));
#开空
if datahigh[-1] <lybbi and lydif[-1] < lydea[-1]:
if context.lyportfoliowcj.sell_quantity==0:#开空
if context.lyportfoliowcj.buy_quantity==0 :
unit = getkcss(context,dataclose[-1]);
orderid= sell_open(context.lystockcode, context.ordertype, volume = unit,serial_id = 4); # 市价开空 返回订单ID 根据此ID去查订单
WriteTxtLog(context,"当前BBI值为{0},ma1:{1},ma2:{2},ma3:{3},ma4:{4},当前收盘价{5},dif:{6},dea:{7},lymacd:{8},当前持多仓{9},当前持空仓{10}".
format(str(lybbi),str(ma1[-1]),str(ma2[-1]),str(ma3[-1]),str(ma4[-1]),str(dataclose[-1]),str(lydif[-1]),str(lydea[-1]),str(lymacd[-1]),str(context.lyportfoliowcj.buy_quantity),str(context.lyportfoliowcj.sell_quantity)));
if orderid == -1:#下单失败
WriteTxtLog(context,"开空:空单下单失败");
else:
InitParams(context);
#time.sleep(10);
lykkorderinfo = get_orders_id(orderid,context.account);
if lykkorderinfo == None:
WriteTxtLog(context,"开空:获取开空下单信息失败,单号{0}".format(orderid));
else:
lykdorderinfo = get_orders_id(orderid,context.account);
if lykdorderinfo != None:
WriteTxtLog(context,"开空:空单下单成功,订单号{0},手数{1},下单价格{2}".
format(lykkorderinfo.order_id,str(lykkorderinfo.quantity),str(lykkorderinfo.price)));
def InitParams(context):
context.lyportfolio=get_portfolio (context.lystockcode,0,context.account);#扣减未成交
context.lyportfoliowcj=get_portfolio (context.lystockcode,0,context.account,False);#不扣减未成交品种
pass;
#计算开仓手数 用该品种可投入的最大资金的30%做投资
def getkcss(context,highopen):
return 1;
#获取合约的单位乘数get_dynainf(context.lystockcode,209)
#获取合约的最新价get_dynainf(context.lystockcode,7) 两者乘积即是开一手需要的保证金
unit = int(context.lyaccountcash*0.3/(highopen* get_dynainf(context.lystockcode,209)));
#WriteTxtLog(context,"计算开仓手数,可用资金{0},开单价{1},开仓手数{2}".format(str(context.lyaccountcash),str(highopen),str(unit)));
if unit <1:
unit = 1;
return unit;
InitParams函数用户在bar更新、开仓、平仓后获取持仓信息,以上策略在回测时均正常
然鹅,在模拟盘跑时出现以下问题:(开仓固定只开1手)
1.get_portfolio 获取不到持仓信息,导致我开了仓下一根满足条件还会开仓,实际是有持仓就不应该开仓,原因未知,请帮忙查看。
2. lykdorderinfo = get_orders_id(orderid,context.account);模拟时在下单后用orderid获取委托信息,总是获取不到,使用time.sleep(10)后在获取,依旧是同样效果,请帮忙查看。
以上两个问题在回测时均不存在,回测都是正常,就是在模拟盘运行会出现。请帮忙查看问题所在
|
-
|