Skip to content

Commit

Permalink
Merge pull request #9780 from vegaprotocol/9770-pnl-bug
Browse files Browse the repository at this point in the history
fix: PnL flicker
  • Loading branch information
EVODelavega authored Oct 13, 2023
2 parents 35b59f2 + ad8a69d commit 27081f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
- [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add filtering for party to the referral fees API.
- [9541](https://github.com/vegaprotocol/vega/issues/9731) - Add X day aggregate totals for referral set referees.
- [2985](https://github.com/vegaprotocol/system-tests/issues/2985) - Coverage for insurance pool transfers, fix deadlock when terminating pending market through governance.
- [9770](https://github.com/vegaprotocol/vega/issues/9770) - Fix `PnL` flickering bug.

### 🐛 Fixes

Expand Down
8 changes: 3 additions & 5 deletions datanode/entities/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,16 @@ func (p *Position) UpdateWithTrade(trade vega.Trade, seller bool, pf num.Decimal
}
marketPrice, _ := num.DecimalFromString(trade.Price) // this is market price
// Scale the trade to the correct size
positionPrice := marketPrice.Mul(pf)
opened, closed := CalculateOpenClosedVolume(p.PendingOpenVolume, size)
realisedPnlDelta := positionPrice.Sub(p.PendingAverageEntryPrice).Mul(num.DecimalFromInt64(closed)).Div(pf)
realisedPnlDelta := marketPrice.Sub(p.PendingAverageEntryPrice).Mul(num.DecimalFromInt64(closed)).Div(pf)
p.PendingRealisedPnl = p.PendingRealisedPnl.Add(realisedPnlDelta)
p.PendingOpenVolume -= closed

positionPriceUint, _ := num.UintFromDecimal(positionPrice)
marketPriceUint, _ := num.UintFromDecimal(marketPrice)
p.PendingAverageEntryPrice = updateVWAP(p.PendingAverageEntryPrice, p.PendingOpenVolume, opened, positionPriceUint)
p.PendingAverageEntryPrice = updateVWAP(p.PendingAverageEntryPrice, p.PendingOpenVolume, opened, marketPriceUint)
p.PendingAverageEntryMarketPrice = updateVWAP(p.PendingAverageEntryMarketPrice, p.PendingOpenVolume, opened, marketPriceUint)
p.PendingOpenVolume += opened
p.pendingMTM(positionPrice, pf)
p.pendingMTM(marketPrice, pf)
}

func (p *Position) ApplyFundingPayment(amount *num.Int) {
Expand Down
12 changes: 6 additions & 6 deletions datanode/entities/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func TestPnLWithPositionDecimals(t *testing.T) {
position.UpdateWithTrade(trade, false, dp)
pp := position.ToProto()
assert.Equal(t, "0", pp.RealisedPnl)
assert.Equal(t, "400", pp.UnrealisedPnl)
assert.Equal(t, "0", pp.UnrealisedPnl)
// now MTM settlement event, contains the same trades, mark price is 1k
ps := events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1000), []events.TradeSettlement{
tradeStub{
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestPnLWithPositionDecimals(t *testing.T) {
position.UpdateWithTrade(trade, false, dp)
pp = position.ToProto()
assert.Equal(t, "0", pp.RealisedPnl)
assert.Equal(t, "5744", pp.UnrealisedPnl)
assert.Equal(t, "0", pp.UnrealisedPnl)
assert.EqualValues(t, 6, pp.OpenVolume)
// now assume this last trade was the only trade that occurred before MTM
ps = events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1150), []events.TradeSettlement{
Expand All @@ -236,8 +236,8 @@ func TestPnLWithPositionDecimals(t *testing.T) {
}
position.UpdateWithTrade(trade, true, dp)
pp = position.ToProto()
assert.Equal(t, "1249", pp.RealisedPnl)
assert.Equal(t, "6244", pp.UnrealisedPnl)
assert.Equal(t, "0", pp.RealisedPnl)
assert.Equal(t, "1", pp.UnrealisedPnl)
assert.EqualValues(t, 5, pp.OpenVolume)
ps = events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1250), []events.TradeSettlement{
tradeStub{
Expand All @@ -261,7 +261,7 @@ func TestPnLWithPositionDecimals(t *testing.T) {
}
position.UpdateWithTrade(trade, true, dp)
pp = position.ToProto()
assert.Equal(t, "6495", pp.RealisedPnl)
assert.Equal(t, "1", pp.RealisedPnl)
assert.Equal(t, "0", pp.UnrealisedPnl)
assert.EqualValues(t, 0, pp.OpenVolume)
ps = events.NewSettlePositionEvent(ctx, party, market, num.NewUint(1250), []events.TradeSettlement{
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestPnLWithTradeDecimals(t *testing.T) {
position.UpdateWithTrade(trade, false, dp)
pp = position.ToProto()
assert.Equal(t, "-300", pp.RealisedPnl)
assert.Equal(t, "3883", pp.UnrealisedPnl)
assert.Equal(t, "50", pp.UnrealisedPnl)
}

type tradeStub struct {
Expand Down
12 changes: 6 additions & 6 deletions datanode/networkhistory/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,12 @@ func TestMain(t *testing.M) {
log.Infof("%s", goldenSourceHistorySegment[4000].HistorySegmentID)
log.Infof("%s", goldenSourceHistorySegment[5000].HistorySegmentID)

panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "QmQYY51bDtR745ZhiiU2SMfYtnnGMVgXVXVCez5jacftto", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmcPW35HdsBUgbQNiJZms56E5TUYvKyeWY8hGbFAqADn7r", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmYxNwnS7CgzLwBTJXCjvYeFFQWQtUzEERgUSfr74stsen", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmZ1BDgrU3qHgT1fnfZoaJCnSmghcerZg2rMp6xw8G2TcR", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmTHvtzTVCRbMvNFpX68SAy2sQBgfccwnwy3Hu1jycy9T6", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmW83by4cR7WULEVirZqPgbKA9xZbGsEEMhsYMEKf3BPnq", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "QmadLDsehNEvYzhdBrGKQf3Y4nPXTvKTEEuvEnWVvt9m9x", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmVwvKA843CyqjT63JFMJhT5QamRjL6q4qzFayRQNYWVbb", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmVEWvqdAUQ8xmAdBKKYtyGoqd3T3hmFGikhEUzed6HRSF", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmZ88jdbhYatm9wKD4gEUkqsd5Wnjnw1PLBWbLFWDMGkGt", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmVy9nvUAXAjFazm4wpCcQJVudzmdwLipHuu4RTnS4i1kR", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmTKSv5ofDsVLnzgy4FCRLHABdknskVo3kinadeYqXBwWj", snapshots)
}, postgresRuntimePath, sqlFs)

if exitCode != 0 {
Expand Down

0 comments on commit 27081f6

Please sign in to comment.