From e5158b096b5641530cfbf05c852fb124f9bdac06 Mon Sep 17 00:00:00 2001 From: msincenselee Date: Tue, 20 Oct 2015 22:54:57 +0800 Subject: [PATCH] save demostrategy data in backtesting --- vn.strategy/strategydemo/backtestingEngine.py | 14 ++++++++++++-- vn.strategy/strategydemo/demoBacktesting.py | 6 +++--- vn.strategy/strategydemo/strategyEngine.py | 10 ++++++++-- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/vn.strategy/strategydemo/backtestingEngine.py b/vn.strategy/strategydemo/backtestingEngine.py index 5e0b04ff..28634b65 100644 --- a/vn.strategy/strategydemo/backtestingEngine.py +++ b/vn.strategy/strategydemo/backtestingEngine.py @@ -384,12 +384,22 @@ class BacktestingEngine(object): values = '' print u'共{0}条交易记录.'.format(len(self.listTrade)) + + if len(self.listTrade) == 0: + return + for tradeItem in self.listTrade: if len(values) > 0: 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, tradeItem['InstrumentID'], tradeItem['OrderRef'], @@ -398,7 +408,7 @@ class BacktestingEngine(object): tradeItem['OffsetFlag'], tradeItem['Price'], 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) diff --git a/vn.strategy/strategydemo/demoBacktesting.py b/vn.strategy/strategydemo/demoBacktesting.py index 89f834ae..f26e5f26 100644 --- a/vn.strategy/strategydemo/demoBacktesting.py +++ b/vn.strategy/strategydemo/demoBacktesting.py @@ -8,7 +8,7 @@ import decimal def main(): """回测程序主函数""" # symbol = 'IF1506' - symbol = 'a' + symbol = 'ag' # 创建回测引擎 be = BacktestingEngine() @@ -22,14 +22,14 @@ def main(): be.connectMysql() # be.loadMongoDataHistory(symbol, datetime(2015,5,1), datetime.today()) # 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['fastAlpha'] = 0.2 setting['slowAlpha'] = 0.05 # 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) diff --git a/vn.strategy/strategydemo/strategyEngine.py b/vn.strategy/strategydemo/strategyEngine.py index b3252a99..95b3788c 100644 --- a/vn.strategy/strategydemo/strategyEngine.py +++ b/vn.strategy/strategydemo/strategyEngine.py @@ -347,6 +347,8 @@ class StrategyEngine(object): # cx = cur.fetchall() fetch_counts = 0 + cx = None + fetch_size = 1000 while True: @@ -355,8 +357,6 @@ class StrategyEngine(object): if not results: break - - if fetch_counts == 0: cx = results else: @@ -421,6 +421,9 @@ class StrategyEngine(object): print u'共{0}条Bar记录.'.format(len(barList)) + if len(barList) == 0: + return + steps = 0 for bar in barList: @@ -478,6 +481,9 @@ class StrategyEngine(object): print u'共{0}条EMA记录.'.format(len(emaList)) + if len(emaList) == 0: + return + steps = 0 for ema in emaList: