Update backtesting.py

This commit is contained in:
1122455801 2019-03-26 15:53:22 +08:00
parent 6359fac196
commit fc1c76ce48

View File

@ -293,7 +293,7 @@ class BacktestingEngine:
self.output("逐日盯市盈亏计算完成") self.output("逐日盯市盈亏计算完成")
return self.daily_df return self.daily_df
def calculate_statistics(self, df: DataFrame = None): def calculate_statistics(self, df: DataFrame = None, Output=True):
"""""" """"""
self.output("开始计算策略统计指标") self.output("开始计算策略统计指标")
@ -325,6 +325,7 @@ class BacktestingEngine:
daily_return = 0 daily_return = 0
return_std = 0 return_std = 0
sharpe_ratio = 0 sharpe_ratio = 0
return_drawdown_ratio = 0
else: else:
# Calculate balance related time series data # Calculate balance related time series data
df["balance"] = df["net_pnl"].cumsum() + self.capital df["balance"] = df["net_pnl"].cumsum() + self.capital
@ -373,7 +374,10 @@ class BacktestingEngine:
else: else:
sharpe_ratio = 0 sharpe_ratio = 0
return_drawdown_ratio = -total_return / max_ddpercent
# Output # Output
if Output:
self.output("-" * 30) self.output("-" * 30)
self.output(f"首个交易日:\t{start_date}") self.output(f"首个交易日:\t{start_date}")
self.output(f"最后交易日:\t{end_date}") self.output(f"最后交易日:\t{end_date}")
@ -405,6 +409,7 @@ class BacktestingEngine:
self.output(f"日均收益率:\t{daily_return:,.2f}%") self.output(f"日均收益率:\t{daily_return:,.2f}%")
self.output(f"收益标准差:\t{return_std:,.2f}%") self.output(f"收益标准差:\t{return_std:,.2f}%")
self.output(f"Sharpe Ratio\t{sharpe_ratio:,.2f}") self.output(f"Sharpe Ratio\t{sharpe_ratio:,.2f}")
self.output(f"收益回撤比:\t{return_drawdown_ratio:,.2f}")
statistics = { statistics = {
"start_date": start_date, "start_date": start_date,
@ -430,6 +435,7 @@ class BacktestingEngine:
"daily_return": daily_return, "daily_return": daily_return,
"return_std": return_std, "return_std": return_std,
"sharpe_ratio": sharpe_ratio, "sharpe_ratio": sharpe_ratio,
"return_drawdown_ratio": return_drawdown_ratio,
} }
return statistics return statistics