diff --git a/order_book.go b/order_book.go index ddc67b3..9837ade 100644 --- a/order_book.go +++ b/order_book.go @@ -233,53 +233,6 @@ func (o *OrderBook) match(msg Message) { e.Size = e.Size.Sub(msg.Size) o.setEntry(e) - -// if trades.Length() == 256 { -// trades.Dequeue() -// } - -// trades.Enqueue(msg) - -// max := history.Size().Y -// length := trades.Length() -// var num int -// if length > max { -// num = max -// } else { -// num = length -// } - -// for i := 0; i < num; i++ { -// var index int - -// adj := trades.Length() - i - 1 - -// if adj < 0 { -// break -// } else { -// index = adj -// } - -// e := trades.Element(index) - -// if e != nil { -// msg := e.(Message) - -// var attrs exhibit.Attributes - -// switch msg.Side { -// case "buy": -// attrs.ForegroundColor = exhibit.FGRed -// case "sell": -// attrs.ForegroundColor = exhibit.FGGreen -// } - -// le := ListEntry{fmtHistoryEntry(msg), attrs} -// history.AddEntry(le) -// } -// } - -// history.Commit() } func (o *OrderBook) change(msg Message) { diff --git a/spectator.go b/spectator.go index bfdef9b..483f320 100644 --- a/spectator.go +++ b/spectator.go @@ -88,6 +88,10 @@ func main() { for msg := range ob.Msg { updateOrders(msg.Side) + + if msg.Type == "match" { + addTrade(msg) + } } } @@ -268,3 +272,52 @@ func updateBids(entries []Entries) { topBids.Commit() } + +func addTrade(msg Message) { + if trades.Length() == 256 { + trades.Dequeue() + } + + trades.Enqueue(msg) + + max := history.Size().Y + length := trades.Length() + var num int + if length > max { + num = max + } else { + num = length + } + + for i := 0; i < num; i++ { + var index int + + adj := trades.Length() - i - 1 + + if adj < 0 { + break + } else { + index = adj + } + + e := trades.Element(index) + + if e != nil { + msg := e.(Message) + + var attrs exhibit.Attributes + + switch msg.Side { + case "buy": + attrs.ForegroundColor = exhibit.FGRed + case "sell": + attrs.ForegroundColor = exhibit.FGGreen + } + + le := ListEntry{fmtHistoryEntry(msg), attrs} + history.AddEntry(le) + } + } + + history.Commit() +}