Merge pull request #26 from ideaplat/patch-3
在ctaDemo.py中增加了OrderManagementDemo,用于展示如何实现细粒度的委托管理操作。
This commit is contained in:
commit
e358d1e28e
@ -6,10 +6,8 @@
|
||||
2. 委托未成交,需要撤单后重新委托
|
||||
3. 断网后恢复交易状态
|
||||
4. 等等
|
||||
|
||||
这些点是作者选择特意忽略不去实现,因此想实盘的朋友请自己多多研究CTA交易的一些细节,
|
||||
做到了然于胸后再去交易,对自己的money和时间负责。
|
||||
|
||||
也希望社区能做出一个解决了以上潜在风险的Demo出来。
|
||||
"""
|
||||
|
||||
@ -180,5 +178,109 @@ class DoubleEmaDemo(CtaTemplate):
|
||||
"""收到成交推送(必须由用户继承实现)"""
|
||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
||||
pass
|
||||
########################################################################################
|
||||
## 基于tick级别细粒度撤单追单测试demo
|
||||
|
||||
|
||||
class OrderManagementDemo(CtaTemplate):
|
||||
"""追撤单策略Demo"""
|
||||
className = 'OrderManagementDemo'
|
||||
author = u'用Python的交易员'
|
||||
|
||||
# 策略参数
|
||||
|
||||
initDays = 10 # 初始化数据所用的天数
|
||||
|
||||
# 策略变量
|
||||
bar = None
|
||||
barMinute = EMPTY_STRING
|
||||
|
||||
|
||||
# 参数列表,保存了参数的名称
|
||||
paramList = ['name',
|
||||
'className',
|
||||
'author',
|
||||
'vtSymbol']
|
||||
|
||||
# 变量列表,保存了变量的名称
|
||||
varList = ['inited',
|
||||
'trading',
|
||||
'pos']
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, ctaEngine, setting):
|
||||
"""Constructor"""
|
||||
super(OrderManagementDemo, self).__init__(ctaEngine, setting)
|
||||
|
||||
self.lastOrder = None
|
||||
self.orderType = ''
|
||||
#----------------------------------------------------------------------
|
||||
def onInit(self):
|
||||
"""初始化策略(必须由用户继承实现)"""
|
||||
self.writeCtaLog(u'双EMA演示策略初始化')
|
||||
|
||||
initData = self.loadBar(self.initDays)
|
||||
for bar in initData:
|
||||
self.onBar(bar)
|
||||
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStart(self):
|
||||
"""启动策略(必须由用户继承实现)"""
|
||||
self.writeCtaLog(u'双EMA演示策略启动')
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStop(self):
|
||||
"""停止策略(必须由用户继承实现)"""
|
||||
self.writeCtaLog(u'双EMA演示策略停止')
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onTick(self, tick):
|
||||
"""收到行情TICK推送(必须由用户继承实现)"""
|
||||
|
||||
# 建立不成交买单测试单
|
||||
|
||||
if self.lastOrder == None:
|
||||
self.buy(tick.lastprice - 10.0, 1)
|
||||
|
||||
# CTA委托类型映射
|
||||
if self.lastOrder != None and self.lastOrder.direction == u'多' and self.lastOrder.offset == u'开仓':
|
||||
self.orderType = u'买开'
|
||||
|
||||
elif self.lastOrder != None and self.lastOrder.direction == u'多' and self.lastOrder.offset == u'平仓':
|
||||
self.orderType = u'买平'
|
||||
|
||||
elif self.lastOrder != None and self.lastOrder.direction == u'空' and self.lastOrder.offset == u'开仓':
|
||||
self.orderType = u'卖开'
|
||||
|
||||
elif self.lastOrder != None and self.lastOrder.direction == u'空' and self.lastOrder.offset == u'平仓':
|
||||
self.orderType = u'卖平'
|
||||
|
||||
# 不成交,即撤单,并追单
|
||||
if self.lastOrder != None and self.lastOrder.status == u'未成交':
|
||||
|
||||
self.cancelOrder(self.lastOrder.vtOrderID)
|
||||
self.lastOrder = None
|
||||
elif self.lastOrder != None and self.lastOrder.status == u'已撤销':
|
||||
# 追单并设置为不能成交
|
||||
|
||||
self.sendOrder(self.orderType, self.tick.lastprice - 10, 1)
|
||||
self.lastOrder = None
|
||||
#----------------------------------------------------------------------
|
||||
def onBar(self, bar):
|
||||
"""收到Bar推送(必须由用户继承实现)"""
|
||||
pass
|
||||
#----------------------------------------------------------------------
|
||||
def onOrder(self, order):
|
||||
"""收到委托变化推送(必须由用户继承实现)"""
|
||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
||||
self.lastOrder = order
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onTrade(self, trade):
|
||||
"""收到成交推送(必须由用户继承实现)"""
|
||||
# 对于无需做细粒度委托控制的策略,可以忽略onOrder
|
||||
pass
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user