mysql_connect

This commit is contained in:
msincenselee 2015-11-09 22:13:13 +08:00
parent 01d9f9e876
commit d7c225e9fe
4 changed files with 104 additions and 10 deletions

View File

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

View File

@ -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,13 +309,18 @@ 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' + \
u'快速EMA' + str(self.fastEMA) + u'慢速EMA' + \
@ -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):

View File

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

View File

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