From 46b3c96861aaa9e75c59ccf40dfc8e0591bdf6b8 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Sun, 11 Jun 2017 20:36:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=9B=AE=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vn.trader/VT_setting.json | 3 +- vn.trader/ctaStrategy/TestLogs/README.md | 1 - vn.trader/ctaStrategy/ctaHistoryData.py | 110 +-------------------- vn.trader/ctaStrategy/research/__init__.py | 0 vn.trader/logs/README.md | 1 - vn.trader/noUiMain.py | 21 ++-- vn.trader/vtEngine.py | 2 +- 7 files changed, 20 insertions(+), 118 deletions(-) delete mode 100644 vn.trader/ctaStrategy/TestLogs/README.md delete mode 100644 vn.trader/ctaStrategy/research/__init__.py delete mode 100644 vn.trader/logs/README.md diff --git a/vn.trader/VT_setting.json b/vn.trader/VT_setting.json index d06bd77e..8c59a6d1 100644 --- a/vn.trader/VT_setting.json +++ b/vn.trader/VT_setting.json @@ -2,8 +2,9 @@ "fontFamily": "微软雅黑", "fontSize": 12, - "mongoHost": "localhost", + "mongoHost": "mongodb://vnpy:vnpy@localhost", "mongoPort": 27017, + "mongoLogging": true, "darkStyle": true, "language": "chinese" diff --git a/vn.trader/ctaStrategy/TestLogs/README.md b/vn.trader/ctaStrategy/TestLogs/README.md deleted file mode 100644 index 5f94ab72..00000000 --- a/vn.trader/ctaStrategy/TestLogs/README.md +++ /dev/null @@ -1 +0,0 @@ -存放回测日志 \ No newline at end of file diff --git a/vn.trader/ctaStrategy/ctaHistoryData.py b/vn.trader/ctaStrategy/ctaHistoryData.py index 8094d577..dcaaed8c 100644 --- a/vn.trader/ctaStrategy/ctaHistoryData.py +++ b/vn.trader/ctaStrategy/ctaHistoryData.py @@ -16,9 +16,12 @@ from collections import OrderedDict import pandas as pd from ctaBase import * +from datayesClient import DatayesClient + +sys.path.append('..') from vtConstant import * from vtFunction import loadMongoSetting -from datayesClient import DatayesClient + # 以下为vn.trader和通联数据规定的交易所代码映射 @@ -353,107 +356,6 @@ def loadMcCsv(fileName, dbName, symbol): print u'插入完毕,耗时:%s' % (time()-start) -def load_ticks_from_file(file_name,symbol,trading_day): - """从csv tick文件中UnicodeDictReader读取tick - file_name,文件全路径 - symbol,合约代码,RB01, RBMI 等 - trading_day,交易日字符串 - """ - # 先读取数据到Dict,以日期时间为key - ticks = OrderedDict() - - if not os.path.isfile(file_name): - print u'{0}文件不存在'.format(file_name) - return ticks - dt = None - csvReadFile = file(file_name, 'rb') - - start_time = time.clock() - df = pd.read_csv(file_name, encoding='gbk', parse_dates=False) - df.columns = ['date', 'time', 'lastPrice', 'lastVolume', 'totalInterest', 'position', - 'bidPrice1', 'bidVolume1', 'bidPrice2', 'bidVolume2', 'bidPrice3', 'bidVolume3', - 'askPrice1', 'askVolume1', 'askPrice2', 'askVolume2', 'askPrice3', 'askVolume3', 'BS'] - readed_ticks = len(df) - - for i in range(0, len(df)): - # 日期, 时间, 成交价, 成交量, 总量, 属性(持仓增减), B1价, B1量, B2价, B2量, B3价, B3量, S1价, S1量, S2价, S2量, S3价, S3量, BS - # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 - row = df.iloc[i].to_dict() - tick = CtaTickData() - - tick.vtSymbol = symbol - tick.symbol = symbol - - tick.date = row['date'] - tick.tradingDay = trading_day - tick.time = row['time'] - - try: - tick.datetime = datetime.strptime(tick.date + ' ' + tick.time, '%Y-%m-%d %H:%M:%S') - except Exception as ex: - print u'日期转换错误:{0},{1}:{2}'.format(tick.date + ' ' + tick.time, Exception, ex) - continue - - tick.date = tick.datetime.strftime('%Y%m%d') - # 修正毫秒 - if tick.datetime.replace(microsecond=0) == dt: - # 与上一个tick的时间(去除毫秒后)相同,修改为500毫秒 - tick.datetime = tick.datetime.replace(microsecond=500) - tick.time = tick.datetime.strftime('%H:%M:%S.%f') - - else: - tick.datetime = tick.datetime.replace(microsecond=0) - tick.time = tick.datetime.strftime('%H:%M:%S.%f') - - dt = tick.datetime - - tick.lastPrice = float(row['lastPrice']) - tick.volume = int(float(row['lastVolume'])) - tick.bidPrice1 = float(row['bidPrice1']) # 叫买价(价格低) - tick.bidVolume1 = int(float(row['bidVolume1'])) - tick.askPrice1 = float(row['askPrice1']) # 叫卖价(价格高) - tick.askVolume1 = int(float(row['askVolume1'])) - - # 排除涨停/跌停的数据 - if (tick.bidPrice1 == float('1.79769E308') and tick.bidVolume1 == 0) : - tick.bidPrice1 = 0 - - if (tick.askPrice1 == float('1.79769E308') and tick.askVolume1 == 0): - tick.askPrice1 = 0 - - dtStr = tick.date + ' ' + tick.time - if dtStr not in ticks: - ticks[dtStr] = tick - if len(ticks)!= readed_ticks: - print u'分析tick对象数量{0}与读取数据数量{1}不一致'.format(len(ticks),readed_ticks) - - print u'读取{0},共加载{1}条数据,耗时:{2}seconds}'.format(file_name, readed_ticks, str(time.clock()-start_time)) - - return ticks - -def impot_ticks_from_folder(folder_path): - - for dirpath, _, file_names in os.walk(folder_path): - for file_name in file_names: - file_path = os.path.join(dirpath, file_name) - - if file_name.lower().find('.csv') != -1: - s = file_name.replace('.csv', '').split('_') - if len(s)!=2: - print u'{0} not match format'.format(file_path) - continue - - symbol = s[0] - trading_day = s[1] - - if len(trading_day)!=8: - print u'{0} trading_day not match format'.format(file_path) - continue - - ticks = load_ticks_from_file(file_name=file_path,symbol=symbol,trading_day=trading_day) - - print ('finish.') - if __name__ == '__main__': ## 简单的测试脚本可以写在这里 #from time import sleep @@ -462,7 +364,5 @@ if __name__ == '__main__': #e.downloadEquityDailyBar('000001') # 这里将项目中包含的股指日内分钟线csv导入MongoDB,作者电脑耗时大约3分钟 - #loadMcCsv('IF0000_1min.csv', MINUTE_DB_NAME, 'IF0000') + loadMcCsv('IF0000_1min.csv', MINUTE_DB_NAME, 'IF0000') - csv_ticks_folder_path = '/home/ubuntu/Ticks/SQ/2017' - impot_ticks_from_folder() \ No newline at end of file diff --git a/vn.trader/ctaStrategy/research/__init__.py b/vn.trader/ctaStrategy/research/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/vn.trader/logs/README.md b/vn.trader/logs/README.md deleted file mode 100644 index 8ae091c4..00000000 --- a/vn.trader/logs/README.md +++ /dev/null @@ -1 +0,0 @@ -存放运行日志 \ No newline at end of file diff --git a/vn.trader/noUiMain.py b/vn.trader/noUiMain.py index 236c95ee..da8111e5 100644 --- a/vn.trader/noUiMain.py +++ b/vn.trader/noUiMain.py @@ -37,6 +37,7 @@ class NoUiMain(object): self.strategies = [u'S28_HCRB'] self.g_count = 0 + self.disconnect_signal = 0 self.last_dt = datetime.now() @@ -94,18 +95,20 @@ class NoUiMain(object): self.mainEngine.writeLog(u'重新连接{0}'.format(self.gateway_name)) self.mainEngine.connect(self.gateway_name) self.connected = True + self.disconnect_signal = 0 return else: if not self.mainEngine.checkGatewayStatus(self.gateway_name): - self.mainEngine.writeLog(u'检查连接{0}异常,重新启动连接'.format(self.gateway_name)) - self.mainEngine.writeLog(u'断开连接{0}'.format(self.gateway_name)) - self.disconnect() - self.connected = False - #self.mainEngine.writeLog(u'清空数据引擎') - #self.mainEngine.clearData() - #self.mainEngine.writeLog(u'重新连接{0}'.format(self.gateway_name)) - #self.mainEngine.connect(self.gateway_name) - #self.connected = True + self.disconnect_signal += 1 + + if self.disconnect_signal >= 5: + self.mainEngine.writeLog(u'检查连接{0}异常,重新启动连接'.format(self.gateway_name)) + self.mainEngine.writeLog(u'断开连接{0}'.format(self.gateway_name)) + self.disconnect() + self.mainEngine.clearData() + self.connected = False + else: + self.disconnect_signal = 0 # def Start(self): """启动""" diff --git a/vn.trader/vtEngine.py b/vn.trader/vtEngine.py index 023fbf63..1832f768 100644 --- a/vn.trader/vtEngine.py +++ b/vn.trader/vtEngine.py @@ -54,7 +54,7 @@ class MainEngine(object): self.gatewayDict = OrderedDict() # 初始化的接口模块,以及其指定的名称,CTP是模块,value,是该模块下的多个连接配置文件,如 CTP_JR_connect.json - init_gateway_names = {'CTP': ['CTP', 'CTP_Prod', 'CTP_Post', 'CTP_EBF', 'CTP_JR']} + init_gateway_names = {'CTP': ['CTP', 'CTP_Prod', 'CTP_Post', 'CTP_EBF', 'CTP_JR', 'CTP_JR2']} # 遍历接口字典并自动创建所有的接口对象 for gatewayModule in GATEWAY_DICT.values():