[Add]新增CTA回测的百分比最大回撤和年化收益率

This commit is contained in:
vn.py 2018-01-09 10:02:42 +08:00
parent 5a954875d0
commit c842281d47

View File

@ -971,6 +971,7 @@ class BacktestingEngine(object):
df['return'] = (np.log(df['balance']) - np.log(df['balance'].shift(1))).fillna(0)
df['highlevel'] = df['balance'].rolling(min_periods=1,window=len(df),center=False).max()
df['drawdown'] = df['balance'] - df['highlevel']
df['ddPercent'] = df['drawdown'] / df['highlevel'] * 100
# 计算统计结果
startDate = df.index[0]
@ -982,6 +983,7 @@ class BacktestingEngine(object):
endBalance = df['balance'].iloc[-1]
maxDrawdown = df['drawdown'].min()
maxDdPercent = df['ddPercent'].min()
totalNetPnl = df['netPnl'].sum()
dailyNetPnl = totalNetPnl / totalDays
@ -999,6 +1001,7 @@ class BacktestingEngine(object):
dailyTradeCount = totalTradeCount / totalDays
totalReturn = (endBalance/self.capital - 1) * 100
annualizedReturn = totalReturn / totalDays * 240
dailyReturn = df['return'].mean() * 100
returnStd = df['return'].std() * 100
@ -1016,6 +1019,7 @@ class BacktestingEngine(object):
'lossDays': lossDays,
'endBalance': endBalance,
'maxDrawdown': maxDrawdown,
'maxDdPercent': maxDdPercent,
'totalNetPnl': totalNetPnl,
'dailyNetPnl': dailyNetPnl,
'totalCommission': totalCommission,
@ -1027,6 +1031,7 @@ class BacktestingEngine(object):
'totalTradeCount': totalTradeCount,
'dailyTradeCount': dailyTradeCount,
'totalReturn': totalReturn,
'annualizedReturn': annualizedReturn,
'dailyReturn': dailyReturn,
'returnStd': returnStd,
'sharpeRatio': sharpeRatio
@ -1053,9 +1058,11 @@ class BacktestingEngine(object):
self.output(u'起始资金:\t%s' % self.capital)
self.output(u'结束资金:\t%s' % formatNumber(result['endBalance']))
self.output(u'总收益率:\t%s' % formatNumber(result['totalReturn']))
self.output(u'总收益率:\t%s%%' % formatNumber(result['totalReturn']))
self.output(u'年化收益:\t%s%%' % formatNumber(result['annualizedReturn']))
self.output(u'总盈亏:\t%s' % formatNumber(result['totalNetPnl']))
self.output(u'最大回撤: \t%s' % formatNumber(result['maxDrawdown']))
self.output(u'最大回撤: \t%s' % formatNumber(result['maxDrawdown']))
self.output(u'百分比最大回撤: %s%%' % formatNumber(result['maxDdPercent']))
self.output(u'总手续费:\t%s' % formatNumber(result['totalCommission']))
self.output(u'总滑点:\t%s' % formatNumber(result['totalSlippage']))