-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathorder-book.go
92 lines (76 loc) · 1.6 KB
/
order-book.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package gemini
type Book struct {
Bids BookEntries `json:"bids,string"`
Asks BookEntries `json:"asks,string"`
}
type BookEntries []BookEntry
type BookEntry struct {
Price float64 `json:"price,string"`
Amount float64 `json:"amount,string"`
}
// Set updates the entries in the Book. It adds an entry if an entry for the
// given price is not found, and it updates the entry if it is found. If the
// amount is 0, it removes the entry altogether.
func (b *BookEntries) Set(price, amount float64) {
pos := b.findByPrice(price)
if pos == -1 {
if amount != 0 {
*b = append(*b, BookEntry{
Price: price,
Amount: amount,
})
}
} else {
if amount == 0 {
*b = append((*b)[:pos], (*b)[pos+1:]...)
} else {
(*b)[pos].Amount = amount
}
}
}
// Lowest returns the lowest priced entry in the list.
func (b BookEntries) Lowest() BookEntry {
var lowest float64
var index int
if len(b) == 0 {
return BookEntry{}
}
for idx, entry := range b {
if idx == 0 {
lowest = entry.Price
continue
}
if entry.Price < lowest {
lowest = entry.Price
index = idx
}
}
return b[index]
}
// Highest returns the highest priced entry in the list.
func (b BookEntries) Highest() BookEntry {
var highest float64
var index int
if len(b) == 0 {
return BookEntry{}
}
for idx, entry := range b {
if idx == 0 {
highest = entry.Price
continue
}
if entry.Price > highest {
highest = entry.Price
index = idx
}
}
return b[index]
}
func (b BookEntries) findByPrice(price float64) int {
for idx, entry := range b {
if entry.Price == price {
return idx
}
}
return -1
}