def setorderid(context):
#检查未成交订单,将单号赋值给全局变量,避免启动策略时变量的值为0
#print("获取未成交订单编号")
Orders=get_orders(context.s1,0) #取未成交单
context.lastbuy=0
context.lastbuyping=0
context.lastsell=0
context.lastsellping=0
if not(Orders == None):
for order in Orders:
orderID=order.order_id #委托单号
sBuySell=order.side #买卖
sKp=order.position_effect #开平
sStatus=order.status #状态
#print(str(orderID)+','+sBuySell+','+sKp+','+sStatus)
if context.lastbuy==0 and sBuySell=="buy" and sKp=="open" and sStatus=="submitted":
context.lastbuy=order.order_id #单号
if context.lastsell==0 and sBuySell=="sell" and sKp=="open" and sStatus=="submitted":
context.lastsell=order.order_id #单号
if context.lastbuyping==0 and sBuySell=="sell" and sKp=="close" and sStatus=="submitted":
context.lastbuyping=order.order_id #单号
if context.lastsellping==0 and sBuySell=="buy" and sKp=="close" and sStatus=="submitted":
context.lastsellping=order.order_id #单号
nPrice=get_dynainf (context.s1,7) #获取最新价
if context.fx==1: #做多
if (context.lastbuy==0 and iDuoTotal<context.maxvol):
nOrdPrice=nCurOrdPrice-context.jiange
context.lastbuy=buy_open(context.s1,"Limit",nOrdPrice,context.vol)
print('档位价:'+str(nCurOrdPrice)+',委托价:'+str(nOrdPrice)+',开多')
if (context.lastbuyping==0 and iDuoTotal>0):
nOrdPrice=nCurOrdPrice+context.jiange
context.lastbuyping=sell_close(context.s1,"Limit",nOrdPrice,context.vol)
print('档位价:'+str(nCurOrdPrice)+',委托价:'+str(nOrdPrice)+',平多')
if context.fx==2 and iKongTotal<context.maxvol: #做空
if (context.lastsell==0):
nOrdPrice=nCurOrdPrice+context.jiange
context.lastsell=sell_open(context.s1,"Limit",nOrdPrice,context.vol)
print('档位价:'+str(nCurOrdPrice)+',委托价:'+str(nOrdPrice)+',开空')
if (context.lastsellping==0 and iKongTotal>0):
nOrdPrice=nCurOrdPrice-context.jiange
context.lastsellping=buy_close(context.s1,"Limit",nOrdPrice,context.vol)
print('档位价:'+str(nCurOrdPrice)+',委托价:'+str(nOrdPrice)+',平空')
# order_status当委托下单,成交,撤单等与下单有关的动作时,该方法就会被调用。---(选择实现)
def order_status(context,order):
#print('订单成交')
#print(order.order_book_id)
#print(str(order.order_id)+','+order.status+','+order.order_book_id)
#print('开多:'+str(context.lastbuy)+',平多:'+str(context.lastbuyping)+',开空:'+str(context.lastsell)+',平空:'+str(context.lastsellping))
#如果是成交,将对应的委托单撤销
if (order.status=="tradeing" and order.order_book_id==context.s1):
print(str(order.order_id)+'全部成交')
if order.order_id==context.lastbuy: #买入成交
if context.lastbuyping!=0:
cancel_order (context.lastbuyping)
print("买入成交之后撤平仓单,"+str(context.lastbuyping))
context.lastbuyping=0
if order.order_id==context.lastbuyping: #平多成交
if context.lastbuy!=0:
cancel_order (context.lastbuy)
print("平多成交之后撤开仓单,"+str(context.lastbuy))
context.lastbuy=0
if order.order_id==context.lastsell: #卖出成交
if context.lastsellping!=0:
cancel_order (context.lastsellping)
print("卖出成交之后撤平仓单,"+str(context.lastsellping))
context.lastsellping=0
if order.order_id==context.lastsellping: #平空成交
if context.lastsell!=0:
cancel_order (context.lastsell)
print("平空成交之后撤开仓单,"+str(context.lastsell))
context.lastsell=0
#如果是撤单,将对应的变量设置为0
if (order.status=="cancelled" and order.order_book_id==context.s1):
if order.order_id==context.lastbuy: #买入撤单
print("买入开仓撤单,"+str(context.lastbuy))
context.lastbuy=0
if order.order_id==context.lastbuyping: #平多撤单
print("平多单撤单,"+str(context.lastbuyping))
context.lastbuyping=0
if order.order_id==context.lastsell: #卖出撤单
print("卖出开仓撤单,"+str(context.lastsell))
context.lastsell=0
if order.order_id==context.lastsellping: #平空撤单
print("平空单撤单,"+str(context.lastsellping))
context.lastsellping=0