Skip to content

Commit da7d22b

Browse files
BUG: Account for commissions in Trade.pl and Trade.pl_pct (#1279)
* Apply Commissions to PnL. * Account for Commissions in Trade.pl_pct.
1 parent 590dee7 commit da7d22b

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

backtesting/backtesting.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,15 +671,22 @@ def is_short(self):
671671

672672
@property
673673
def pl(self):
674-
"""Trade profit (positive) or loss (negative) in cash units."""
674+
"""
675+
Trade profit (positive) or loss (negative) in cash units.
676+
Commissions are reflected only after the Trade is closed.
677+
"""
675678
price = self.__exit_price or self.__broker.last_price
676-
return self.__size * (price - self.__entry_price)
679+
return (self.__size * (price - self.__entry_price)) - self._commissions
677680

678681
@property
679682
def pl_pct(self):
680683
"""Trade profit (positive) or loss (negative) in percent."""
681684
price = self.__exit_price or self.__broker.last_price
682-
return copysign(1, self.__size) * (price / self.__entry_price - 1)
685+
gross_pl_pct = copysign(1, self.__size) * (price / self.__entry_price - 1)
686+
687+
# Total commission across the entire trade size to individual units
688+
commission_pct = self._commissions / (abs(self.__size) * self.__entry_price)
689+
return gross_pl_pct - commission_pct
683690

684691
@property
685692
def value(self):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy