save demostrategy data in backtesting

This commit is contained in:
msincenselee 2015-10-20 22:54:57 +08:00
parent 94399f2250
commit e5158b096b
3 changed files with 23 additions and 7 deletions

View File

@ -384,12 +384,22 @@ class BacktestingEngine(object):
values = '' values = ''
print u'{0}条交易记录.'.format(len(self.listTrade)) print u'{0}条交易记录.'.format(len(self.listTrade))
if len(self.listTrade) == 0:
return
for tradeItem in self.listTrade: for tradeItem in self.listTrade:
if len(values) > 0: if len(values) > 0:
values = values + ',' values = values + ','
values = values + '(\'{0}\',\'{1}\',{2},{3},{4},{5},{6},{7},\'{8}\')'.format( if tradeItem['OffsetFlag'] == '0':
amount = 0-float(tradeItem['Price'])*int(tradeItem['Volume'])
else:
amount = float(tradeItem['Price'])*int(tradeItem['Volume'])
values = values + '(\'{0}\',\'{1}\',{2},{3},{4},{5},{6},{7},\'{8}\',{9})'.format(
self.Id, self.Id,
tradeItem['InstrumentID'], tradeItem['InstrumentID'],
tradeItem['OrderRef'], tradeItem['OrderRef'],
@ -398,7 +408,7 @@ class BacktestingEngine(object):
tradeItem['OffsetFlag'], tradeItem['OffsetFlag'],
tradeItem['Price'], tradeItem['Price'],
tradeItem['Volume'], tradeItem['Volume'],
tradeItem['TradeTime'].strftime('%Y-%m-%d %H:%M:%S')) tradeItem['TradeTime'].strftime('%Y-%m-%d %H:%M:%S'),amount)
cur = self.__mysqlConnection.cursor(MySQLdb.cursors.DictCursor) cur = self.__mysqlConnection.cursor(MySQLdb.cursors.DictCursor)

View File

@ -8,7 +8,7 @@ import decimal
def main(): def main():
"""回测程序主函数""" """回测程序主函数"""
# symbol = 'IF1506' # symbol = 'IF1506'
symbol = 'a' symbol = 'ag'
# 创建回测引擎 # 创建回测引擎
be = BacktestingEngine() be = BacktestingEngine()
@ -22,14 +22,14 @@ def main():
be.connectMysql() be.connectMysql()
# be.loadMongoDataHistory(symbol, datetime(2015,5,1), datetime.today()) # be.loadMongoDataHistory(symbol, datetime(2015,5,1), datetime.today())
# be.loadMongoDataHistory(symbol, datetime(2012,1,9), datetime(2012,1,14)) # be.loadMongoDataHistory(symbol, datetime(2012,1,9), datetime(2012,1,14))
be.loadMysqlDataHistory(symbol, datetime(2012,1,9), datetime(2012,1,14)) be.loadMysqlDataHistory(symbol, datetime(2012,6,9), datetime(2012,6,20))
# 创建策略对象 # 创建策略对象
setting = {} setting = {}
setting['fastAlpha'] = 0.2 setting['fastAlpha'] = 0.2
setting['slowAlpha'] = 0.05 setting['slowAlpha'] = 0.05
# setting['startDate'] = datetime(year=2015, month=5, day=20) # setting['startDate'] = datetime(year=2015, month=5, day=20)
setting['startDate'] = datetime(year=2012, month=1, day=9) setting['startDate'] = datetime(year=2012, month=6, day=9)
se.createStrategy(u'EMA演示策略', symbol, SimpleEmaStrategy, setting) se.createStrategy(u'EMA演示策略', symbol, SimpleEmaStrategy, setting)

View File

@ -347,6 +347,8 @@ class StrategyEngine(object):
# cx = cur.fetchall() # cx = cur.fetchall()
fetch_counts = 0 fetch_counts = 0
cx = None
fetch_size = 1000 fetch_size = 1000
while True: while True:
@ -355,8 +357,6 @@ class StrategyEngine(object):
if not results: if not results:
break break
if fetch_counts == 0: if fetch_counts == 0:
cx = results cx = results
else: else:
@ -421,6 +421,9 @@ class StrategyEngine(object):
print u'{0}条Bar记录.'.format(len(barList)) print u'{0}条Bar记录.'.format(len(barList))
if len(barList) == 0:
return
steps = 0 steps = 0
for bar in barList: for bar in barList:
@ -478,6 +481,9 @@ class StrategyEngine(object):
print u'{0}条EMA记录.'.format(len(emaList)) print u'{0}条EMA记录.'.format(len(emaList))
if len(emaList) == 0:
return
steps = 0 steps = 0
for ema in emaList: for ema in emaList: