Closed cjdsellers closed 1 year ago
This has been confirmed and now fixed on develop
from d140d9eb4520766dafe53e8bbd15bbc94744b5e5.
The issue was that when flipping the position - the entire filled quantity was being booked for realized PnL, rather than the positions current open quantity. So the fixes were along the lines of:
position.pyx
cdef double _calculate_pnl(
self,
double avg_px_open,
double avg_px_close,
double quantity,
):
# Only book open quantity towards PnL
quantity = fmin(quantity, fabs(self.signed_qty)) # <-----------------------
if self.is_inverse:
# In base currency
return quantity * self.multiplier.as_f64_c() * self._calculate_points_inverse(avg_px_open, avg_px_close)
else:
# In quote currency
return quantity * self.multiplier.as_f64_c() * self._calculate_points(avg_px_open, avg_px_close)
And also during cash account calculations:
cdef double fill_qty = fill.last_qty.as_f64_c()
cdef double fill_px = fill.last_px.as_f64_c()
if position is not None:
# Only book open quantity towards realized PnL
fill_qty = fmin(fill_qty, position.quantity.as_f64_c())
Bug Report
Reported from @Scout in Discord.
Expected Behavior
Realized PnL in this scenario should be 88.68245000
Actual Behavior
Realized PnL was 113.7286600000225 (overstated)