撤单终结版的代码例程
#!/usr/bin/env python
coding:utf-8
from PoboAPI import *
import datetime, time
import numpy as np
from copy import *
import random
开始事件
def OnStart(context) :
#登录交易账号
context.myacc = None
# 回测时用这个
if context.backtest :
context.accounts["回测期权"].Login()
if context.accounts["回测期权"].Login() :
context.myacc = context.accounts["回测期权"]
# 实盘时用这个
else :
context.accounts["股票期权"].Login()
if context.accounts["股票期权"].Login() :
context.myacc = context.accounts["股票期权"]
# 设定委托价
g.price_type = PriceType(PbPriceType.Limit, limit_price_type=16, limit_price_offset=0)
g.stock = '510050.SHSE' # 标的物
# (略)
# 合约码
g.put_code, g.call_code = '', ''
# 开仓计划数
g.SKput_lots_plan, g.SKcall_lots_plan = 0, 0
# 下单uid
g.SKput_uid, g.SKcall_uid = None, None
g.BPput_uid, g.BPcall_uid = None, None
# 订单开关(下单、拆单、撤单)
g.order_SKput_onoff, g.order_SKcall_onoff = False, False
g.order_BPput_onoff, g.order_BPcall_onoff = False, False
g.sleep = 10
# 成交统计
g.sum_vol_SK, g.sum_vol_BP = 0, 0 # 累计成交量
g.temp_SK, g.temp_BP = 0, 0 # 临时变量
g.SK_price_wma, g.BP_price_wma = 0, 0 # 成交均价
g.net_hold_vol = 0 # 净持仓手数
# 开平损益结果记录
g.payoff = []
# (略)
初始化事件
def OnMarketQuotationInitialEx(context, exchange, daynight) :
if exchange != 'SHSE': return
# 风控:设置检查自成交
context.myacc.SetSelfTradeCheckFlag(True)
# 设置闹钟,收盘打印账户交易结果(省略第二个参数、闹钟不重复)
context.alarmid_close_print = SetAlarm(datetime.time(15, 3))
# (略)
# 获取当月平值期权驱动OnQuote
klinedata = GetHisData2(g.stock,BarType.Day, count=3, weight_type=1)
lastclose = klinedata[-1].close
g.atmopc = GetAtmOptionContract(g.stock, 0, lastclose, 0)
SubscribeQuote(g.atmopc)
SubscribeBar(g.stock, BarType.Day)
#(略)
# (略)
# 委托事件、撤单时间、成交时间
g.insert_time, g.cancel_time, g.trade_time = 0, 0, 0
K线更新事件
def OnBar(context, code, bartype) :
klinedata = GetHisDataByField2(g.stock,["open", "close"], bar_type=BarType.Day, count=2)
g.pre_open, g.pre_close = klinedata[0][-2], klinedata[1][-2] # 昨开、昨收
实时行情事件
def OnQuote(context, code) :
# 14:57进入集合竞价时发单会报错
if HMS() < 93100 or 112900 < HMS() < 130100 or HMS() > 145500 : return
# (略)
# --------------------获取信号------------------------------------
# (略)
# -------------------- 撤单 + 下单(含拆单)------------------------------------
# --------- 买平认沽 ---------
# 撤单
if HMS() > max(g.insert_time, g.cancel_time, g.trade_time) + g.sleep \
and g.BPput_uid != None and g.order_BPput_onoff == False :
order = context.myacc.GetOrder(g.BPput_uid)
if order != None and order.IsCanCancel() : context.myacc.CancelOrder(order)
# 下单(含拆单)
if g.order_BPput_onoff :
g.order_BPput_onoff = False
委托回报事件
def OnOrderChange(context, AccountName, order) :
if order.stgyflag == 1 : # 是本策略
g.insert_time = int(order.InsertTime.strftime('%H%M%S'))
g.cancel_time = int(order.CancelTime.strftime('%H%M%S'))
# 更新订单下达开关
# 买平认沽,买平认购
if g.net_hold_vol > 0 :
if order.UID == g.BPput_uid \
and (order.status == 32 or order.IsOrderCancelled() or order.IsOrderRejected()) :
g.order_BPput_onoff, g.BPput_uid = True, None
if order.UID == g.BPcall_uid \
and (order.status == 32 or order.IsOrderCancelled() or order.IsOrderRejected()) :
g.order_BPcall_onoff, g.BPcall_uid = True, None
# 卖开认沽
if order.UID == g.SKput_uid and g.SKput_lots_plan - g.net_hold_vol > 0 \
and (order.status == 32 or order.IsOrderCancelled() or order.IsOrderRejected()) :
g.order_SKput_onoff, g.SKput_uid = True, None
# 卖开认购
if order.UID == g.SKcall_uid and g.SKcall_lots_plan - g.net_hold_vol > 0 \
and (order.status == 32 or order.IsOrderCancelled() or order.IsOrderRejected()) :
g.order_SKcall_onoff, g.SKcall_uid = True, None
成交事件
def OnTradeDeal(context, AccountName, trade) :
if trade.stgyflag == 1 : # 是本策略
g.trade_time = int(trade.TradeTime.strftime('%H%M%S'))
SK, BK, BP, SP = False, False, False, False
if trade.bstype.BuySellFlag == '0' and trade.bstype.OffsetFlag == '0' : BK = True # 买开
if trade.bstype.BuySellFlag == '1' and trade.bstype.OffsetFlag == '0' : SK = True # 卖开
if trade.bstype.BuySellFlag == '0' and trade.bstype.OffsetFlag == '1' : BP = True # 买平
if trade.bstype.BuySellFlag == '1' and trade.bstype.OffsetFlag == '1' : SP = True # 卖平
# 计算净持仓手数
if SK : g.net_hold_vol += trade.volume
if BP : g.net_hold_vol -= trade.volume
您尚未登录,请 登录 真格量化发表回复
支持