Skip to content

Apply Commissions to PnL and PnL Percentage. #1279

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

Merged
merged 2 commits into from
Jun 6, 2025
Merged
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
13 changes: 10 additions & 3 deletions backtesting/backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,15 +671,22 @@ def is_short(self):

@property
def pl(self):
"""Trade profit (positive) or loss (negative) in cash units."""
"""
Trade profit (positive) or loss (negative) in cash units.
Commissions are reflected only after the Trade is closed.
"""
price = self.__exit_price or self.__broker.last_price
return self.__size * (price - self.__entry_price)
return (self.__size * (price - self.__entry_price)) - self._commissions

@property
def pl_pct(self):
"""Trade profit (positive) or loss (negative) in percent."""
price = self.__exit_price or self.__broker.last_price
return copysign(1, self.__size) * (price / self.__entry_price - 1)
gross_pl_pct = copysign(1, self.__size) * (price / self.__entry_price - 1)

# Total commission across the entire trade size to individual units
commission_pct = self._commissions / (abs(self.__size) * self.__entry_price)
return gross_pl_pct - commission_pct

@property
def value(self):
Expand Down