[Mod]set all statistics to 0 if no trade in backtesting

This commit is contained in:
vn.py 2019-02-19 15:18:20 +08:00
parent 38aabe1b09
commit f467afcf5e

View File

@ -301,6 +301,32 @@ class BacktestingEngine:
if not df:
df = self.daily_df
if df is None:
# Set all statistics to 0 if no trade.
start_date = ""
end_date = ""
total_days = 0
profit_days = 0
loss_days = 0
end_balance = 0
max_drawdown = 0
max_ddpercent = 0
total_net_pnl = 0
daily_net_pnl = 0
total_commission = 0
daily_commission = 0
total_slippage = 0
daily_slippage = 0
total_turnover = 0
daily_turnover = 0
total_trade_count = 0
daily_trade_count = 0
total_return = 0
annual_return = 0
daily_return = 0
return_std = 0
sharpe_ratio = 0
else:
# Calculate balance related time series data
df["balance"] = df["net_pnl"].cumsum() + self.capital
df["return"] = np.log(df["balance"] / df["balance"].shift(1)).fillna(0)
@ -349,7 +375,6 @@ class BacktestingEngine:
sharpe_ratio = 0
# Output
# 输出统计结果
self.output("-" * 30)
self.output(f"首个交易日:\t{start_date}")
self.output(f"最后交易日:\t{end_date}")
@ -415,6 +440,9 @@ class BacktestingEngine:
if not df:
df = self.daily_df
if df is None:
return
plt.figure(figsize=(10, 16))
balance_plot = plt.subplot(4, 1, 1)