jesse icon indicating copy to clipboard operation
jesse copied to clipboard

PNL is incorrect?

Open luceCoding opened this issue 6 months ago • 6 comments

Describe the bug Aren't fees calculated twice on the exchange? Once for buying and another for selling?

Expected behavior Fees are calculated twice, once for buying and another for selling.

EDIT 1: Maker and taker fees not accounted for. Perpetuals fee every 4-8 hours not accounted for.

Screenshots Image

luceCoding avatar Jun 24 '25 20:06 luceCoding

Looking at other crypto libraries like freqtrade, fees are defined as so.

Image

luceCoding avatar Jun 30 '25 17:06 luceCoding

this formula : fee = trading_fee * qty * (entry_price + exit_price) is incorrect. entry price+ exit price doubles the basis but not how the fees is applies. fees is charged per transaction so it should be applied once on buy and once on sell. that means 2 separate fee calcs.

this should be the correct function:

def estimate_PNL(qty: float, entry_price: float, exit_price: float, trade_type: str, trading_fee: float = 0.0) -> float: qty = abs(qty)

# Calculate gross profit/loss
profit = qty * (exit_price - entry_price)
if trade_type == 'short':
    profit *= -1

# Apply trading fees on both entry and exit
entry_fee = trading_fee * qty * entry_price
exit_fee = trading_fee * qty * exit_price
total_fee = entry_fee + exit_fee

return profit - total_fee

prajeeta15 avatar Jul 21 '25 09:07 prajeeta15

@prajeeta15 @luceCoding Just clarifying here....

your suggestion:

Apply trading fees on both entry and exit

entry_fee = trading_fee * qty * entry_price exit_fee = trading_fee * qty * exit_price total_fee = entry_fee + exit_fee

let's expand it,

total_fee = entry_fee + exit_fee = ( trading_fee * qty * entry_price) + (trading_fee * qty * exit_price) just refactor your formula and you are back to the originalcode = trading_fee*qty (entry_price + exit_price)

so this is not even a issue.

ashoktzr avatar Aug 24 '25 15:08 ashoktzr

hey @ashoktzr , you are correct both the formulas yield the same answer. it wasn't an issue per say. I was just explaining to the other person how its actually interpreted, mb I said "incorrect" I was just expanding the equation to show that "exchanges charge two separate times: once at entry and once at exit"

prajeeta15 avatar Aug 24 '25 17:08 prajeeta15

@ashoktzr Maybe I should have clarified. For most exchanges, the fee for entry is different from the exit fee, maker and taker. Also if you are dealing with perpetuals, then there is a fee every 4 or 8 hours too.

luceCoding avatar Oct 22 '25 16:10 luceCoding

Reopening as github is being silly

luceCoding avatar Oct 22 '25 16:10 luceCoding