From d7c225e9fe56785853449a647a507f7e8dc6c25e Mon Sep 17 00:00:00 2001 From: msincenselee Date: Mon, 9 Nov 2015 22:13:13 +0800 Subject: [PATCH] mysql_connect --- vn.strategy/strategydemo/backtestingEngine.py | 31 ++++++++++++++++--- vn.strategy/strategydemo/demoStrategy.py | 31 ++++++++++++++++--- vn.strategy/strategydemo/strategyEngine.py | 27 +++++++++++++++- .../strategydemo/strategyProduceBar.py | 25 ++++++++++++++- 4 files changed, 104 insertions(+), 10 deletions(-) diff --git a/vn.strategy/strategydemo/backtestingEngine.py b/vn.strategy/strategydemo/backtestingEngine.py index 370887e5..95588403 100644 --- a/vn.strategy/strategydemo/backtestingEngine.py +++ b/vn.strategy/strategydemo/backtestingEngine.py @@ -12,7 +12,7 @@ from strategyEngine import * import sys import os import cPickle - +import json ######################################################################## @@ -123,9 +123,32 @@ class BacktestingEngine(object): #---------------------------------------------------------------------- def connectMysql(self): """连接MysqlDB""" + + # 载入json文件 + fileName = 'mysql_connect.json' try: - self.__mysqlConnection = MySQLdb.connect(host='vnpy.cloudapp.net', user='vnpy', - passwd='vnpy', db='stockcn', port=3306) + f = file(fileName) + except IOError: + self.writeLog(u'回测引擎读取Mysql_connect.json失败') + return + + # 解析json文件 + setting = json.load(f) + try: + mysql_host = str(setting['host']) + mysql_port = int(setting['port']) + mysql_user = str(setting['user']) + mysql_passwd = str(setting['passwd']) + mysql_db = str(setting['db']) + + + except IOError: + self.writeLog(u'回测引擎读取Mysql_connect.json,连接配置缺少字段,请检查') + return + + try: + self.__mysqlConnection = MySQLdb.connect(host=mysql_host, user=mysql_user, + passwd=mysql_passwd, db=mysql_db, port=mysql_port) self.__mysqlConnected = True self.writeLog(u'回测引擎连接MysqlDB成功') except ConnectionFailure: @@ -554,7 +577,7 @@ class BacktestingEngine(object): if tradeItem['Direction'] == '0': if tradeItem['OffsetFlag'] == '0': # 开多仓 Buy - amount = 0-float(tradeItem['Price'])*int(tradeItem['Volume']) + amount = 0 - float(tradeItem['Price'])*int(tradeItem['Volume']) else: # 平空仓 Cover amount = 0 -float(tradeItem['Price'])*int(tradeItem['Volume']) else: diff --git a/vn.strategy/strategydemo/demoStrategy.py b/vn.strategy/strategydemo/demoStrategy.py index 9f9f8835..67fd4a6b 100644 --- a/vn.strategy/strategydemo/demoStrategy.py +++ b/vn.strategy/strategydemo/demoStrategy.py @@ -212,10 +212,10 @@ class SimpleEmaStrategy(StrategyTemplate): #---------------------------------------------------------------------- def onTrade(self, trade): """交易更新""" - if trade.direction == DIRECTION_BUY: - self.pos = self.pos + trade.volume - else: - self.pos = self.pos - trade.volume + #if trade.direction == DIRECTION_BUY: + # self.pos = self.pos + trade.volume + #else: + # self.pos = self.pos - trade.volume log = self.name + u'当前持仓:' + str(self.pos) print log @@ -282,6 +282,9 @@ class SimpleEmaStrategy(StrategyTemplate): return # End added + # 每日收市平仓 + self.__dailyCloseMarket(o, t) + # 快速EMA在慢速EMA上方,做多 if self.fastEMA > self.slowEMA: # 如果当前手头无仓位,则直接做多 @@ -290,6 +293,7 @@ class SimpleEmaStrategy(StrategyTemplate): # Modified by Incense Lee :回测时,Tick数据中没有涨停价,只能使用当前价 # self.buy(self.currentTick.upperLimit, 1) self.buy(self.currentTick.lastPrice, 1, t) # 价格,数量,下单时间 + self.pos = 1 # 手头有空仓,则先平空,再开多 elif self.pos < 0: @@ -297,6 +301,7 @@ class SimpleEmaStrategy(StrategyTemplate): self.cover(self.currentTick.lastPrice, 1, t) # 价格,数量, 下单时间 # self.buy(self.currentTick.upperLimit, 1) self.buy(self.currentTick.lastPrice, 1, t) + self.pos = 1 # 反之,做空 elif self.fastEMA < self.slowEMA: @@ -304,12 +309,17 @@ class SimpleEmaStrategy(StrategyTemplate): # Modified by Incense Lee :回测时,Tick数据中没有最低价价,只能使用当前价 # self.short(self.currentTick.lowerLimit, 1) self.short(self.currentTick.lastPrice, 1, t) + self.pos = - 1 + elif self.pos > 0: # self.sell(self.currentTick.lowerLimit, 1) self.sell(self.currentTick.lastPrice, 1, t) + # self.short(self.currentTick.lowerLimit, 1) self.short(self.currentTick.lastPrice, 1, t) + + self.pos = -1 # 记录日志 log = self.name + u',K线时间:' + str(t) + '\n' + \ @@ -333,7 +343,20 @@ class SimpleEmaStrategy(StrategyTemplate): # 保存快速EMA和慢速EMA self.engine.saveEmaToMysql(id, self.lineEMA) + #---------------------------------------------------------------------- + def __dailyCloseMarket(self, o, t): + """每日收市平仓""" + if not (t.hour == 14 and t.minute == 55): + return + + if self.pos > 0: + self.sell(o, self.pos, t) + self.pos = 0 + + if self.pos < 0: + self.cover(o, 0-self.pos, t) + self.pos = 0 #---------------------------------------------------------------------- def print_log(event): diff --git a/vn.strategy/strategydemo/strategyEngine.py b/vn.strategy/strategydemo/strategyEngine.py index 66333f60..c850a1cf 100644 --- a/vn.strategy/strategydemo/strategyEngine.py +++ b/vn.strategy/strategydemo/strategyEngine.py @@ -19,6 +19,7 @@ import MySQLdb import os import sys import cPickle +import json # 常量定义 OFFSET_OPEN = '0' # 开仓 @@ -296,11 +297,35 @@ class StrategyEngine(object): # else: # return None + #---------------------------------------------------------------------- def __connectMysql(self): """连接MysqlDB""" + + # 载入json文件 + fileName = 'mysql_connect.json' try: - self.__mysqlConnection = MySQLdb.connect(host='vnpy.cloudapp.net', user='stockcn', passwd='7uhb*IJN', db='stockcn', port=3306) + f = file(fileName) + except IOError: + self.writeLog(u'回测引擎读取Mysql_connect.json失败') + return + # 解析json文件 + setting = json.load(f) + try: + mysql_host = str(setting['host']) + mysql_port = int(setting['port']) + mysql_user = str(setting['user']) + mysql_passwd = str(setting['passwd']) + mysql_db = str(setting['db']) + + + except IOError: + self.writeLog(u'回测引擎读取Mysql_connect.json,连接配置缺少字段,请检查') + return + + try: + self.__mysqlConnection = MySQLdb.connect(host=mysql_host, user=mysql_user, + passwd=mysql_passwd, db=mysql_db, port=mysql_port) self.__mysqlConnected = True self.writeLog(u'策略引擎连接MysqlDB成功') except ConnectionFailure: diff --git a/vn.strategy/strategydemo/strategyProduceBar.py b/vn.strategy/strategydemo/strategyProduceBar.py index 68e091bb..e8c69d3a 100644 --- a/vn.strategy/strategydemo/strategyProduceBar.py +++ b/vn.strategy/strategydemo/strategyProduceBar.py @@ -368,8 +368,31 @@ class StrategyProduceBar(StrategyTemplate): #---------------------------------------------------------------------- def __connectMysql(self): """连接MysqlDB""" + + # 载入json文件 + fileName = 'mysql_connect.json' try: - self.__mysqlConnection = MySQLdb.connect(host='vnpy.cloudapp.net', user='stockcn', passwd='7uhb*IJN', db='stockcn', port=3306) + f = file(fileName) + except IOError: + self.writeLog(u'回测引擎读取Mysql_connect.json失败') + return + # 解析json文件 + setting = json.load(f) + try: + mysql_host = str(setting['host']) + mysql_port = int(setting['port']) + mysql_user = str(setting['user']) + mysql_passwd = str(setting['passwd']) + mysql_db = str(setting['db']) + + + except IOError: + self.writeLog(u'回测引擎读取Mysql_connect.json,连接配置缺少字段,请检查') + return + + try: + self.__mysqlConnection = MySQLdb.connect(host=mysql_host, user=mysql_user, + passwd=mysql_passwd, db=mysql_db, port=mysql_port) self.__mysqlConnected = True print u'策略连接MysqlDB成功' except ConnectionFailure: