Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add asset price to trade events #9959

Merged
merged 4 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

- [9941](https://github.com/vegaprotocol/vega/issues/9941) - Add data node mapping for `WasEligible` field in referral set.
- [9940](https://github.com/vegaprotocol/vega/issues/9940) - Truncate fee stats in quantum down to the 6 decimal.
- [9952](https://github.com/vegaprotocol/vega/issues/9952) - `PnL` flickering fix.


## 0.73.0
Expand Down
1 change: 1 addition & 0 deletions core/events/trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Trade struct {
func NewTradeEvent(ctx context.Context, t types.Trade) *Trade {
p := t.IntoProto()
p.Price = t.MarketPrice.String()
p.AssetPrice = t.Price.String()
return &Trade{
Base: newBase(ctx, TradeEvent),
t: *p,
Expand Down
2 changes: 1 addition & 1 deletion datanode/entities/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (p *Position) UpdateWithTrade(trade vega.Trade, seller bool, pf num.Decimal
if seller {
size *= -1
}
marketPrice, _ := num.DecimalFromString(trade.Price) // this is market price
marketPrice, _ := num.DecimalFromString(trade.AssetPrice) // this is market price
// Scale the trade to the correct size
opened, closed := CalculateOpenClosedVolume(p.PendingOpenVolume, size)
realisedPnlDelta := marketPrice.Sub(p.PendingAverageEntryPrice).Mul(num.DecimalFromInt64(closed)).Div(pf)
Expand Down
71 changes: 39 additions & 32 deletions datanode/entities/position_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,17 +166,19 @@ func TestPnLWithPositionDecimals(t *testing.T) {

// first update with trades
trade := vega.Trade{
Id: "t1",
MarketId: market,
Price: "1000",
Size: 2,
Buyer: party,
Seller: "seller",
Id: "t1",
MarketId: market,
Price: "1000",
Size: 2,
Buyer: party,
Seller: "seller",
AssetPrice: "1000",
}
position.UpdateWithTrade(trade, false, dp)
trade.Id = "t2"
trade.Size = 3
trade.Price = "1200"
trade.AssetPrice = "1200"
position.UpdateWithTrade(trade, false, dp)
pp := position.ToProto()
assert.Equal(t, "0", pp.RealisedPnl)
Expand All @@ -200,12 +202,13 @@ func TestPnLWithPositionDecimals(t *testing.T) {

// let's make it look like this party is trading, buyer in this case
trade = vega.Trade{
Id: "t3",
MarketId: market,
Price: "1150",
Size: 1,
Buyer: party,
Seller: "seller",
Id: "t3",
MarketId: market,
Price: "1150",
Size: 1,
Buyer: party,
Seller: "seller",
AssetPrice: "1150",
}
// position.UpdateWithTrade(trade, false, num.DecimalFromFloat(1))
position.UpdateWithTrade(trade, false, dp)
Expand All @@ -227,12 +230,13 @@ func TestPnLWithPositionDecimals(t *testing.T) {
assert.EqualValues(t, 6, pp.OpenVolume)
// now close a position to see some realised PnL
trade = vega.Trade{
Id: "t4",
MarketId: market,
Price: "1250",
Size: 1,
Buyer: "buyer",
Seller: party,
Id: "t4",
MarketId: market,
Price: "1250",
Size: 1,
Buyer: "buyer",
Seller: party,
AssetPrice: "1250",
}
position.UpdateWithTrade(trade, true, dp)
pp = position.ToProto()
Expand All @@ -252,12 +256,13 @@ func TestPnLWithPositionDecimals(t *testing.T) {
assert.EqualValues(t, 5, pp.OpenVolume)
// now close the position
trade = vega.Trade{
Id: "t5",
MarketId: market,
Price: "1300",
Size: 5,
Buyer: "buyer",
Seller: party,
Id: "t5",
MarketId: market,
Price: "1300",
Size: 5,
Buyer: "buyer",
Seller: party,
AssetPrice: "1300",
}
position.UpdateWithTrade(trade, true, dp)
pp = position.ToProto()
Expand Down Expand Up @@ -307,12 +312,13 @@ func TestPnLWithTradeDecimals(t *testing.T) {
assert.Equal(t, "-200", pp.UnrealisedPnl)
// let's make it look like this party is trading, buyer in this case
trade := vega.Trade{
Id: "t1",
MarketId: market,
Price: "1150",
Size: 1,
Buyer: party,
Seller: "seller",
Id: "t1",
MarketId: market,
Price: "1150",
Size: 1,
Buyer: party,
Seller: "seller",
AssetPrice: "1150",
}
// position.UpdateWithTrade(trade, false, num.DecimalFromFloat(1))
position.UpdateWithTrade(trade, false, dp)
Expand Down Expand Up @@ -386,8 +392,9 @@ func (t tradeStub) ToVega(dp num.Decimal) vega.Trade {
size = uint64(-t.size)
}
return vega.Trade{
Size: size,
Price: t.price.String(),
Size: size,
Price: t.price.String(),
AssetPrice: t.price.String(),
}
}

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, "QmQGrPqM72evPWmpaCduiEzcndEwzsVM8PrWe7w9gqPtv4", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmQKVcMmo9D9Bx8MFHg7q2hkmDksi5zmYhanvzXmpNk7kx", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmVAP6NsCAC3qTN2VnVxyBY6A8s4tVLqppUhWL8ArkVePg", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmUXg7sLvDd53NmykEptcKUyzkb6RUvNXGR9StjjyUiE2K", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmUCbCaewhdYsEPHpC7f2uynSJjcnMXY5GEw4UKUWuJLCu", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmcGBEV4XiM98idDU9evmAArP7pZwmFe9DSP2aYsnfTg9w", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[1000].HistorySegmentID, "QmYfDKQr1vkcEs1jMTKWwuqp3MKJ8extLt5w2ceJ7FTcUS", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2000].HistorySegmentID, "QmTfyL9eTsQaNXs22R3HyLGfMtz4U7sxsTwoqGqUx6TCUr", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[2500].HistorySegmentID, "QmZRrCLaEuNwwYmr79gavFuBpjNAjoKQ49PAiAHCE5vzmS", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[3000].HistorySegmentID, "QmPAcAwfY6UTz9Q45kVLp3Q6Ug1VY6P2z63Bo4X11KvAuy", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[4000].HistorySegmentID, "QmTRugRgf6PDS97u87uQw1PfsrcSwJMwQdSb6CCWPrGHfS", snapshots)
panicIfHistorySegmentIdsNotEqual(goldenSourceHistorySegment[5000].HistorySegmentID, "QmdkNBXr3LVUd4aeSHbksKKxCoo7Nx4zRyKUpMz3SN7gUg", snapshots)
}, postgresRuntimePath, sqlFs)

if exitCode != 0 {
Expand Down
3 changes: 3 additions & 0 deletions protos/sources/vega/vega.proto
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,9 @@ message Trade {
uint64 buyer_auction_batch = 14;
// Auction batch number that the sell side order was placed in.
uint64 seller_auction_batch = 15;
// Price for the trade using asset decimals, as opposed to market decimals used
// in the price field. This is only used in trade events for position updates.
string asset_price = 16;
}

// Represents any fees paid by a party, resulting from a trade
Expand Down
Loading
Loading