forked from bitfinexcom/bitfinex-api-go
/
orders.go
94 lines (79 loc) · 2.13 KB
/
orders.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
93
94
package rest
import (
"fmt"
"path"
"github.com/FTIVLTD/bitfinex-api-go/v2"
)
// OrderService manages data flow for the Order API endpoint
type OrderService struct {
requestFactory
Synchronous
}
// All returns all orders for the authenticated account.
func (s *OrderService) All(symbol string) (*bitfinex.OrderSnapshot, error) {
req, err := s.requestFactory.NewAuthenticatedRequest(path.Join("orders", symbol))
if err != nil {
return nil, err
}
raw, err := s.Request(req)
if err != nil {
return nil, err
}
os, err := bitfinex.NewOrderSnapshotFromRaw(raw)
if err != nil {
return nil, err
}
return os, nil
}
// Status retrieves the given order from the API. This is just a wrapper around
// the All() method, since the API does not provide lookup for a single Order.
func (s *OrderService) Status(orderID int64) (o *bitfinex.Order, err error) {
os, err := s.All("")
if err != nil {
return o, err
}
if len(os.Snapshot) == 0 {
return o, bitfinex.ErrNotFound
}
for _, e := range os.Snapshot {
if e.ID == orderID {
return e, nil
}
}
return o, bitfinex.ErrNotFound
}
// All returns all orders for the authenticated account.
func (s *OrderService) History(symbol string) (*bitfinex.OrderSnapshot, error) {
if symbol == "" {
return nil, fmt.Errorf("symbol cannot be empty")
}
req, err := s.requestFactory.NewAuthenticatedRequest(path.Join("orders", symbol, "hist"))
if err != nil {
return nil, err
}
raw, err := s.Request(req)
if err != nil {
return nil, err
}
os, err := bitfinex.NewOrderSnapshotFromRaw(raw)
if err != nil {
return nil, err
}
return os, nil
}
// OrderTrades returns a set of executed trades related to an order.
func (s *OrderService) OrderTrades(symbol string, orderID int64) (*bitfinex.TradeExecutionUpdateSnapshot, error) {
if symbol == "" {
return nil, fmt.Errorf("symbol cannot be empty")
}
key := fmt.Sprintf("%s:%d", symbol, orderID)
req, err := s.requestFactory.NewAuthenticatedRequest(path.Join("order", key, "trades"))
if err != nil {
return nil, err
}
raw, err := s.Request(req)
if err != nil {
return nil, err
}
return bitfinex.NewTradeExecutionUpdateSnapshotFromRaw(raw)
}