Skip to content

Introduce NetPnL in Position and Trades. #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion backtesting/_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def compute_stats(
'SL': [t.sl for t in trades],
'TP': [t.tp for t in trades],
'PnL': [t.pl for t in trades],
'Commissions': [t._commissions for t in trades],
'NetPnL': [t.net_pl for t in trades],
'ReturnPct': [t.pl_pct for t in trades],
'EntryTime': [t.entry_time for t in trades],
'ExitTime': [t.exit_time for t in trades],
Expand All @@ -86,7 +88,7 @@ def compute_stats(
commissions = sum(t._commissions for t in trades)
del trades

pl = trades_df['PnL']
pl = trades_df['NetPnL']
returns = trades_df['ReturnPct']
durations = trades_df['Duration']

Expand Down
10 changes: 10 additions & 0 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,11 @@ def pl(self) -> float:
"""Profit (positive) or loss (negative) of the current position in cash units."""
return sum(trade.pl for trade in self.__broker.trades)

@property
def net_pl(self) -> float:
"""Net Profit (positive) or loss (negative) of the current position in cash units."""
return sum(trade.net_pl for trade in self.__broker.trades)

@property
def pl_pct(self) -> float:
"""Profit (positive) or loss (negative) of the current position in percent."""
Expand Down Expand Up @@ -675,6 +680,11 @@ def pl(self):
price = self.__exit_price or self.__broker.last_price
return self.__size * (price - self.__entry_price)

@property
def net_pl(self):
"""Trade profit (positive) or loss (negative) after all deductions in cash units."""
return self.pl - self._commissions

@property
def pl_pct(self):
"""Trade profit (positive) or loss (negative) in percent."""
Expand Down
5 changes: 3 additions & 2 deletions backtesting/test/_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,9 @@ def almost_equal(a, b):
for n in (SmaCross.fast, SmaCross.slow)]
self.assertSequenceEqual(
sorted(stats['_trades'].columns),
sorted(['Size', 'EntryBar', 'ExitBar', 'EntryPrice', 'ExitPrice', 'SL', 'TP',
'PnL', 'ReturnPct', 'EntryTime', 'ExitTime', 'Duration', 'Tag',
sorted(['Size', 'EntryBar', 'ExitBar', 'EntryPrice', 'ExitPrice',
'SL', 'TP', 'PnL', 'ReturnPct', 'EntryTime', 'ExitTime',
'Duration', 'Tag', 'Commissions', 'NetPnL',
*indicator_columns]))

def test_compute_stats_bordercase(self):
Expand Down