From c163803ad7dd7a62ed935829f7aa6cd406e069d5 Mon Sep 17 00:00:00 2001 From: Abhirath Mahipal Date: Wed, 28 May 2025 20:25:21 +0530 Subject: [PATCH 1/2] Apply Commissions to PnL. --- backtesting/backtesting.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 3a331651..9bf68a75 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -671,9 +671,12 @@ 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): From cecb18ee34bddf905267b65c6dde3dced94bf264 Mon Sep 17 00:00:00 2001 From: Abhirath Mahipal Date: Thu, 5 Jun 2025 22:19:10 +0530 Subject: [PATCH 2/2] Account for Commissions in Trade.pl_pct. --- backtesting/backtesting.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backtesting/backtesting.py b/backtesting/backtesting.py index 9bf68a75..f0b67b50 100644 --- a/backtesting/backtesting.py +++ b/backtesting/backtesting.py @@ -682,7 +682,11 @@ def pl(self): 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):