Merge branch 'dev' of https://github.com/vnpy/vnpy into dev

This commit is contained in:
vn.py 2018-06-11 22:07:16 +08:00
commit 70092a628f
3 changed files with 47 additions and 7 deletions

View File

@ -22,9 +22,10 @@ EVENT_ALGO_VAR = 'eAlgoVar' # 算法变量事件
EVENT_ALGO_SETTING = 'eAlgoSetting' # 算法配置事件
ALGOTRADING_DB_NAME = 'VnTrader_AlgoTrading_Db'
ALGOTRADING_DB_NAME = 'VnTrader_AlgoTrading_Db' # AlgoTrading数据库名
SETTING_COLLECTION_NAME = 'AlgoSetting'
SETTING_COLLECTION_NAME = 'AlgoSetting' # 算法配置集合名
HISTORY_COLLECTION_NAME = 'AlgoHistory' # 算法历史集合名
ALGO_DICT = {
@ -47,6 +48,7 @@ class AlgoEngine(object):
self.orderAlgoDict = {} # vtOrderID:algo
self.symbolAlgoDict = {} # vtSymbol:algo set
self.settingDict = {} # settingName:setting
self.historyDict = {} # algoName:dict
self.registerEvent()
@ -103,7 +105,9 @@ class AlgoEngine(object):
templateName = algoSetting['templateName']
algoClass = ALGO_DICT[templateName]
algo = algoClass.new(self, algoSetting)
self.algoDict[algo.algoName] = algo
return algo.algoName
#----------------------------------------------------------------------
@ -216,18 +220,44 @@ class AlgoEngine(object):
#----------------------------------------------------------------------
def putVarEvent(self, algo, d):
"""更新变量"""
d['algoName'] = algo.algoName
algoName = algo.algoName
d['algoName'] = algoName
event = Event(EVENT_ALGO_VAR)
event.dict_['data'] = d
self.eventEngine.put(event)
# 保存数据到数据库
history = self.historyDict.setdefault(algoName, {})
history['algoName'] = algoName
history['var'] = d
self.mainEngine.dbUpdate(ALGOTRADING_DB_NAME,
HISTORY_COLLECTION_NAME,
history,
{'algoName': algoName},
True)
#----------------------------------------------------------------------
def putParamEvent(self, algo, d):
"""更新参数"""
d['algoName'] = algo.algoName
algoName = algo.algoName
d['algoName'] = algoName
event = Event(EVENT_ALGO_PARAM)
event.dict_['data'] = d
self.eventEngine.put(event)
# 保存数据到数据库
history = self.historyDict.setdefault(algoName, {})
history['algoName'] = algoName
history['param'] = d
self.mainEngine.dbUpdate(ALGOTRADING_DB_NAME,
HISTORY_COLLECTION_NAME,
history,
{'algoName': algoName},
True)
#----------------------------------------------------------------------
def getTick(self, algo, vtSymbol):

View File

@ -1,6 +1,7 @@
# encoding: UTF-8
from __future__ import division
from datetime import datetime
from vnpy.trader.vtConstant import STATUS_NOTTRADED, STATUS_PARTTRADED, STATUS_UNKNOWN
@ -13,15 +14,22 @@ STATUS_ACTIVE = [STATUS_NOTTRADED, STATUS_PARTTRADED, STATUS_UNKNOWN]
class AlgoTemplate(object):
"""算法模板"""
templateName = 'AlgoTemplate'
timestamp = ''
count = 0
@classmethod
#----------------------------------------------------------------------
def new(cls, engine, setting):
"""创建新对象"""
cls.count += 1
algoName = '%s_%s' %(cls.templateName, cls.count)
timestamp = datetime.now().strftime('%Y%m%d%H%M%S')
if timestamp != cls.timestamp:
cls.timestamp = timestamp
cls.count = 0
else:
cls.count += 1
algoName = '_'.join([cls.templateName, cls.timestamp, str(cls.count)])
algo = cls(engine, setting, algoName)
return algo

View File

@ -347,6 +347,7 @@ class AlgoSettingMonitor(QtWidgets.QTableWidget):
self.cellDict[settingName] = {
'start': buttonStart,
'template': cellTemplateName,
'text': cellSettingText,
'delete': buttonDelete
}
@ -354,6 +355,7 @@ class AlgoSettingMonitor(QtWidgets.QTableWidget):
else:
d = self.cellDict[settingName]
d['start'].updateSetting(setting)
d['template'].setText(setting['templateName'])
d['text'].setText(self.generateText(setting))
d['delete'].updateSetting(setting)