[Add]添加期权策略交易界面的参数修改功能

This commit is contained in:
vn.py 2018-01-16 13:27:08 +08:00
parent 33a65208e5
commit 62fc0bf751
7 changed files with 142 additions and 15 deletions

View File

@ -0,0 +1,7 @@
[
{
"name": "demo",
"className": "DemoStrategy",
"vtSymbols": "510050.SSE"
}
]

View File

@ -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'
########################################################################

View File

@ -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):

View File

@ -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)

View File

@ -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))

View File

@ -1,7 +1,7 @@
[
{
"name": "double ema",
"className": "EmaDemoStrategy",
"vtSymbol": "IF1706"
"name": "demo",
"className": "DemoStrategy",
"vtSymbols": "510050.SSE"
}
]

View File

@ -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)