forked from coinrust/crex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorderbook.go
55 lines (47 loc) · 829 Bytes
/
orderbook.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
package crex
import (
"time"
)
type Item struct {
Price float64
Amount float64
}
type OrderBook struct {
Symbol string
Time time.Time
Asks []Item
Bids []Item
}
// Ask 卖一
func (o *OrderBook) Ask() (result Item) {
if len(o.Asks) > 0 {
result = o.Asks[0]
}
return
}
// Bid 买一
func (o *OrderBook) Bid() (result Item) {
if len(o.Bids) > 0 {
result = o.Bids[0]
}
return
}
// AskPrice 卖一价
func (o *OrderBook) AskPrice() (result float64) {
if len(o.Asks) > 0 {
result = o.Asks[0].Price
}
return
}
// BidPrice 买一价
func (o *OrderBook) BidPrice() (result float64) {
if len(o.Bids) > 0 {
result = o.Bids[0].Price
}
return
}
// Price returns the middle of Bid and Ask.
func (o *OrderBook) Price() float64 {
latest := (o.Bid().Price + o.Ask().Price) / float64(2)
return latest
}