Skip to content

Commit e1a86ef

Browse files
committed
BUG: Fix Position.pl sometimes not matching Position.pl_pct in sign
Fixes #1240
1 parent cd4bc6c commit e1a86ef

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

backtesting/backtesting.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -368,10 +368,8 @@ def pl(self) -> float:
368368
@property
369369
def pl_pct(self) -> float:
370370
"""Profit (positive) or loss (negative) of the current position in percent."""
371-
weights = np.abs([trade.size for trade in self.__broker.trades])
372-
weights = weights / weights.sum()
373-
pl_pcts = np.array([trade.pl_pct for trade in self.__broker.trades])
374-
return (pl_pcts * weights).sum()
371+
total_invested = sum(trade.entry_price * abs(trade.size) for trade in self.__broker.trades)
372+
return (self.pl / total_invested) * 100 if total_invested else 0
375373

376374
@property
377375
def is_long(self) -> bool:

0 commit comments

Comments
 (0)