Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit da7d22b

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

1 file changed

+10-3Lines changed: 10 additions & 3 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎backtesting/backtesting.py‎

Copy file name to clipboardExpand all lines: backtesting/backtesting.py
+10-3Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.