1. 修复mainWindow的状态栏更新直接调用函数的bug,可能导致意外崩溃,改为使用signal
2. 删除ctaTemplate中的DataRecorder 3. 修复ctpGateway中的一个tdConnected的bug 4. 在ctaDemo中增加关于写策略类时需要注意可变对象必须在__init__函数中再次初始化的注释
This commit is contained in:
parent
880ee96702
commit
879d380bcd
@ -61,6 +61,13 @@ class DoubleEmaDemo(CtaTemplate):
|
||||
"""Constructor"""
|
||||
super(DoubleEmaDemo, self).__init__(ctaEngine, setting)
|
||||
|
||||
# 注意策略类中的可变对象属性(通常是list和dict等),在策略初始化时需要重新创建,
|
||||
# 否则会出现多个策略实例之间数据共享的情况,有可能导致潜在的策略逻辑错误风险,
|
||||
# 策略类中的这些可变对象属性可以选择不写,全都放在__init__下面,写主要是为了阅读
|
||||
# 策略时方便(更多是个编程习惯的选择)
|
||||
self.fastMa = []
|
||||
self.slowMa = []
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onInit(self):
|
||||
"""初始化策略(必须由用户继承实现)"""
|
||||
|
@ -161,111 +161,3 @@ class CtaTemplate(object):
|
||||
"""发出策略状态变化事件"""
|
||||
self.ctaEngine.putStrategyEvent(self.name)
|
||||
|
||||
|
||||
########################################################################
|
||||
class DataRecorder(CtaTemplate):
|
||||
"""
|
||||
纯粹用来记录历史数据的工具(基于CTA策略),
|
||||
建议运行在实际交易程序外的一个vn.trader实例中,
|
||||
本工具会记录Tick和1分钟K线数据。
|
||||
"""
|
||||
className = 'DataRecorder'
|
||||
author = u'用Python的交易员'
|
||||
|
||||
# 策略的基本参数
|
||||
name = EMPTY_UNICODE # 策略实例名称
|
||||
vtSymbol = EMPTY_STRING # 交易的合约vt系统代码
|
||||
|
||||
# 策略的变量
|
||||
bar = None # K线数据对象
|
||||
barMinute = EMPTY_STRING # 当前的分钟,初始化设为-1
|
||||
|
||||
# 变量列表,保存了变量的名称
|
||||
varList = ['inited',
|
||||
'trading',
|
||||
'pos',
|
||||
'barMinute']
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, ctaEngine, setting):
|
||||
"""Constructor"""
|
||||
super(DataRecorder, self).__init__(ctaEngine, setting)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onInit(self):
|
||||
"""初始化"""
|
||||
self.writeCtaLog(u'数据记录工具初始化')
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStart(self):
|
||||
"""启动策略(必须由用户继承实现)"""
|
||||
self.writeCtaLog(u'数据记录工具启动')
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onStop(self):
|
||||
"""停止策略(必须由用户继承实现)"""
|
||||
self.writeCtaLog(u'数据记录工具停止')
|
||||
self.putEvent()
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onTick(self, tick):
|
||||
"""收到行情TICK推送"""
|
||||
# 收到Tick后,首先插入到数据库里
|
||||
self.insertTick(tick)
|
||||
|
||||
# 计算K线
|
||||
tickMinute = tick.datetime.minute
|
||||
|
||||
if tickMinute != self.barMinute: # 如果分钟变了,则把旧的K线插入数据库,并生成新的K线
|
||||
if self.bar:
|
||||
self.onBar(self.bar)
|
||||
|
||||
bar = CtaBarData() # 创建新的K线,目的在于防止之前K线对象在插入Mongo中被再次修改,导致出错
|
||||
bar.vtSymbol = tick.vtSymbol
|
||||
bar.symbol = tick.symbol
|
||||
bar.exchange = tick.exchange
|
||||
|
||||
bar.open = tick.lastPrice
|
||||
bar.high = tick.lastPrice
|
||||
bar.low = tick.lastPrice
|
||||
bar.close = tick.lastPrice
|
||||
|
||||
bar.date = tick.date
|
||||
bar.time = tick.time
|
||||
bar.datetime = tick.datetime # K线的时间设为第一个Tick的时间
|
||||
|
||||
bar.volume = tick.volume
|
||||
bar.openInterest = tick.openInterest
|
||||
|
||||
self.bar = bar # 这种写法为了减少一层访问,加快速度
|
||||
self.barMinute = tickMinute # 更新当前的分钟
|
||||
|
||||
else: # 否则继续累加新的K线
|
||||
bar = self.bar # 写法同样为了加快速度
|
||||
|
||||
bar.high = max(bar.high, tick.lastPrice)
|
||||
bar.low = min(bar.low, tick.lastPrice)
|
||||
bar.close = tick.lastPrice
|
||||
|
||||
bar.volume = bar.volume + tick.volume # 成交量是累加的
|
||||
bar.openInterest = tick.openInterest # 持仓量直接更新
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onOrder(self, order):
|
||||
"""收到委托变化推送"""
|
||||
pass
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onTrade(self, trade):
|
||||
"""收到成交推送"""
|
||||
pass
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onBar(self, bar):
|
||||
"""收到Bar推送"""
|
||||
self.insertBar(bar)
|
||||
|
||||
|
||||
|
||||
|
@ -80,6 +80,11 @@ class AtrRsiStrategy(CtaTemplate):
|
||||
def __init__(self, ctaEngine, setting):
|
||||
"""Constructor"""
|
||||
super(AtrRsiStrategy, self).__init__(ctaEngine, setting)
|
||||
|
||||
# 注意策略类中的可变对象属性(通常是list和dict等),在策略初始化时需要重新创建,
|
||||
# 否则会出现多个策略实例之间数据共享的情况,有可能导致潜在的策略逻辑错误风险,
|
||||
# 策略类中的这些可变对象属性可以选择不写,全都放在__init__下面,写主要是为了阅读
|
||||
# 策略时方便(更多是个编程习惯的选择)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def onInit(self):
|
||||
|
@ -483,7 +483,7 @@ class CtpTdApi(TdApi):
|
||||
self.frontID = str(data['FrontID'])
|
||||
self.sessionID = str(data['SessionID'])
|
||||
self.loginStatus = True
|
||||
self.gateway.mdConnected = True
|
||||
self.gateway.tdConnected = True
|
||||
|
||||
log = VtLogData()
|
||||
log.gatewayName = self.gatewayName
|
||||
|
@ -10,6 +10,7 @@ from riskManager.uiRmWidget import RmEngineManager
|
||||
########################################################################
|
||||
class MainWindow(QtGui.QMainWindow):
|
||||
"""主窗口"""
|
||||
signalStatusBar = QtCore.pyqtSignal(type(Event()))
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def __init__(self, mainEngine, eventEngine):
|
||||
@ -169,7 +170,8 @@ class MainWindow(QtGui.QMainWindow):
|
||||
|
||||
self.sbCount = 0
|
||||
self.sbTrigger = 10 # 10秒刷新一次
|
||||
self.eventEngine.register(EVENT_TIMER, self.updateStatusBar)
|
||||
self.signalStatusBar.connect(self.updateStatusBar)
|
||||
self.eventEngine.register(EVENT_TIMER, self.signalStatusBar.emit)
|
||||
|
||||
#----------------------------------------------------------------------
|
||||
def updateStatusBar(self, event):
|
||||
|
Loading…
Reference in New Issue
Block a user