Closed Otlk closed 1 day ago
Hey @Otlk
Thanks for the report here.
Just to check on expectations for behavior, we don't expect any realized PnL if we're adding to a position - only when reducing. With this in mind do you still think there's a bug in the PnL calculation?
At what point did the -308.23 of PnL occur prior to these logs? It looks like a position was already open but then I see the PositionOpened
event.
Also, is this instrument really quoted in USD to 4 decimal places?
Given you've identified where the PnL is calculated, do you have any suggestions for a fix at this point?
Hi @cjdsellers,
With this in mind do you still think there's a bug in the PnL calculation?
The problem is that since calculate_pnls
is called on open we could hit this condition on multiple fills, resulting in the wrong fill value:
if position is not None and position.quantity._mem.raw != 0:
# Only book open quantity towards realized PnL
fill_qty = fmin(fill_qty, position.quantity.as_f64_c())
This value is then used to incorrectly update account balances: I.e. from my logs above:
fill 1
Portfolio: Calculated PnLs: [Money(-308.20, USD)] ## = 268 * 1.15
Portfolio: Updated AccountState(... balances=[AccountBalance(total=9_691.80 USD, locked=0.00 USD, free=9_691.80 USD)]...)
Portfolio: Updated OrderFilled(...last_qty=268, last_px=1.1500 USD ...)
Portfolio: HUT.XNAS net_position=268
Account balance is correct, 10000 - (268 * 1.15) = 9_691.80 USD
fill 2
Portfolio: Calculated PnLs: [Money(-308.23, USD)] ## = 268 * 1.1501
Portfolio: Updated AccountState(...balances=[AccountBalance(total=9_383.57 USD, locked=0.00 USD, free=9_383.57 USD)]...)
Portfolio: Updated OrderFilled(...last_qty=7_993, last_px=1.1501 USD...)
Portfolio: HUT.XNAS net_position=8261
Account balance is wrong since we hit the condition: 9_691.80 - (7_993 * 1.1501) != 9_383.57 USD
Given you've identified where the PnL is calculated, do you have any suggestions for a fix at this point?
I think just checking that order side != position side is enough here ?
Also, is this instrument really quoted in USD to 4 decimal places?
Sub penny quoting is prohibited for stocks > 1$ on exchange but it occurs off exchange in ATS or wherever
@cjdsellers Changing the account type from CASH to MARGIN produce the correct output. Doing the same as this in the CASH account should fix it. Underlying cause is that the calculate pnl function in the cash account dont calculate pnl directly but via the fill "notional" value that is then added or substracted to the account balance.
Thanks @Otlk I see the issue now.
Fixed here af93bf1e497b427848e69c1eeaddba136916a453.
Bug Report
Account balance is wrong when opening a position from a market order and multiple fills are created.
Expected Behavior
Should correctly compute fill and update account balance. Here the 2nd fill should have an impact of 7993 * 1.1501 ~ 9 192.75 USD
Actual Behavior
When submitting a market order that generate multiple fills, the wrong quantity is used to determine impact of further fills. https://github.com/nautechsystems/nautilus_trader/blob/develop/nautilus_trader/accounting/accounts/cash.pyx#L338:L357
In the following logs we see that 2 fills are created but when nautilus calculate pnl for the 2nd fill, the last quantity is used instead of the new one.
If needed can provide a MRE, thanks !
Steps to Reproduce the Problem
Specifications
nautilus_trader
version: latest