forked from bitfinexcom/bitfinex-api-go
/
order_book.go
66 lines (54 loc) · 1.23 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
package bitfinex
import (
"net/url"
"strconv"
"strings"
"time"
)
type OrderBookService struct {
client *Client
}
type OrderBookEntry struct {
Price string
Rate string
Amount string
Period int
Timestamp string
Frr string
}
type OrderBook struct {
Bids []OrderBookEntry
Asks []OrderBookEntry
}
func (el *OrderBookEntry) ParseTime() (*time.Time, error) {
i, err := strconv.ParseFloat(el.Timestamp, 64)
if err != nil {
return nil, err
}
t := time.Unix(int64(i), 0)
return &t, nil
}
// GET /book
func (s *OrderBookService) Get(pair string, limitBids, limitAsks int, noGroup bool) (OrderBook, error) {
pair = strings.ToUpper(pair)
params := url.Values{}
if limitBids != 0 {
params.Add("limit_bids", strconv.Itoa(limitBids))
}
if limitAsks != 0 {
params.Add("limit_asks", strconv.Itoa(limitAsks))
}
if noGroup {
params.Add("group", "0")
}
req, err := s.client.newRequest("GET", "book/"+pair, params)
if err != nil {
return OrderBook{}, err
}
var v OrderBook
_, err = s.client.do(req, &v)
if err != nil {
return OrderBook{}, err
}
return v, nil
}