From 5cfb62fd3138687c403ae63100bf069acc643301 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 6 Aug 2018 13:56:12 +0800 Subject: [PATCH] =?UTF-8?q?[Add]DrEngine=E6=96=B0=E5=A2=9E=E6=94=B6?= =?UTF-8?q?=E7=9B=98=E6=97=B6=E9=97=B4=E5=8A=9F=E8=83=BD=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E9=83=A8=E5=88=86=E4=BA=A4=E6=98=93=E6=89=80=E6=B2=A1?= =?UTF-8?q?=E6=9C=89=E6=94=B6=E7=9B=98Tick=E6=8E=A8=E9=80=81=E5=AF=BC?= =?UTF-8?q?=E8=87=B4=E6=9C=80=E5=90=8E=E4=B8=80=E6=A0=B9K=E7=BA=BF?= =?UTF-8?q?=E7=BC=BA=E5=A4=B1=E7=9A=84=E9=97=AE=E9=A2=98=20#957=20=20#888?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/DataRecording/DR_setting.json | 1 + vnpy/trader/app/ctaStrategy/ctaTemplate.py | 7 ++++ vnpy/trader/app/dataRecorder/drEngine.py | 43 +++++++++++++++++++++- 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/examples/DataRecording/DR_setting.json b/examples/DataRecording/DR_setting.json index 5ec30169..05ae8279 100644 --- a/examples/DataRecording/DR_setting.json +++ b/examples/DataRecording/DR_setting.json @@ -1,5 +1,6 @@ { "working": true, + "marketCloseTime": "15:05:00", "tick": [ diff --git a/vnpy/trader/app/ctaStrategy/ctaTemplate.py b/vnpy/trader/app/ctaStrategy/ctaTemplate.py index 5f829d0f..f6b6a465 100644 --- a/vnpy/trader/app/ctaStrategy/ctaTemplate.py +++ b/vnpy/trader/app/ctaStrategy/ctaTemplate.py @@ -449,6 +449,13 @@ class BarGenerator(object): # 清空老K线缓存对象 self.xminBar = None + #---------------------------------------------------------------------- + def generate(self): + """手动强制立即完成K线合成""" + self.onBar(self.bar) + self.bar = None + + ######################################################################## class ArrayManager(object): diff --git a/vnpy/trader/app/dataRecorder/drEngine.py b/vnpy/trader/app/dataRecorder/drEngine.py index ba6049af..dee29801 100644 --- a/vnpy/trader/app/dataRecorder/drEngine.py +++ b/vnpy/trader/app/dataRecorder/drEngine.py @@ -12,7 +12,7 @@ import os import copy import traceback from collections import OrderedDict -from datetime import datetime, timedelta +from datetime import datetime, timedelta, time from Queue import Queue, Empty from threading import Thread from pymongo.errors import DuplicateKeyError @@ -60,6 +60,11 @@ class DrEngine(object): self.queue = Queue() # 队列 self.thread = Thread(target=self.run) # 线程 + # 收盘相关 + self.marketCloseTime = None # 收盘时间 + self.timerCount = 0 # 定时器计数 + self.lastTimerTime = None # 上一次记录时间 + # 载入设置,订阅行情 self.loadSetting() @@ -79,6 +84,11 @@ class DrEngine(object): working = drSetting['working'] if not working: return + + # 加载收盘时间 + if 'marketCloseTime' in drSetting: + timestamp = drSetting['marketCloseTime'] + self.marketCloseTime = datetime.strptime(timestamp, '%H:%M:%S').time() # Tick记录配置 if 'tick' in drSetting: @@ -185,7 +195,37 @@ class DrEngine(object): bm = self.bgDict.get(vtSymbol, None) if bm: bm.updateTick(tick) + + #---------------------------------------------------------------------- + def processTimerEvent(self, event): + """处理定时事件""" + # 如果没有设置收盘时间,则无需处理 + if not self.marketCloseTime: + return + # 10秒检查一次 + self.timerCount += 1 + if self.timerCount < 10: + return + self.timerCount = 0 + + # 获取当前时间 + currentTime = datetime.now().time() + + if not self.lastTimerTime: + self.lastTimerTime = currentTime + return + + # 上一个时间戳尚未到收盘时间,且当前时间戳已经到收盘时间 + if (self.lastTimerTime < self.marketCloseTime and + currentTime >= self.marketCloseTime): + # 强制所有的K线生成器立即完成K线 + for bg in self.bgDict.values(): + bg.generate() + + # 记录新的时间 + self.lastTimerTime = currentTime + #---------------------------------------------------------------------- def onTick(self, tick): """Tick更新""" @@ -227,6 +267,7 @@ class DrEngine(object): def registerEvent(self): """注册事件监听""" self.eventEngine.register(EVENT_TICK, self.procecssTickEvent) + self.eventEngine.register(EVENT_TIMER, self.processTimerEvent) #---------------------------------------------------------------------- def insertData(self, dbName, collectionName, data):