mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-17 17:34:39 +08:00
fix(trader): stop over-attributing entry fees on partial position closes
The FIFO matcher reduced an open trade's remaining quantity but not its remaining fee, so each subsequent partial close re-attributed entry fee that earlier closes had already counted (e.g. open 2.0 with fee 0.4, two 1.0 closes attributed 0.6 total). Deduct the consumed fee portion alongside the quantity so attributed fees sum to the fee actually paid.
This commit is contained in:
@@ -143,7 +143,11 @@ func buildClosedPosition(trade TradeRecord, side string, state *positionState) *
|
||||
|
||||
weightedSum += ot.Price * matchQty
|
||||
matchedQty += matchQty
|
||||
totalEntryFee += ot.Fee * (matchQty / ot.Quantity)
|
||||
// Attribute the entry fee proportionally and deduct the consumed
|
||||
// portion from the open trade, so a later partial close cannot
|
||||
// re-attribute fee that was already counted.
|
||||
feePortion := ot.Fee * (matchQty / ot.Quantity)
|
||||
totalEntryFee += feePortion
|
||||
|
||||
if entryTime.IsZero() {
|
||||
entryTime = ot.Time
|
||||
@@ -151,6 +155,7 @@ func buildClosedPosition(trade TradeRecord, side string, state *positionState) *
|
||||
|
||||
remainingQty -= matchQty
|
||||
ot.Quantity -= matchQty
|
||||
ot.Fee -= feePortion
|
||||
|
||||
// Remove fully consumed open trade
|
||||
if ot.Quantity <= dustQuantityEpsilon {
|
||||
|
||||
Reference in New Issue
Block a user