backtesting.py icon indicating copy to clipboard operation
backtesting.py copied to clipboard

More clarity on trade pct calculation

Open ironhak opened this issue 10 months ago • 2 comments

Hello.

The stats._trades has a column called ReturnPct. By reading the docs and the source code it's not clear if the return in percentage is calculated relative to the starting capital, the current equity, or the trade volume. By reading the code:

    @property
    def pl(self):
        """Trade profit (positive) or loss (negative) in cash units."""
        price = self.__exit_price or self.__broker.last_price
        return self.__size * (price - self.__entry_price)

    @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)

it's clear that the return Pct comes from the trade volume. So it would be useful to let the user know this in the docs:

"""Trade profit (positive) or loss (negative) in percent relative to trade size.""" 

Thank's :)

ironhak avatar Mar 23 '25 15:03 ironhak

it's clear that the return Pct comes from the trade volume.

... in percent relative to trade size."""

If pl is the profit or loss of a trade (the whole trade), what else could pl_pct be? What alternatives to "relative to trade size" would there be for something like profit or loss? 😕

kernc avatar Mar 30 '25 05:03 kernc

it's clear that the return Pct comes from the trade volume.

... in percent relative to trade size."""

If pl is the profit or loss of a trade (the whole trade), what else could pl_pct be? What alternatives to "relative to trade size" would there be for something like profit or loss? 😕

Could also be relative to equity, most platform returns the pnl of a trade based on the equity on the account.

ironhak avatar Apr 05 '25 18:04 ironhak