Skip to content

Commit

Permalink
add order summary and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
buck54321 committed Oct 20, 2024
1 parent e9b6f8a commit 7c53b02
Show file tree
Hide file tree
Showing 10 changed files with 520 additions and 351 deletions.
342 changes: 245 additions & 97 deletions client/mm/exchange_adaptor.go

Large diffs are not rendered by default.

326 changes: 163 additions & 163 deletions client/mm/exchange_adaptor_test.go

Large diffs are not rendered by default.

38 changes: 20 additions & 18 deletions client/mm/mm_arb_market_maker.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,18 @@ func (a *arbMarketMaker) dexPlacementRate(cexRate uint64, sell bool) (uint64, er
return dexPlacementRate(cexRate, sell, a.cfg().Profit, a.market, feesInQuoteUnits, a.log)
}

func (a *arbMarketMaker) ordersToPlace() (buys, sells []*multiTradePlacement) {
orders := func(cfgPlacements []*ArbMarketMakingPlacement, sellOnDEX bool) []*multiTradePlacement {
newPlacements := make([]*multiTradePlacement, 0, len(cfgPlacements))
func (a *arbMarketMaker) ordersToPlace() (buys, sells []*TradePlacement) {
orders := func(cfgPlacements []*ArbMarketMakingPlacement, sellOnDEX bool) []*TradePlacement {
newPlacements := make([]*TradePlacement, 0, len(cfgPlacements))
var cumulativeCEXDepth uint64
for i, cfgPlacement := range cfgPlacements {
cumulativeCEXDepth += uint64(float64(cfgPlacement.Lots*a.lotSize) * cfgPlacement.Multiplier)
_, extrema, filled, err := a.CEX.VWAP(a.baseID, a.quoteID, sellOnDEX, cumulativeCEXDepth)
if err != nil {
a.log.Errorf("Error calculating vwap: %v", err)
newPlacements = append(newPlacements, &multiTradePlacement{
rate: 0,
lots: 0,
newPlacements = append(newPlacements, &TradePlacement{
Rate: 0,
Lots: 0,
})
continue
}
Expand All @@ -259,27 +259,27 @@ func (a *arbMarketMaker) ordersToPlace() (buys, sells []*multiTradePlacement) {

if !filled {
a.log.Infof("CEX %s side has < %s on the orderbook.", sellStr(!sellOnDEX), a.fmtBase(cumulativeCEXDepth))
newPlacements = append(newPlacements, &multiTradePlacement{
rate: 0,
lots: 0,
newPlacements = append(newPlacements, &TradePlacement{
Rate: 0,
Lots: 0,
})
continue
}

placementRate, err := a.dexPlacementRate(extrema, sellOnDEX)
if err != nil {
a.log.Errorf("Error calculating dex placement rate: %v", err)
newPlacements = append(newPlacements, &multiTradePlacement{
rate: 0,
lots: 0,
newPlacements = append(newPlacements, &TradePlacement{
Rate: 0,
Lots: 0,
})
continue
}

newPlacements = append(newPlacements, &multiTradePlacement{
rate: placementRate,
lots: cfgPlacement.Lots,
counterTradeRate: extrema,
newPlacements = append(newPlacements, &TradePlacement{
Rate: placementRate,
Lots: cfgPlacement.Lots,
CounterTradeRate: extrema,
})
}

Expand Down Expand Up @@ -354,8 +354,8 @@ func (a *arbMarketMaker) rebalance(epoch uint64) {
}

buys, sells := a.ordersToPlace()
buyInfos := a.multiTrade(buys, false, a.cfg().DriftTolerance, currEpoch)
sellInfos := a.multiTrade(sells, true, a.cfg().DriftTolerance, currEpoch)
buyInfos, buyReport := a.multiTrade(buys, false, a.cfg().DriftTolerance, currEpoch)
sellInfos, sellReport := a.multiTrade(sells, true, a.cfg().DriftTolerance, currEpoch)
a.matchesMtx.Lock()
for oid, info := range buyInfos {
a.pendingOrders[oid] = info.counterTradeRate
Expand All @@ -368,6 +368,8 @@ func (a *arbMarketMaker) rebalance(epoch uint64) {
a.cancelExpiredCEXTrades()

a.registerFeeGap()

a.Broadcast(newEpochNote(a.mwh, buyReport, sellReport))
}

func (a *arbMarketMaker) tryTransfers(currEpoch uint64) (actionTaken bool, err error) {
Expand Down
10 changes: 5 additions & 5 deletions client/mm/mm_arb_market_maker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,25 @@ func TestArbMMRebalance(t *testing.T) {
cex: newTBotCEXAdaptor(),
pendingOrders: make(map[order.OrderID]uint64),
}
a.buyFees = &orderFees{
a.buyFees = &OrderFees{
LotFeeRange: &LotFeeRange{
Max: &LotFees{
Redeem: buyRedeemFees,
Swap: buySwapFees,
},
Estimated: &LotFees{},
},
bookingFeesPerLot: buySwapFees,
BookingFeesPerLot: buySwapFees,
}
a.sellFees = &orderFees{
a.sellFees = &OrderFees{
LotFeeRange: &LotFeeRange{
Max: &LotFees{
Redeem: sellRedeemFees,
Swap: sellSwapFees,
},
Estimated: &LotFees{},
},
bookingFeesPerLot: sellSwapFees,
BookingFeesPerLot: sellSwapFees,
}

var buyLots, sellLots, minDexBase, minCexBase /* totalBase, */, minDexQuote, minCexQuote /*, totalQuote */ uint64
Expand Down Expand Up @@ -103,7 +103,7 @@ func TestArbMMRebalance(t *testing.T) {
}
minDexBase = sellLots * (lotSize + sellSwapFees)
minCexBase = buyLots * lotSize
minDexQuote = calc.BaseToQuote(buyRate, buyLots*lotSize) + a.buyFees.bookingFeesPerLot*buyLots
minDexQuote = calc.BaseToQuote(buyRate, buyLots*lotSize) + a.buyFees.BookingFeesPerLot*buyLots
minCexQuote = calc.BaseToQuote(sellRate, sellLots*lotSize)
}

Expand Down
12 changes: 6 additions & 6 deletions client/mm/mm_basic.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ func (m *basicMarketMaker) orderPrice(basisPrice, feeAdj uint64, sell bool, gapF
return basisPrice - adj
}

func (m *basicMarketMaker) ordersToPlace() (buyOrders, sellOrders []*multiTradePlacement, err error) {
func (m *basicMarketMaker) ordersToPlace() (buyOrders, sellOrders []*TradePlacement, err error) {
basisPrice := m.calculator.basisPrice()
if basisPrice == 0 {
return nil, nil, fmt.Errorf("no basis price available")
Expand All @@ -340,8 +340,8 @@ func (m *basicMarketMaker) ordersToPlace() (buyOrders, sellOrders []*multiTradeP
m.name, m.fmtRate(basisPrice), m.fmtRate(feeAdj))
}

orders := func(orderPlacements []*OrderPlacement, sell bool) []*multiTradePlacement {
placements := make([]*multiTradePlacement, 0, len(orderPlacements))
orders := func(orderPlacements []*OrderPlacement, sell bool) []*TradePlacement {
placements := make([]*TradePlacement, 0, len(orderPlacements))
for i, p := range orderPlacements {
rate := m.orderPrice(basisPrice, feeAdj, sell, p.GapFactor)

Expand All @@ -354,9 +354,9 @@ func (m *basicMarketMaker) ordersToPlace() (buyOrders, sellOrders []*multiTradeP
if rate == 0 {
lots = 0
}
placements = append(placements, &multiTradePlacement{
rate: rate,
lots: lots,
placements = append(placements, &TradePlacement{
Rate: rate,
Lots: lots,
})
}
return placements
Expand Down
104 changes: 52 additions & 52 deletions client/mm/mm_basic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ func TestBasicMMRebalance(t *testing.T) {
cfgBuyPlacements []*OrderPlacement
cfgSellPlacements []*OrderPlacement

expBuyPlacements []*multiTradePlacement
expSellPlacements []*multiTradePlacement
expBuyPlacements []*TradePlacement
expSellPlacements []*TradePlacement
}
tests := []*test{
{
Expand All @@ -209,15 +209,15 @@ func TestBasicMMRebalance(t *testing.T) {
{Lots: 2, GapFactor: 2},
{Lots: 1, GapFactor: 3},
},
expBuyPlacements: []*multiTradePlacement{
{lots: 1, rate: steppedRate(basisPrice-3*halfSpread, rateStep)},
{lots: 2, rate: steppedRate(basisPrice-2*halfSpread, rateStep)},
{lots: 3, rate: steppedRate(basisPrice-1*halfSpread, rateStep)},
expBuyPlacements: []*TradePlacement{
{Lots: 1, Rate: steppedRate(basisPrice-3*halfSpread, rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice-2*halfSpread, rateStep)},
{Lots: 3, Rate: steppedRate(basisPrice-1*halfSpread, rateStep)},
},
expSellPlacements: []*multiTradePlacement{
{lots: 3, rate: steppedRate(basisPrice+1*halfSpread, rateStep)},
{lots: 2, rate: steppedRate(basisPrice+2*halfSpread, rateStep)},
{lots: 1, rate: steppedRate(basisPrice+3*halfSpread, rateStep)},
expSellPlacements: []*TradePlacement{
{Lots: 3, Rate: steppedRate(basisPrice+1*halfSpread, rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice+2*halfSpread, rateStep)},
{Lots: 1, Rate: steppedRate(basisPrice+3*halfSpread, rateStep)},
},
},
{
Expand All @@ -233,15 +233,15 @@ func TestBasicMMRebalance(t *testing.T) {
{Lots: 2, GapFactor: 0.1},
{Lots: 1, GapFactor: 0.05},
},
expBuyPlacements: []*multiTradePlacement{
{lots: 1, rate: steppedRate(basisPrice-uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
{lots: 2, rate: steppedRate(basisPrice-uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{lots: 3, rate: steppedRate(basisPrice-uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
expBuyPlacements: []*TradePlacement{
{Lots: 1, Rate: steppedRate(basisPrice-uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice-uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{Lots: 3, Rate: steppedRate(basisPrice-uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
},
expSellPlacements: []*multiTradePlacement{
{lots: 3, rate: steppedRate(basisPrice+uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
{lots: 2, rate: steppedRate(basisPrice+uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{lots: 1, rate: steppedRate(basisPrice+uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
expSellPlacements: []*TradePlacement{
{Lots: 3, Rate: steppedRate(basisPrice+uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice+uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{Lots: 1, Rate: steppedRate(basisPrice+uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
},
},
{
Expand All @@ -257,15 +257,15 @@ func TestBasicMMRebalance(t *testing.T) {
{Lots: 2, GapFactor: 0.1},
{Lots: 1, GapFactor: 0.05},
},
expBuyPlacements: []*multiTradePlacement{
{lots: 1, rate: steppedRate(basisPrice-halfSpread-uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
{lots: 2, rate: steppedRate(basisPrice-halfSpread-uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{lots: 3, rate: steppedRate(basisPrice-halfSpread-uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
expBuyPlacements: []*TradePlacement{
{Lots: 1, Rate: steppedRate(basisPrice-halfSpread-uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice-halfSpread-uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{Lots: 3, Rate: steppedRate(basisPrice-halfSpread-uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
},
expSellPlacements: []*multiTradePlacement{
{lots: 3, rate: steppedRate(basisPrice+halfSpread+uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
{lots: 2, rate: steppedRate(basisPrice+halfSpread+uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{lots: 1, rate: steppedRate(basisPrice+halfSpread+uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
expSellPlacements: []*TradePlacement{
{Lots: 3, Rate: steppedRate(basisPrice+halfSpread+uint64(math.Round((float64(basisPrice)*0.15))), rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice+halfSpread+uint64(math.Round((float64(basisPrice)*0.1))), rateStep)},
{Lots: 1, Rate: steppedRate(basisPrice+halfSpread+uint64(math.Round((float64(basisPrice)*0.05))), rateStep)},
},
},
{
Expand All @@ -281,14 +281,14 @@ func TestBasicMMRebalance(t *testing.T) {
{Lots: 2, GapFactor: .03},
{Lots: 1, GapFactor: .01},
},
expBuyPlacements: []*multiTradePlacement{
{lots: 1, rate: steppedRate(basisPrice-1e6, rateStep)},
{lots: 2, rate: steppedRate(basisPrice-3e6, rateStep)},
expBuyPlacements: []*TradePlacement{
{Lots: 1, Rate: steppedRate(basisPrice-1e6, rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice-3e6, rateStep)},
},
expSellPlacements: []*multiTradePlacement{
{lots: 3, rate: steppedRate(basisPrice+6e6, rateStep)},
{lots: 2, rate: steppedRate(basisPrice+3e6, rateStep)},
{lots: 1, rate: steppedRate(basisPrice+1e6, rateStep)},
expSellPlacements: []*TradePlacement{
{Lots: 3, Rate: steppedRate(basisPrice+6e6, rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice+3e6, rateStep)},
{Lots: 1, Rate: steppedRate(basisPrice+1e6, rateStep)},
},
},
{
Expand All @@ -304,14 +304,14 @@ func TestBasicMMRebalance(t *testing.T) {
{Lots: 2, GapFactor: .03},
{Lots: 1, GapFactor: .01},
},
expBuyPlacements: []*multiTradePlacement{
{lots: 1, rate: steppedRate(basisPrice-halfSpread-1e6, rateStep)},
{lots: 2, rate: steppedRate(basisPrice-halfSpread-3e6, rateStep)},
expBuyPlacements: []*TradePlacement{
{Lots: 1, Rate: steppedRate(basisPrice-halfSpread-1e6, rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice-halfSpread-3e6, rateStep)},
},
expSellPlacements: []*multiTradePlacement{
{lots: 3, rate: steppedRate(basisPrice+halfSpread+6e6, rateStep)},
{lots: 2, rate: steppedRate(basisPrice+halfSpread+3e6, rateStep)},
{lots: 1, rate: steppedRate(basisPrice+halfSpread+1e6, rateStep)},
expSellPlacements: []*TradePlacement{
{Lots: 3, Rate: steppedRate(basisPrice+halfSpread+6e6, rateStep)},
{Lots: 2, Rate: steppedRate(basisPrice+halfSpread+3e6, rateStep)},
{Lots: 1, Rate: steppedRate(basisPrice+halfSpread+1e6, rateStep)},
},
},
}
Expand All @@ -336,25 +336,25 @@ func TestBasicMMRebalance(t *testing.T) {
mm.fiatRates.Store(map[uint32]float64{baseID: 1, quoteID: 1})
const sellSwapFees, sellRedeemFees = 3e6, 1e6
const buySwapFees, buyRedeemFees = 2e5, 1e5
mm.buyFees = &orderFees{
mm.buyFees = &OrderFees{
LotFeeRange: &LotFeeRange{
Max: &LotFees{
Redeem: buyRedeemFees,
Swap: buySwapFees,
},
Estimated: &LotFees{},
},
bookingFeesPerLot: buySwapFees,
BookingFeesPerLot: buySwapFees,
}
mm.sellFees = &orderFees{
mm.sellFees = &OrderFees{
LotFeeRange: &LotFeeRange{
Max: &LotFees{
Redeem: sellRedeemFees,
Swap: sellSwapFees,
},
Estimated: &LotFees{},
},
bookingFeesPerLot: sellSwapFees,
BookingFeesPerLot: sellSwapFees,
}
mm.baseDexBalances[baseID] = lotSize * 50
mm.baseCexBalances[baseID] = lotSize * 50
Expand Down Expand Up @@ -382,11 +382,11 @@ func TestBasicMMRebalance(t *testing.T) {
buyRateLots[p.Rate] = p.Qty / lotSize
}
for _, expBuy := range tt.expBuyPlacements {
if lots, found := buyRateLots[expBuy.rate]; !found {
t.Fatalf("buy rate %d not found", expBuy.rate)
if lots, found := buyRateLots[expBuy.Rate]; !found {
t.Fatalf("buy rate %d not found", expBuy.Rate)
} else {
if expBuy.lots != lots {
t.Fatalf("wrong lots %d for buy at rate %d", lots, expBuy.rate)
if expBuy.Lots != lots {
t.Fatalf("wrong lots %d for buy at rate %d", lots, expBuy.Rate)
}
}
}
Expand All @@ -395,11 +395,11 @@ func TestBasicMMRebalance(t *testing.T) {
sellRateLots[p.Rate] = p.Qty / lotSize
}
for _, expSell := range tt.expSellPlacements {
if lots, found := sellRateLots[expSell.rate]; !found {
t.Fatalf("sell rate %d not found", expSell.rate)
if lots, found := sellRateLots[expSell.Rate]; !found {
t.Fatalf("sell rate %d not found", expSell.Rate)
} else {
if expSell.lots != lots {
t.Fatalf("wrong lots %d for sell at rate %d", lots, expSell.rate)
if expSell.Lots != lots {
t.Fatalf("wrong lots %d for sell at rate %d", lots, expSell.Rate)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions client/mm/mm_simple_arb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ func TestArbRebalance(t *testing.T) {
a.CEX = tcex
tcex.asksVWAP[lotSize] = vwapResult{avg: buyRate}
tcex.bidsVWAP[lotSize] = vwapResult{avg: sellRate}
a.buyFees = &orderFees{
a.buyFees = &OrderFees{
LotFeeRange: &LotFeeRange{
Max: &LotFees{
Redeem: buyRedeemFees,
Expand All @@ -526,9 +526,9 @@ func TestArbRebalance(t *testing.T) {
Redeem: buyRedeemFees,
},
},
bookingFeesPerLot: buySwapFees,
BookingFeesPerLot: buySwapFees,
}
a.sellFees = &orderFees{
a.sellFees = &OrderFees{
LotFeeRange: &LotFeeRange{
Max: &LotFees{
Redeem: sellRedeemFees,
Expand All @@ -538,7 +538,7 @@ func TestArbRebalance(t *testing.T) {
Redeem: sellRedeemFees,
},
},
bookingFeesPerLot: sellSwapFees,
BookingFeesPerLot: sellSwapFees,
}
// arbEngine.setBotLoop(arbEngine.botLoop)
a.cfgV.Store(&SimpleArbConfig{
Expand Down
Loading

0 comments on commit 7c53b02

Please sign in to comment.