Skip to content

Commit 942171d

Browse files
committed
implement v2 rest tickers
1 parent f84365d commit 942171d

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

v2/rest/client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type Client struct {
3838
Platform PlatformService
3939
Book BookService
4040
Wallet WalletService
41+
Ticker TickerService
4142

4243
Synchronous
4344
}
@@ -95,6 +96,7 @@ func NewClientWithSynchronousURLNonce(sync Synchronous, url string, nonce utils.
9596
c.Platform = PlatformService{Synchronous: c}
9697
c.Positions = PositionService{Synchronous: c, requestFactory: c}
9798
c.Wallet = WalletService{Synchronous: c, requestFactory: c}
99+
c.Ticker = TickerService{Synchronous: c}
98100
return c
99101
}
100102

v2/rest/ticker.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package rest
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"net/url"
7+
"strings"
8+
9+
bitfinex "github.com/bitfinexcom/bitfinex-api-go/v2"
10+
)
11+
12+
type TickerService struct {
13+
Synchronous
14+
}
15+
16+
func (t *TickerService) All() (*bitfinex.TickerSnapshot, error) {
17+
req := NewRequestWithMethod("tickers", "GET")
18+
req.Params = make(url.Values)
19+
req.Params.Add("symbols", "ALL")
20+
raw, err := t.Request(req)
21+
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
tickers := make([]*bitfinex.Ticker, len(raw))
27+
for i, ifacearr := range raw {
28+
arr, ok := ifacearr.([]interface{})
29+
if !ok {
30+
return nil, fmt.Errorf("expecting array, got %T", ifacearr)
31+
}
32+
symbol, ok := arr[0].(string)
33+
if !ok {
34+
return nil, fmt.Errorf("expecting string, got %T", arr[0])
35+
}
36+
if len(symbol) <= 1 || (symbol[0] != 't' && symbol[0] != 'f') {
37+
return nil, errors.New("invalid symbol")
38+
}
39+
if (symbol[0] == 't' && len(arr) < 11) || (symbol[0] == 'f' && len(arr) < 14) {
40+
return nil, errors.New("invalid length of ticker")
41+
}
42+
sub := make([]float64, len(arr)-1)
43+
for j, iface := range arr[1:] {
44+
if iface == nil {
45+
sub[j] = 0
46+
continue
47+
}
48+
flt, ok := iface.(float64)
49+
if !ok {
50+
return nil, fmt.Errorf("expecting float64, got %T", iface)
51+
}
52+
sub[j] = flt
53+
}
54+
var entry *bitfinex.Ticker
55+
switch symbol[0] {
56+
case 't':
57+
entry = &bitfinex.Ticker{
58+
Symbol: strings.ToLower(symbol[1:]),
59+
Bid: sub[0],
60+
BidSize: sub[1],
61+
Ask: sub[2],
62+
AskSize: sub[3],
63+
DailyChange: sub[4],
64+
DailyChangePerc: sub[5],
65+
LastPrice: sub[6],
66+
Volume: sub[7],
67+
High: sub[8],
68+
Low: sub[9],
69+
}
70+
case 'f':
71+
entry = &bitfinex.Ticker{
72+
Symbol: strings.ToLower(symbol[1:]),
73+
FRR: sub[0],
74+
Bid: sub[1],
75+
BidSize: sub[2],
76+
BidPeriod: int64(sub[3]),
77+
Ask: sub[4],
78+
AskSize: sub[5],
79+
AskPeriod: int64(sub[6]),
80+
DailyChange: sub[7],
81+
DailyChangePerc: sub[8],
82+
LastPrice: sub[9],
83+
Volume: sub[10],
84+
High: sub[11],
85+
Low: sub[12],
86+
}
87+
}
88+
tickers[i] = entry
89+
}
90+
return &bitfinex.TickerSnapshot{Snapshot: tickers}, nil
91+
}

v2/rest/ticker_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package rest
2+
3+
import (
4+
"bytes"
5+
"io/ioutil"
6+
"net/http"
7+
"testing"
8+
)
9+
10+
func TestTickerAll(t *testing.T) {
11+
httpDo := func(_ *http.Client, req *http.Request) (*http.Response, error) {
12+
msg := `[["fSYMBOL1",3.00,0.01,0.02,4,0.03,0.04,5,0.05,0.06,0.07,0.08,0.09,0.10,null],["tSYMBOL2",0.11,0.12,0.13,0.14,0.15,0.16,0.17,0.18,0.19,0.50]]`
13+
resp := http.Response{
14+
Body: ioutil.NopCloser(bytes.NewBufferString(msg)),
15+
StatusCode: 200,
16+
}
17+
return &resp, nil
18+
}
19+
20+
ticker, err := NewClientWithHttpDo(httpDo).Ticker.All()
21+
22+
if err != nil {
23+
t.Fatal(err)
24+
}
25+
if len(ticker.Snapshot) != 2 {
26+
t.Fatalf("expected 2 ticker entries, but got %d", len(ticker.Snapshot))
27+
}
28+
if ticker.Snapshot[1].Symbol != "symbol2" {
29+
t.Fatalf("expected symbol2 symbol, but got %s", ticker.Snapshot[1].Symbol)
30+
}
31+
if ticker.Snapshot[1].Low != 0.5 {
32+
t.Fatalf("expected low equal to 0.5, but got %f", ticker.Snapshot[1].Low)
33+
}
34+
if ticker.Snapshot[0].BidPeriod != 4 {
35+
t.Fatalf("expected bit period equal to 4, but got %d", ticker.Snapshot[0].BidPeriod)
36+
}
37+
if ticker.Snapshot[0].AskPeriod != 5 {
38+
t.Fatalf("expected ask period equal to 5, but got %d", ticker.Snapshot[0].AskPeriod)
39+
}
40+
}

v2/types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,6 +1214,7 @@ func NewNotificationFromRaw(raw []interface{}) (o *Notification, err error) {
12141214

12151215
type Ticker struct {
12161216
Symbol string
1217+
FRR float64
12171218
Bid float64
12181219
BidPeriod int64
12191220
BidSize float64

0 commit comments

Comments
 (0)