PNL is incorrect?
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
Looking at other crypto libraries like freqtrade, fees are defined as so.
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 @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.
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"
@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.
Reopening as github is being silly