Skip to content

Commit

Permalink
updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dylanlott committed May 16, 2023
1 parent cb1a23d commit 105b8e3
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 23 deletions.
34 changes: 17 additions & 17 deletions pkg/orderbook/orderbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,9 @@ func AttemptFill(
) {
for {
book.Lock()

if fillorder.Filled < fillorder.Open {
log.Printf("NOT FILLED")
} else {
log.Printf("FILLED: %+v", fillorder)
}

if fillorder.Side == "buy" {
// match to sell
log.Printf("[buy order]: %+v", fillorder)
wanted := fillorder.Open - fillorder.Filled

low := book.sell.FindMin()
if len(low.Orders) == 0 {
book.Unlock()
Expand All @@ -162,8 +155,6 @@ func AttemptFill(
bookorder := low.Orders[0] // select highest time priority by first price-valid match
available := bookorder.Open - bookorder.Filled

wanted := fillorder.Open - fillorder.Filled

match := &Match{
Buy: fillorder,
Sell: bookorder,
Expand All @@ -172,15 +163,17 @@ func AttemptFill(
switch {
case wanted > available:
greedy(book, acc, match, matches, errs)
book.Unlock()
continue
case wanted < available:
humble(book, acc, match, matches, errs)
book.Unlock()
return
default:
exact(book, acc, match, matches, errs)
book.Unlock()
return
}

} else {
// sell
log.Printf("[sell order]: %+v", fillorder)
}

book.Unlock()
Expand All @@ -195,18 +188,23 @@ func AttemptFill(
func exact(book *Book, acc accounts.AccountManager, match *Match, matchCh chan Match, errs chan error) {
available := match.Sell.Open - match.Sell.Filled
wanted := match.Buy.Open - match.Buy.Filled

if wanted == 0 {
matchCh <- *match
return
}

if available != wanted {
log.Fatalf("should not happen, this is a bug - match: %+v", match)
}

amount := float64((available * match.Sell.Price) / 100)

balances, err := acc.Tx(match.Buy.AccountID, match.Sell.AccountID, amount)
_, err := acc.Tx(match.Buy.AccountID, match.Sell.AccountID, amount)
if err != nil {
errs <- fmt.Errorf("failed to transfer: %v", err)
return
}
log.Printf("balances: %+v", balances)

match.Buy.Filled += available
match.Sell.Filled += available
Expand All @@ -216,9 +214,11 @@ func exact(book *Book, acc accounts.AccountManager, match *Match, matchCh chan M

if ok := book.buy.RemoveOrder(match.Buy); !ok {
errs <- fmt.Errorf("failed to remove over from tree %+v", match.Buy)
log.Fatalf("failed to remove order from tree %+v", match.Buy)
}
if ok := book.sell.RemoveOrder(match.Sell); !ok {
errs <- fmt.Errorf("failed to remove over from tree %+v", match.Sell)
log.Fatalf("failed to remove order from tree %+v", match.Sell)
}

matchCh <- *match
Expand Down
73 changes: 73 additions & 0 deletions pkg/orderbook/orderbook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,79 @@ func TestAttemptFill(t *testing.T) {
errs: make(chan error, 1000),
},
},
{
name: "should fill humble",
args: args{
book: &Book{
buy: &Node{
Price: 10,
Orders: []*Order{},
Left: &Node{},
Right: &Node{
Price: 11,
Orders: []*Order{
{
Price: 11,
ID: "foo",
Side: "buy",
Filled: 0,
Open: 20,
AccountID: "foo@test.com",
Kind: "market",
History: make([]Match, 0),
},
},
Left: &Node{},
Right: &Node{},
},
},
sell: &Node{
Price: 10,
Orders: []*Order{},
Left: &Node{
Price: 9,
Orders: []*Order{
{
Price: 9,
ID: "bar",
Side: "sell",
Filled: 0,
Open: 10,
AccountID: "bar@test.com",
Kind: "market",
History: make([]Match, 0),
},
{
Price: 9,
ID: "baz",
Side: "sell",
Filled: 0,
Open: 10,
AccountID: "baz@test.com",
Kind: "market",
History: make([]Match, 0),
},
{
Price: 9,
ID: "baz",
Side: "sell",
Filled: 0,
Open: 10,
AccountID: "baz@test.com",
Kind: "market",
History: make([]Match, 0),
},
},
},
Right: &Node{},
},
},
acc: acc,
fillorder: fillorder,
matches: make(chan Match, 1000),
errs: make(chan error, 1000),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions pkg/orderbook/tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func TestList(t *testing.T) {
}

func seedRootTree(root *Node) {
order1 := &Order{ID: "1", Price: 5}
order2 := &Order{ID: "2", Price: 15}
order3 := &Order{ID: "3", Price: 8}
order4 := &Order{ID: "4", Price: 12}
order5 := &Order{ID: "5", Price: 12}
order6 := &Order{ID: "6", Price: 12}
order1 := &Order{ID: "1", Price: 5, Side: "buy"}
order2 := &Order{ID: "2", Price: 15, Side: "buy"}
order3 := &Order{ID: "3", Price: 8, Side: "buy"}
order4 := &Order{ID: "4", Price: 12, Side: "buy"}
order5 := &Order{ID: "5", Price: 12, Side: "buy"}
order6 := &Order{ID: "6", Price: 12, Side: "buy"}

root.Insert(order1)
root.Insert(order2)
Expand Down

0 comments on commit 105b8e3

Please sign in to comment.