Skip to content

Commit

Permalink
chore: add AC for market depth expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
wwestgarth committed Aug 13, 2024
1 parent 131edd1 commit 975f5a2
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
13 changes: 13 additions & 0 deletions datanode/service/market_depth.go
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,19 @@ func (m *MarketDepth) GetVolumeAtPrice(market string, side types.Side, price uin
return 0
}

// GetVolumeAtPrice returns the order volume at the given price level.
func (m *MarketDepth) GetEstimatedVolumeAtPrice(market string, side types.Side, price uint64) uint64 {
md := m.marketDepths[market]
if md != nil {
pl := md.GetPriceLevel(side, num.NewUint(price))
if pl == nil {
return 0
}
return pl.TotalEstimatedAMMVolume
}
return 0
}

// GetTotalVolume returns the total volume in the order book.
func (m *MarketDepth) GetTotalVolume(market string) int64 {
var volume int64
Expand Down
84 changes: 84 additions & 0 deletions datanode/service/market_depth_amm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,74 @@ func getServiceWithConfig(t *testing.T, cfg service.MarketDepthConfig) *MDS {
}
}

func Test_0015_NP_OBES_002(t *testing.T) {
/*
0015-NP-OBES-002:
With amm_full_expansion_percentage set to 3%, amm_estimate_step_percentage set to 5% and amm_max_estimated_steps set to 2, when the mid-price is 100 then the order book expansion should return:
Volume levels at every valid tick between 97 and 103
Volume levels outside that at every 1 increment from 108 to 116 and 92 to 87
No volume levels above 116 or below 87
*/
ctx := context.Background()
mds := getServiceWithConfig(t,
service.MarketDepthConfig{
AmmFullExpansionPercentage: 3,
AmmEstimatedStepPercentage: 5,
AmmMaxEstimatedSteps: 2,
},
)
defer mds.ctrl.Finish()

marketID := vgcrypto.RandomHash()

mds.orders.EXPECT().GetLiveOrders(gomock.Any()).Return([]entities.Order{}, nil)
ensureDecimalPlaces(t, mds)
mds.pos.EXPECT().GetByMarketAndParty(gomock.Any(), gomock.Any(), gomock.Any()).Return(entities.Position{OpenVolume: 0}, nil)

// mid-price is 100
mds.marketData.EXPECT().GetMarketDataByID(gomock.Any(), gomock.Any()).Times(1).Return(entities.MarketData{MidPrice: num.DecimalFromInt64(100)}, nil)

// data node is starting from network history, initialise market-depth based on whats aleady there
pool := getAMMDefinitionMid100(t, marketID)
mds.amm.EXPECT().ListActive(gomock.Any()).Return([]entities.AMMPool{pool}, nil).Times(1)
mds.service.Initialise(ctx)

// buys estimates at 87, 92, accurate ones at 97, 98, 99
prices := map[uint64]bool{
87: true,
92: true,
97: false,
98: false,
99: false,
}
assert.Equal(t, 5, mds.service.GetBuyPriceLevels(marketID))
for p, estimated := range prices {
volume := mds.service.GetVolumeAtPrice(marketID, types.SideBuy, p)
if estimated {
volume = mds.service.GetEstimatedVolumeAtPrice(marketID, types.SideBuy, p)
}
assert.NotEqual(t, uint64(0), volume)
}

// sell estimates at 109, 104, accurate ones at 103, 102, 101
prices = map[uint64]bool{
109: true,
104: true,
103: false,
102: false,
101: false,
}
assert.Equal(t, 5, mds.service.GetSellPriceLevels(marketID))
for p, estimated := range prices {
volume := mds.service.GetVolumeAtPrice(marketID, types.SideSell, p)
if estimated {
volume = mds.service.GetEstimatedVolumeAtPrice(marketID, types.SideSell, p)
}
assert.NotEqual(t, uint64(0), volume)
}
}

func TestAMMMarketDepth(t *testing.T) {
ctx := context.Background()
mds := getService(t)
Expand Down Expand Up @@ -401,6 +469,22 @@ func getAMMDefinition(t *testing.T, marketID string) entities.AMMPool {
}
}

func getAMMDefinitionMid100(t *testing.T, marketID string) entities.AMMPool {
t.Helper()
return entities.AMMPool{
PartyID: entities.PartyID(vgcrypto.RandomHash()),
AmmPartyID: entities.PartyID(vgcrypto.RandomHash()),
MarketID: entities.MarketID(marketID),
ParametersLowerBound: ptr.From(num.DecimalFromInt64(50)),
LowerVirtualLiquidity: num.DecimalFromFloat(109933.47060272754448304259594317590451),
LowerTheoreticalPosition: num.DecimalFromFloat(4553.5934482393695541),
ParametersBase: num.DecimalFromInt64(100),
ParametersUpperBound: ptr.From(num.DecimalFromInt64(150)),
UpperVirtualLiquidity: num.DecimalFromFloat(174241.4190625882586702427011885011744),
UpperTheoreticalPosition: num.DecimalFromFloat(3197.389614198983918),
}
}

func ensureAMMs(t *testing.T, mds *MDS, marketID string) entities.AMMPool {
t.Helper()

Expand Down

0 comments on commit 975f5a2

Please sign in to comment.