Add trade history back to the UI using the refactored order book

This commit is contained in:
Kevin Cotugno 2018-01-01 18:30:27 -08:00
parent 902590a9b9
commit 7d07aa8e8f
2 changed files with 53 additions and 47 deletions

View File

@ -233,53 +233,6 @@ func (o *OrderBook) match(msg Message) {
e.Size = e.Size.Sub(msg.Size) e.Size = e.Size.Sub(msg.Size)
o.setEntry(e) 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) { func (o *OrderBook) change(msg Message) {

View File

@ -88,6 +88,10 @@ func main() {
for msg := range ob.Msg { for msg := range ob.Msg {
updateOrders(msg.Side) updateOrders(msg.Side)
if msg.Type == "match" {
addTrade(msg)
}
} }
} }
@ -268,3 +272,52 @@ func updateBids(entries []Entries) {
topBids.Commit() 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()
}