From 0fce9d77396628f43a53583ba98150b0a98a2d5a Mon Sep 17 00:00:00 2001 From: Aryan Dhanuka Date: Wed, 1 Oct 2025 10:55:40 +0530 Subject: [PATCH] Updated stats.py and plotting.py --- backtesting/_plotting.py | 3 ++- backtesting/_stats.py | 10 ++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/backtesting/_plotting.py b/backtesting/_plotting.py index 338454da..5c5cb5aa 100644 --- a/backtesting/_plotting.py +++ b/backtesting/_plotting.py @@ -232,7 +232,8 @@ def plot(*, results: pd.Series, df.index.name = None # Provides source name @index df['datetime'] = df.index # Save original, maybe datetime index - df = df.reset_index(drop=True) + df = df.copy() + df['datetime'] = df.index equity_data = equity_data.reset_index(drop=True) index = df.index diff --git a/backtesting/_stats.py b/backtesting/_stats.py index 6905d62c..783ee91c 100644 --- a/backtesting/_stats.py +++ b/backtesting/_stats.py @@ -75,6 +75,16 @@ def compute_stats( trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime'] trades_df['Tag'] = [t.tag for t in trades] + if strategy_instance is not None and hasattr(strategy_instance, "data1") and hasattr(strategy_instance, "data2"): + s1 = strategy_instance.data1.Close + s2 = strategy_instance.data2.Close + trades_df['PnL_cash'] = ( + (trades_df['ExitPrice'] - trades_df['EntryPrice']) / + (s1.iloc[trades_df['EntryBar']].values + s2.iloc[trades_df['EntryBar']].values) + ) + else: + trades_df['PnL_cash'] = np.nan + # Add indicator values if len(trades_df) and strategy_instance: for ind in strategy_instance._indicators: