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

Fixed auction uncrossing #2281

Merged
merged 1 commit into from
Sep 22, 2020
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
12 changes: 11 additions & 1 deletion execution/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,13 +513,23 @@ func (m *Market) LeaveAuction(ctx context.Context) {

m.tradeMode = types.MarketState_MARKET_STATE_AUCTION
}
m.matching.LeaveAuction() // TODO (WG 03/09/20): Push out trades, calling this only to be able the test the triggers for now.

// Change market type to continuous trading
uncrossedOrders, ordersToCancel, err := m.matching.LeaveAuction()
if err != nil {
m.log.Error("Error leaving auction", logging.Error(err))
}

// Process each confirmation
for _, uncrossedOrder := range uncrossedOrders {
m.handleConfirmation(ctx, uncrossedOrder.Order, uncrossedOrder)

if uncrossedOrder.Order.Remaining == 0 {
uncrossedOrder.Order.Status = types.Order_STATUS_FILLED
}
m.broker.Send(events.NewOrderEvent(ctx, uncrossedOrder.Order))
}

// Process each order we have to cancel
for _, order := range ordersToCancel {
_, err := m.CancelOrder(ctx, order.PartyID, order.Id)
Expand Down
33 changes: 18 additions & 15 deletions matching/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,6 @@ type OrderBook struct {
batchID uint64
}

// UncrossedOrder contains the details relating to a single order that
// has been uncrossed at the end of an auction
type UncrossedOrder struct {
Order *types.Order
AffectedOrders []*types.Order
Trades []*types.Trade
}

// CumulativeVolumeLevel represents the cumulative volume at a price level for both bid and ask
type CumulativeVolumeLevel struct {
price uint64
Expand Down Expand Up @@ -200,7 +192,7 @@ func (b *OrderBook) EnterAuction() ([]*types.Order, error) {
}

// LeaveAuction Moves the order book back into continuous trading state
func (b *OrderBook) LeaveAuction() ([]*UncrossedOrder, []*types.Order, error) {
func (b *OrderBook) LeaveAuction() ([]*types.OrderConfirmation, []*types.Order, error) {
// Update batchID
b.batchID++

Expand Down Expand Up @@ -326,7 +318,7 @@ func (b *OrderBook) buildCumulativePriceLevels(maxPrice, minPrice uint64) map[ui
}

// Uncrosses the book to generate the maximum volume set of trades
func (b *OrderBook) uncrossBook() ([]*UncrossedOrder, error) {
func (b *OrderBook) uncrossBook() ([]*types.OrderConfirmation, error) {
// Get the uncrossing price and which side has the most volume at that price
price, volume, uncrossSide := b.GetIndicativePriceAndVolume()

Expand All @@ -335,8 +327,8 @@ func (b *OrderBook) uncrossBook() ([]*UncrossedOrder, error) {
return nil, nil
}

var uncrossedOrder *UncrossedOrder
var allOrders []*UncrossedOrder
var uncrossedOrder *types.OrderConfirmation
var allOrders []*types.OrderConfirmation

// Remove all the orders from that side of the book upto the given volume
if uncrossSide == types.Side_SIDE_BUY {
Expand All @@ -357,7 +349,13 @@ func (b *OrderBook) uncrossBook() ([]*UncrossedOrder, error) {
for index := 0; index < len(trades); index++ {
trades[index].Price = price
}
uncrossedOrder = &UncrossedOrder{Order: order, AffectedOrders: affectedOrders, Trades: trades}
// If the affected order is fully filled set the status
for _, affectedOrder := range affectedOrders {
if affectedOrder.Remaining == 0 {
affectedOrder.Status = types.Order_STATUS_FILLED
}
}
uncrossedOrder = &types.OrderConfirmation{Order: order, PassiveOrdersAffected: affectedOrders, Trades: trades}
allOrders = append(allOrders, uncrossedOrder)
}
} else {
Expand All @@ -378,11 +376,16 @@ func (b *OrderBook) uncrossBook() ([]*UncrossedOrder, error) {
for index := 0; index < len(trades); index++ {
trades[index].Price = price
}
uncrossedOrder = &UncrossedOrder{Order: order, AffectedOrders: affectedOrders, Trades: trades}
// If the affected order is fully filled set the status
for _, affectedOrder := range affectedOrders {
if affectedOrder.Remaining == 0 {
affectedOrder.Status = types.Order_STATUS_FILLED
}
}
uncrossedOrder = &types.OrderConfirmation{Order: order, PassiveOrdersAffected: affectedOrders, Trades: trades}
allOrders = append(allOrders, uncrossedOrder)
}
}

return allOrders, nil
}

Expand Down