diff --git a/examples/OptionMaster/strategy_setting.json b/examples/OptionMaster/strategy_setting.json new file mode 100644 index 00000000..ef583e1c --- /dev/null +++ b/examples/OptionMaster/strategy_setting.json @@ -0,0 +1,7 @@ +[ + { + "name": "demo", + "className": "DemoStrategy", + "vtSymbols": "510050.SSE" + } +] \ No newline at end of file diff --git a/vnpy/trader/app/optionMaster/omBase.py b/vnpy/trader/app/optionMaster/omBase.py index 7db76642..35f3b5da 100644 --- a/vnpy/trader/app/optionMaster/omBase.py +++ b/vnpy/trader/app/optionMaster/omBase.py @@ -19,7 +19,8 @@ OM_DB_NAME = 'VnTrader_OptionMaster_Db' # 事件定义 EVENT_OM_LOG = 'eOmLog' -EVENT_OM_STRATEGY = 'eOmStrategy' +EVENT_OM_STRATEGY = 'eOmStrategy.' +EVENT_OM_STRATEGYLOG = 'eOmStrategyLog' ######################################################################## diff --git a/vnpy/trader/app/optionMaster/omEngine.py b/vnpy/trader/app/optionMaster/omEngine.py index b2f90c65..f7f98b2c 100644 --- a/vnpy/trader/app/optionMaster/omEngine.py +++ b/vnpy/trader/app/optionMaster/omEngine.py @@ -20,7 +20,7 @@ from vnpy.trader.vtConstant import (PRODUCT_OPTION, OPTION_CALL, OPTION_PUT, from vnpy.pricing import black, bs, crr from .omBase import (OmOption, OmUnderlying, OmChain, OmPortfolio, - EVENT_OM_LOG, EVENT_OM_STRATEGY, + EVENT_OM_LOG, EVENT_OM_STRATEGY, EVENT_OM_STRATEGYLOG, OM_DB_NAME) from .strategy import STRATEGY_CLASS @@ -257,7 +257,12 @@ class OmStrategyEngine(object): #---------------------------------------------------------------------- def writeLog(self, content): """快速发出日志事件""" - self.omEngine.writeLog(content) + log = VtLogData() + log.logContent = content + + event = Event(EVENT_OM_STRATEGYLOG) + event.dict_['data'] = log + self.eventEngine.put(event) #---------------------------------------------------------------------- def callStrategyFunc(self, strategy, func, params=None): @@ -457,6 +462,18 @@ class OmStrategyEngine(object): """触发策略状态变化事件(通常用于通知GUI更新)""" event = Event(EVENT_OM_STRATEGY+name) self.eventEngine.put(event) + + #---------------------------------------------------------------------- + def setStrategyParam(self, name, key, value): + """设置策略变量""" + if name in self.strategyDict: + strategy = self.strategyDict[name] + strategy.__setattr__(key, value) + self.writeLog(u'策略%s参数%s已修改为%s' %(name, key, value)) + else: + self.writeLog(u'策略实例不存在:' + name) + return None + #---------------------------------------------------------------------- def getStrategyVar(self, name): diff --git a/vnpy/trader/app/optionMaster/omStrategy.py b/vnpy/trader/app/optionMaster/omStrategy.py index 06ca4cee..188d81a5 100644 --- a/vnpy/trader/app/optionMaster/omStrategy.py +++ b/vnpy/trader/app/optionMaster/omStrategy.py @@ -1,7 +1,8 @@ # encoding: UTF-8 from vnpy.trader.vtConstant import (DIRECTION_LONG, DIRECTION_SHORT, - OFFSET_OPEN, OFFSET_CLOSE) + OFFSET_OPEN, OFFSET_CLOSE, + EMPTY_UNICODE) ######################################################################## @@ -68,7 +69,7 @@ class OmStrategyTemplate(object): raise NotImplementedError #---------------------------------------------------------------------- - def onTimer(self, order): + def onTimer(self): """定时推送""" raise NotImplementedError @@ -131,6 +132,13 @@ class OmStrategyTemplate(object): def putEvent(self): """发出GUI更新通知""" self.engine.putStrategyEvent(self.name) + + #---------------------------------------------------------------------- + def writeLog(self, content): + """记录日志""" + content = '%s:%s' %(self.name, content) + self.engine.writeLog(content) + diff --git a/vnpy/trader/app/optionMaster/strategy/omStrategyDemo.py b/vnpy/trader/app/optionMaster/strategy/omStrategyDemo.py new file mode 100644 index 00000000..1a4b93e1 --- /dev/null +++ b/vnpy/trader/app/optionMaster/strategy/omStrategyDemo.py @@ -0,0 +1,71 @@ +# encoding: UTF-8 + +from vnpy.trader.app.optionMaster.omStrategy import OmStrategyTemplate + + +######################################################################## +class DemoStrategy(OmStrategyTemplate): + """演示策略""" + className = 'DemoStrategy' + author = u'用Python的交易员' + + temp = 123 + + # 参数列表,保存了参数的名称 + paramList = ['name', + 'className', + 'author', + 'vtSymbols', + 'temp'] + + # 变量列表,保存了变量的名称 + varList = ['inited', + 'trading'] + + #---------------------------------------------------------------------- + def __init__(self, engine, setting): + """Constructor""" + super(DemoStrategy, self).__init__(engine, setting) + + #---------------------------------------------------------------------- + def onInit(self): + """初始化""" + self.writeLog(u'%s策略初始化' %self.name) + self.putEvent() + + #---------------------------------------------------------------------- + def onStart(self): + """启动""" + self.writeLog(u'%s策略启动' %self.name) + self.putEvent() + + #---------------------------------------------------------------------- + def onStop(self): + """停止""" + self.writeLog(u'%s策略停止' %self.name) + self.putEvent() + + #---------------------------------------------------------------------- + def onTick(self, tick): + """行情推送""" + self.writeLog(u'%s策略收到行情推送' %self.name) + self.putEvent() + + #---------------------------------------------------------------------- + def onTrade(self, trade): + """成交推送""" + self.writeLog(u'%s策略收到成交推送' %self.name) + self.putEvent() + + #---------------------------------------------------------------------- + def onOrder(self, order): + """委托推送""" + self.writeLog(u'%s策略收到委托推送' %self.name) + self.putEvent() + + #---------------------------------------------------------------------- + def onTimer(self): + """定时推送""" + self.writeLog(u'%s策略收到定时推送,自定义参数%s' %(self.name, self.temp)) + + \ No newline at end of file diff --git a/vnpy/trader/app/optionMaster/strategy_setting.json b/vnpy/trader/app/optionMaster/strategy_setting.json index f353fe60..ef583e1c 100644 --- a/vnpy/trader/app/optionMaster/strategy_setting.json +++ b/vnpy/trader/app/optionMaster/strategy_setting.json @@ -1,7 +1,7 @@ [ { - "name": "double ema", - "className": "EmaDemoStrategy", - "vtSymbol": "IF1706" + "name": "demo", + "className": "DemoStrategy", + "vtSymbols": "510050.SSE" } ] \ No newline at end of file diff --git a/vnpy/trader/app/optionMaster/uiOmStrategyManager.py b/vnpy/trader/app/optionMaster/uiOmStrategyManager.py index 924d39cc..2180c713 100644 --- a/vnpy/trader/app/optionMaster/uiOmStrategyManager.py +++ b/vnpy/trader/app/optionMaster/uiOmStrategyManager.py @@ -9,7 +9,7 @@ from vnpy.event import Event from vnpy.trader.vtEvent import * from vnpy.trader.uiBasicWidget import QtGui, QtCore, QtWidgets, BasicCell -from .omBase import EVENT_OM_LOG, EVENT_OM_STRATEGY +from .omBase import EVENT_OM_STRATEGYLOG, EVENT_OM_STRATEGY ######################################################################## @@ -17,13 +17,14 @@ class ValueMonitor(QtWidgets.QTableWidget): """参数监控""" #---------------------------------------------------------------------- - def __init__(self, parent=None): + def __init__(self, editable=False, parent=None): """Constructor""" super(ValueMonitor, self).__init__(parent) self.keyCellDict = {} self.data = None self.inited = False + self.editable = editable self.initUi() @@ -32,7 +33,9 @@ class ValueMonitor(QtWidgets.QTableWidget): """初始化界面""" self.setRowCount(1) self.verticalHeader().setVisible(False) - self.setEditTriggers(self.NoEditTriggers) + + if not self.editable: + self.setEditTriggers(self.NoEditTriggers) self.setMaximumHeight(self.sizeHint().height()) @@ -46,6 +49,8 @@ class ValueMonitor(QtWidgets.QTableWidget): col = 0 for k, v in data.items(): cell = QtWidgets.QTableWidgetItem(unicode(v)) + cell.key = k + self.keyCellDict[k] = cell self.setItem(0, col, cell) col += 1 @@ -55,7 +60,7 @@ class ValueMonitor(QtWidgets.QTableWidget): for k, v in data.items(): cell = self.keyCellDict[k] cell.setText(unicode(v)) - + ######################################################################## class StrategyManager(QtWidgets.QGroupBox): @@ -80,8 +85,8 @@ class StrategyManager(QtWidgets.QGroupBox): """初始化界面""" self.setTitle(self.name) - self.paramMonitor = ValueMonitor(self) - self.varMonitor = ValueMonitor(self) + self.paramMonitor = ValueMonitor(True, self) + self.varMonitor = ValueMonitor(False, self) height = 65 self.paramMonitor.setFixedHeight(height) @@ -113,6 +118,8 @@ class StrategyManager(QtWidgets.QGroupBox): self.setLayout(vbox) + self.paramMonitor.itemChanged.connect(self.setParam) + #---------------------------------------------------------------------- def updateMonitor(self, event=None): """显示策略最新状态""" @@ -144,6 +151,22 @@ class StrategyManager(QtWidgets.QGroupBox): def stop(self): """停止策略""" self.engine.stopStrategy(self.name) + + #---------------------------------------------------------------------- + def setParam(self, cell): + """更新参数""" + text = unicode(cell.text()) + key = cell.key + + if text.isdigit(): + if '.' in text: + value = float(text) + else: + value = int(text) + else: + value = text + + self.engine.setStrategyParam(self.name, key, value) ######################################################################## @@ -256,7 +279,7 @@ class StrategyEngineManager(QtWidgets.QWidget): def registerEvent(self): """注册事件监听""" self.signal.connect(self.updateLog) - self.eventEngine.register(EVENT_OM_LOG, self.signal.emit) + self.eventEngine.register(EVENT_OM_STRATEGYLOG, self.signal.emit)