Skip to content
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