Skip to content

Commit

Permalink
add tests for MatchOrders
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed May 16, 2023
1 parent 105b8e3 commit 7030aab
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
20 changes: 8 additions & 12 deletions pkg/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,9 @@ func greedy(
matchCh <- *match
}

// TODO: write another set of tests using MatchOrders instead and then benchmark them.
//
// MatchOrders is an alternative approach to order matching that
// works by aligning two opposing sorted slices of Orders.
func MatchOrders(buyOrders []Order, sellOrders []Order) []Order {
// Sort the orders by price
func MatchOrders(buyOrders []Order, sellOrders []Order) []Match {
sort.Slice(buyOrders, func(i, j int) bool {
return buyOrders[i].Price > buyOrders[j].Price
})
Expand All @@ -307,19 +304,18 @@ func MatchOrders(buyOrders []Order, sellOrders []Order) []Order {
// Initialize the index variables
buyIndex := 0
sellIndex := 0
var filledOrders []Order
var matches []Match

// Loop until there are no more Sell orders left
for sellIndex < len(sellOrders) {
// Check if the current Buy order matches the current Sell order
if buyOrders[buyIndex].Price >= sellOrders[sellIndex].Price {
// Fill the Buy order with the Sell order
filledOrder := Order{
// Buyer: buyOrders[buyIndex].Buyer,
// Seller: sellOrders[sellIndex].Seller,
Price: sellOrders[sellIndex].Price,
// Create a Match of the Buy and Sell side
m := Match{
Buy: &buyOrders[buyIndex],
Sell: &sellOrders[sellIndex],
}
filledOrders = append(filledOrders, filledOrder)
matches = append(matches, m)
// Increment the Sell order index
sellIndex++
} else {
Expand All @@ -333,5 +329,5 @@ func MatchOrders(buyOrders []Order, sellOrders []Order) []Order {
}

// Return the list of filled orders
return filledOrders
return matches
}
38 changes: 38 additions & 0 deletions pkg/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,41 @@ func TestAttemptFill(t *testing.T) {
})
}
}

func TestMatchOrders(t *testing.T) {
buy, sell := newTestOrders(t, 1000)
got := MatchOrders(buy, sell)
for _, match := range got {
t.Logf("\nmatch: [buy] %+v\n [sell] %+v\n", match.Buy, match.Sell)
}
}

func newTestOrders(t *testing.T, count int) (buyOrders []Order, sellOrders []Order) {
rand.Seed(time.Now().UnixNano())

min := 100
max := 10000

for i := 0; i < count; i++ {
o := Order{
ID: fmt.Sprintf("%d", i),
AccountID: "", // TODO: add a random account owner
Kind: "market",
Price: uint64(rand.Intn(max-min) + min),
Open: uint64(rand.Intn(max-min) + min),
Filled: 0,
History: []Match{}, // history should be nil
}

// half buy, half sell orders
if i%2 == 0 {
o.Side = "buy"
buyOrders = append(buyOrders, o)
} else {
o.Side = "sell"
sellOrders = append(sellOrders, o)
}
}

return
}

0 comments on commit 7030aab

Please sign in to comment.