Skip to content

Commit 0fce9d7

Browse files
Updated stats.py and plotting.py
1 parent 7677461 commit 0fce9d7

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

β€Žbacktesting/_plotting.pyβ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,8 @@ def plot(*, results: pd.Series,
232232

233233
df.index.name = None # Provides source name @index
234234
df['datetime'] = df.index # Save original, maybe datetime index
235-
df = df.reset_index(drop=True)
235+
df = df.copy()
236+
df['datetime'] = df.index
236237
equity_data = equity_data.reset_index(drop=True)
237238
index = df.index
238239

β€Žbacktesting/_stats.pyβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,16 @@ def compute_stats(
7575
trades_df['Duration'] = trades_df['ExitTime'] - trades_df['EntryTime']
7676
trades_df['Tag'] = [t.tag for t in trades]
7777

78+
if strategy_instance is not None and hasattr(strategy_instance, "data1") and hasattr(strategy_instance, "data2"):
79+
s1 = strategy_instance.data1.Close
80+
s2 = strategy_instance.data2.Close
81+
trades_df['PnL_cash'] = (
82+
(trades_df['ExitPrice'] - trades_df['EntryPrice']) /
83+
(s1.iloc[trades_df['EntryBar']].values + s2.iloc[trades_df['EntryBar']].values)
84+
)
85+
else:
86+
trades_df['PnL_cash'] = np.nan
87+
7888
# Add indicator values
7989
if len(trades_df) and strategy_instance:
8090
for ind in strategy_instance._indicators:

0 commit comments

Comments
Β (0)