2015-10-16 08:50:44 +00:00
|
|
|
|
# encoding: UTF-8
|
|
|
|
|
|
2016-02-04 12:41:37 +00:00
|
|
|
|
'''
|
|
|
|
|
本文件包含了CTA引擎中的策略开发用模板,开发策略时需要继承CtaTemplate类。
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
from ctaBase import *
|
2015-10-16 08:50:44 +00:00
|
|
|
|
from vtConstant import *
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
2016-02-04 12:41:37 +00:00
|
|
|
|
class CtaTemplate(object):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""CTA策略模板"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
|
|
|
|
# 策略类的名称和作者
|
|
|
|
|
className = 'CtaTemplate'
|
|
|
|
|
author = EMPTY_UNICODE
|
|
|
|
|
|
|
|
|
|
# MongoDB数据库的名称,K线数据库默认为1分钟
|
|
|
|
|
tickDbName = TICK_DB_NAME
|
|
|
|
|
barDbName = MINUTE_DB_NAME
|
|
|
|
|
|
|
|
|
|
# 策略的基本参数
|
|
|
|
|
name = EMPTY_UNICODE # 策略实例名称
|
|
|
|
|
vtSymbol = EMPTY_STRING # 交易的合约vt系统代码
|
2016-05-13 14:04:29 +00:00
|
|
|
|
productClass = EMPTY_STRING # 产品类型(只有IB接口需要)
|
|
|
|
|
currency = EMPTY_STRING # 货币(只有IB接口需要)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
|
|
|
|
# 策略的基本变量,由引擎管理
|
|
|
|
|
inited = False # 是否进行了初始化
|
|
|
|
|
trading = False # 是否启动交易,由引擎管理
|
|
|
|
|
pos = 0 # 持仓情况
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
# 参数列表,保存了参数的名称
|
2016-02-04 12:41:37 +00:00
|
|
|
|
paramList = ['name',
|
|
|
|
|
'className',
|
|
|
|
|
'author',
|
|
|
|
|
'vtSymbol']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
# 变量列表,保存了变量的名称
|
2016-02-04 12:41:37 +00:00
|
|
|
|
varList = ['inited',
|
|
|
|
|
'trading',
|
|
|
|
|
'pos']
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-04 12:41:37 +00:00
|
|
|
|
def __init__(self, ctaEngine, setting):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""Constructor"""
|
|
|
|
|
self.ctaEngine = ctaEngine
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
|
|
|
|
# 设置策略的参数
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if setting:
|
2016-02-04 12:41:37 +00:00
|
|
|
|
d = self.__dict__
|
|
|
|
|
for key in self.paramList:
|
|
|
|
|
if key in setting:
|
|
|
|
|
d[key] = setting[key]
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-04 12:41:37 +00:00
|
|
|
|
def onInit(self):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""初始化策略(必须由用户继承实现)"""
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-04 12:41:37 +00:00
|
|
|
|
def onStart(self):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""启动策略(必须由用户继承实现)"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
raise NotImplementedError
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-04 12:41:37 +00:00
|
|
|
|
def onStop(self):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""停止策略(必须由用户继承实现)"""
|
|
|
|
|
raise NotImplementedError
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onTick(self, tick):
|
|
|
|
|
"""收到行情TICK推送(必须由用户继承实现)"""
|
|
|
|
|
raise NotImplementedError
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
2015-10-16 08:50:44 +00:00
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onOrder(self, order):
|
|
|
|
|
"""收到委托变化推送(必须由用户继承实现)"""
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onTrade(self, trade):
|
|
|
|
|
"""收到成交推送(必须由用户继承实现)"""
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onBar(self, bar):
|
|
|
|
|
"""收到Bar推送(必须由用户继承实现)"""
|
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def buy(self, price, volume, stop=False):
|
|
|
|
|
"""买开"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
return self.sendOrder(CTAORDER_BUY, price, volume, stop)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def sell(self, price, volume, stop=False):
|
|
|
|
|
"""卖平"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
return self.sendOrder(CTAORDER_SELL, price, volume, stop)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def short(self, price, volume, stop=False):
|
|
|
|
|
"""卖开"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
return self.sendOrder(CTAORDER_SHORT, price, volume, stop)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def cover(self, price, volume, stop=False):
|
|
|
|
|
"""买平"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
return self.sendOrder(CTAORDER_COVER, price, volume, stop)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def sendOrder(self, orderType, price, volume, stop=False):
|
|
|
|
|
"""发送委托"""
|
2015-10-16 08:50:44 +00:00
|
|
|
|
if self.trading:
|
|
|
|
|
# 如果stop为True,则意味着发本地停止单
|
|
|
|
|
if stop:
|
2016-02-21 09:10:42 +00:00
|
|
|
|
vtOrderID = self.ctaEngine.sendStopOrder(self.vtSymbol, orderType, price, volume, self)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
else:
|
2016-02-21 09:10:42 +00:00
|
|
|
|
vtOrderID = self.ctaEngine.sendOrder(self.vtSymbol, orderType, price, volume, self)
|
|
|
|
|
return vtOrderID
|
2015-10-16 08:50:44 +00:00
|
|
|
|
else:
|
2016-06-23 14:20:34 +00:00
|
|
|
|
# 交易停止时发单返回空字符串
|
|
|
|
|
return ''
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-21 09:10:42 +00:00
|
|
|
|
def cancelOrder(self, vtOrderID):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""撤单"""
|
2016-06-23 14:20:34 +00:00
|
|
|
|
# 如果发单号为空字符串,则不进行后续操作
|
|
|
|
|
if not vtOrderID:
|
|
|
|
|
return
|
|
|
|
|
|
2016-02-21 09:10:42 +00:00
|
|
|
|
if STOPORDERPREFIX in vtOrderID:
|
|
|
|
|
self.ctaEngine.cancelStopOrder(vtOrderID)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
else:
|
2016-02-21 09:10:42 +00:00
|
|
|
|
self.ctaEngine.cancelOrder(vtOrderID)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def insertTick(self, tick):
|
|
|
|
|
"""向数据库中插入tick数据"""
|
|
|
|
|
self.ctaEngine.insertData(self.tickDbName, self.vtSymbol, tick)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def insertBar(self, bar):
|
|
|
|
|
"""向数据库中插入bar数据"""
|
|
|
|
|
self.ctaEngine.insertData(self.barDbName, self.vtSymbol, bar)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-04 12:41:37 +00:00
|
|
|
|
def loadTick(self, days):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""读取tick数据"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
return self.ctaEngine.loadTick(self.tickDbName, self.vtSymbol, days)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
2016-02-04 12:41:37 +00:00
|
|
|
|
def loadBar(self, days):
|
2015-10-16 08:50:44 +00:00
|
|
|
|
"""读取bar数据"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
return self.ctaEngine.loadBar(self.barDbName, self.vtSymbol, days)
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def writeCtaLog(self, content):
|
|
|
|
|
"""记录CTA日志"""
|
2016-02-04 12:41:37 +00:00
|
|
|
|
content = self.name + ':' + content
|
2015-10-16 08:50:44 +00:00
|
|
|
|
self.ctaEngine.writeCtaLog(content)
|
2016-02-04 12:41:37 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def putEvent(self):
|
|
|
|
|
"""发出策略状态变化事件"""
|
|
|
|
|
self.ctaEngine.putStrategyEvent(self.name)
|
2016-08-03 13:54:40 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def getEngineType(self):
|
|
|
|
|
"""查询当前运行的环境"""
|
|
|
|
|
return self.ctaEngine.engineType
|
2015-10-16 08:50:44 +00:00
|
|
|
|
|
2017-03-05 07:51:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
########################################################################
|
|
|
|
|
class TargetPosTemplate(CtaTemplate):
|
|
|
|
|
"""
|
|
|
|
|
允许直接通过修改目标持仓来实现交易的策略模板
|
|
|
|
|
|
|
|
|
|
开发策略时,无需再调用buy/sell/cover/short这些具体的委托指令,
|
|
|
|
|
只需在策略逻辑运行完成后调用setTargetPos设置目标持仓,底层算法
|
|
|
|
|
会自动完成相关交易,适合不擅长管理交易挂撤单细节的用户。
|
|
|
|
|
|
|
|
|
|
使用该模板开发策略时,请在以下回调方法中先调用母类的方法:
|
|
|
|
|
onTick
|
|
|
|
|
onBar
|
|
|
|
|
onOrder
|
|
|
|
|
|
|
|
|
|
假设策略名为TestStrategy,请在onTick回调中加上:
|
|
|
|
|
super(TestStrategy, self).onTick(tick)
|
|
|
|
|
|
|
|
|
|
其他方法类同。
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
className = 'TargetPosTemplate'
|
|
|
|
|
author = u'量衍投资'
|
|
|
|
|
|
|
|
|
|
# 目标持仓模板的基本变量
|
|
|
|
|
tickAdd = 1 # 委托时相对基准价格的超价
|
|
|
|
|
lastTick = None # 最新tick数据
|
|
|
|
|
lastBar = None # 最新bar数据
|
|
|
|
|
targetPos = EMPTY_INT # 目标持仓
|
|
|
|
|
orderList = [] # 委托号列表
|
|
|
|
|
|
|
|
|
|
# 变量列表,保存了变量的名称
|
|
|
|
|
varList = ['inited',
|
|
|
|
|
'trading',
|
|
|
|
|
'pos',
|
|
|
|
|
'targetPos']
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def __init__(self, ctaEngine, setting):
|
|
|
|
|
"""Constructor"""
|
|
|
|
|
super(TargetPosTemplate, self).__init__(ctaEngine, setting)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onTick(self, tick):
|
|
|
|
|
"""收到行情推送"""
|
|
|
|
|
self.lastTick = tick
|
|
|
|
|
|
2017-03-07 14:34:21 +00:00
|
|
|
|
# 实盘模式下,启动交易后,需要根据tick的实时推送执行自动开平仓操作
|
|
|
|
|
if self.trading:
|
|
|
|
|
self.trade()
|
2017-03-05 07:51:28 +00:00
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onBar(self, bar):
|
|
|
|
|
"""收到K线推送"""
|
|
|
|
|
self.lastBar = bar
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def onOrder(self, order):
|
|
|
|
|
"""收到委托推送"""
|
2017-03-07 07:47:57 +00:00
|
|
|
|
if order.status == STATUS_ALLTRADED or order.status == STATUS_CANCELLED:
|
2017-03-05 07:51:28 +00:00
|
|
|
|
self.orderList.remove(order.vtOrderID)
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def setTargetPos(self, targetPos):
|
|
|
|
|
"""设置目标仓位"""
|
|
|
|
|
self.targetPos = targetPos
|
|
|
|
|
|
|
|
|
|
self.trade()
|
|
|
|
|
|
|
|
|
|
#----------------------------------------------------------------------
|
|
|
|
|
def trade(self):
|
|
|
|
|
"""执行交易"""
|
|
|
|
|
# 先撤销之前的委托
|
|
|
|
|
for vtOrderID in self.orderList:
|
|
|
|
|
self.cancelOrder(vtOrderID)
|
2017-03-07 14:34:21 +00:00
|
|
|
|
self.orderList = []
|
2017-03-05 07:51:28 +00:00
|
|
|
|
|
|
|
|
|
# 如果目标仓位和实际仓位一致,则不进行任何操作
|
|
|
|
|
posChange = self.targetPos - self.pos
|
|
|
|
|
if not posChange:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 确定委托基准价格,有tick数据时优先使用,否则使用bar
|
|
|
|
|
longPrice = 0
|
|
|
|
|
shortPrice = 0
|
|
|
|
|
|
|
|
|
|
if self.lastTick:
|
|
|
|
|
if posChange > 0:
|
|
|
|
|
longPrice = self.lastTick.askPrice1 + self.tickAdd
|
|
|
|
|
else:
|
|
|
|
|
shortPrice = self.lastTick.bidPrice1 - self.tickAdd
|
|
|
|
|
else:
|
|
|
|
|
if posChange > 0:
|
|
|
|
|
longPrice = self.lastBar.close + self.tickAdd
|
|
|
|
|
else:
|
|
|
|
|
shortPrice = self.lastBar.close - self.tickAdd
|
|
|
|
|
|
|
|
|
|
# 回测模式下,采用合并平仓和反向开仓委托的方式
|
|
|
|
|
if self.getEngineType() == ENGINETYPE_BACKTESTING:
|
|
|
|
|
if posChange > 0:
|
|
|
|
|
vtOrderID = self.buy(longPrice, abs(posChange))
|
|
|
|
|
else:
|
|
|
|
|
vtOrderID = self.short(shortPrice, abs(posChange))
|
|
|
|
|
self.orderList.append(vtOrderID)
|
|
|
|
|
|
|
|
|
|
# 实盘模式下,首先确保之前的委托都已经结束(全成、撤销)
|
|
|
|
|
# 然后先发平仓委托,等待成交后,再发送新的开仓委托
|
|
|
|
|
else:
|
|
|
|
|
# 检查之前委托都已结束
|
|
|
|
|
if self.orderList:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 买入
|
|
|
|
|
if posChange > 0:
|
|
|
|
|
if self.pos < 0:
|
|
|
|
|
vtOrderID = self.cover(longPrice, abs(self.pos))
|
|
|
|
|
else:
|
|
|
|
|
vtOrderID = self.buy(longPrice, abs(posChange))
|
|
|
|
|
# 卖出
|
|
|
|
|
else:
|
|
|
|
|
if self.pos > 0:
|
|
|
|
|
vtOrderID = self.sell(shortPrice, abs(self.pos))
|
|
|
|
|
else:
|
|
|
|
|
vtOrderID = self.short(shortPrice, abs(posChange))
|
|
|
|
|
self.orderList.append(vtOrderID)
|
|
|
|
|
|