From 879d380bcd5e7783ca9de070676d2291fe6e4f03 Mon Sep 17 00:00:00 2001 From: chenxy123 Date: Sat, 11 Jun 2016 11:30:03 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E4=BF=AE=E5=A4=8DmainWindow=E7=9A=84?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=A0=8F=E6=9B=B4=E6=96=B0=E7=9B=B4=E6=8E=A5?= =?UTF-8?q?=E8=B0=83=E7=94=A8=E5=87=BD=E6=95=B0=E7=9A=84bug=EF=BC=8C?= =?UTF-8?q?=E5=8F=AF=E8=83=BD=E5=AF=BC=E8=87=B4=E6=84=8F=E5=A4=96=E5=B4=A9?= =?UTF-8?q?=E6=BA=83=EF=BC=8C=E6=94=B9=E4=B8=BA=E4=BD=BF=E7=94=A8signal=20?= =?UTF-8?q?2.=20=E5=88=A0=E9=99=A4ctaTemplate=E4=B8=AD=E7=9A=84DataRecorde?= =?UTF-8?q?r=203.=20=E4=BF=AE=E5=A4=8DctpGateway=E4=B8=AD=E7=9A=84?= =?UTF-8?q?=E4=B8=80=E4=B8=AAtdConnected=E7=9A=84bug=204.=20=E5=9C=A8ctaDe?= =?UTF-8?q?mo=E4=B8=AD=E5=A2=9E=E5=8A=A0=E5=85=B3=E4=BA=8E=E5=86=99?= =?UTF-8?q?=E7=AD=96=E7=95=A5=E7=B1=BB=E6=97=B6=E9=9C=80=E8=A6=81=E6=B3=A8?= =?UTF-8?q?=E6=84=8F=E5=8F=AF=E5=8F=98=E5=AF=B9=E8=B1=A1=E5=BF=85=E9=A1=BB?= =?UTF-8?q?=E5=9C=A8=5F=5Finit=5F=5F=E5=87=BD=E6=95=B0=E4=B8=AD=E5=86=8D?= =?UTF-8?q?=E6=AC=A1=E5=88=9D=E5=A7=8B=E5=8C=96=E7=9A=84=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/ctaAlgo/ctaDemo.py | 7 ++ vn.trader/ctaAlgo/ctaTemplate.py | 108 ---------------------------- vn.trader/ctaAlgo/strategyAtrRsi.py | 5 ++ vn.trader/ctpGateway/ctpGateway.py | 2 +- vn.trader/uiMainWindow.py | 4 +- 5 files changed, 16 insertions(+), 110 deletions(-) diff --git a/vn.trader/ctaAlgo/ctaDemo.py b/vn.trader/ctaAlgo/ctaDemo.py index 4391fb21..747be12a 100644 --- a/vn.trader/ctaAlgo/ctaDemo.py +++ b/vn.trader/ctaAlgo/ctaDemo.py @@ -61,6 +61,13 @@ class DoubleEmaDemo(CtaTemplate): """Constructor""" super(DoubleEmaDemo, self).__init__(ctaEngine, setting) + # 注意策略类中的可变对象属性(通常是list和dict等),在策略初始化时需要重新创建, + # 否则会出现多个策略实例之间数据共享的情况,有可能导致潜在的策略逻辑错误风险, + # 策略类中的这些可变对象属性可以选择不写,全都放在__init__下面,写主要是为了阅读 + # 策略时方便(更多是个编程习惯的选择) + self.fastMa = [] + self.slowMa = [] + #---------------------------------------------------------------------- def onInit(self): """初始化策略(必须由用户继承实现)""" diff --git a/vn.trader/ctaAlgo/ctaTemplate.py b/vn.trader/ctaAlgo/ctaTemplate.py index 6689b08f..7cd3150b 100644 --- a/vn.trader/ctaAlgo/ctaTemplate.py +++ b/vn.trader/ctaAlgo/ctaTemplate.py @@ -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) - - - - \ No newline at end of file diff --git a/vn.trader/ctaAlgo/strategyAtrRsi.py b/vn.trader/ctaAlgo/strategyAtrRsi.py index 9a5d654c..c838e6a2 100644 --- a/vn.trader/ctaAlgo/strategyAtrRsi.py +++ b/vn.trader/ctaAlgo/strategyAtrRsi.py @@ -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): diff --git a/vn.trader/ctpGateway/ctpGateway.py b/vn.trader/ctpGateway/ctpGateway.py index 3fae581b..1f505c0a 100644 --- a/vn.trader/ctpGateway/ctpGateway.py +++ b/vn.trader/ctpGateway/ctpGateway.py @@ -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 diff --git a/vn.trader/uiMainWindow.py b/vn.trader/uiMainWindow.py index 02833bae..b3a3d32f 100644 --- a/vn.trader/uiMainWindow.py +++ b/vn.trader/uiMainWindow.py @@ -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):