Skip to content

Commit

Permalink
implement v2 rest tickers
Browse files Browse the repository at this point in the history
  • Loading branch information
igrmk committed Oct 16, 2018
1 parent f84365d commit 942171d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
2 changes: 2 additions & 0 deletions v2/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Client struct {
Platform PlatformService
Book BookService
Wallet WalletService
Ticker TickerService

Synchronous
}
Expand Down Expand Up @@ -95,6 +96,7 @@ func NewClientWithSynchronousURLNonce(sync Synchronous, url string, nonce utils.
c.Platform = PlatformService{Synchronous: c}
c.Positions = PositionService{Synchronous: c, requestFactory: c}
c.Wallet = WalletService{Synchronous: c, requestFactory: c}
c.Ticker = TickerService{Synchronous: c}
return c
}

Expand Down
91 changes: 91 additions & 0 deletions v2/rest/ticker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package rest

import (
"errors"
"fmt"
"net/url"
"strings"

bitfinex "github.com/bitfinexcom/bitfinex-api-go/v2"
)

type TickerService struct {
Synchronous
}

func (t *TickerService) All() (*bitfinex.TickerSnapshot, error) {
req := NewRequestWithMethod("tickers", "GET")
req.Params = make(url.Values)
req.Params.Add("symbols", "ALL")
raw, err := t.Request(req)

if err != nil {
return nil, err
}

tickers := make([]*bitfinex.Ticker, len(raw))
for i, ifacearr := range raw {
arr, ok := ifacearr.([]interface{})
if !ok {
return nil, fmt.Errorf("expecting array, got %T", ifacearr)
}
symbol, ok := arr[0].(string)
if !ok {
return nil, fmt.Errorf("expecting string, got %T", arr[0])
}
if len(symbol) <= 1 || (symbol[0] != 't' && symbol[0] != 'f') {
return nil, errors.New("invalid symbol")
}
if (symbol[0] == 't' && len(arr) < 11) || (symbol[0] == 'f' && len(arr) < 14) {
return nil, errors.New("invalid length of ticker")
}
sub := make([]float64, len(arr)-1)
for j, iface := range arr[1:] {
if iface == nil {
sub[j] = 0
continue
}
flt, ok := iface.(float64)
if !ok {
return nil, fmt.Errorf("expecting float64, got %T", iface)
}
sub[j] = flt
}
var entry *bitfinex.Ticker
switch symbol[0] {
case 't':
entry = &bitfinex.Ticker{
Symbol: strings.ToLower(symbol[1:]),
Bid: sub[0],
BidSize: sub[1],
Ask: sub[2],
AskSize: sub[3],
DailyChange: sub[4],
DailyChangePerc: sub[5],
LastPrice: sub[6],
Volume: sub[7],
High: sub[8],
Low: sub[9],
}
case 'f':
entry = &bitfinex.Ticker{
Symbol: strings.ToLower(symbol[1:]),
FRR: sub[0],
Bid: sub[1],
BidSize: sub[2],
BidPeriod: int64(sub[3]),
Ask: sub[4],
AskSize: sub[5],
AskPeriod: int64(sub[6]),
DailyChange: sub[7],
DailyChangePerc: sub[8],
LastPrice: sub[9],
Volume: sub[10],
High: sub[11],
Low: sub[12],
}
}
tickers[i] = entry
}
return &bitfinex.TickerSnapshot{Snapshot: tickers}, nil
}
40 changes: 40 additions & 0 deletions v2/rest/ticker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rest

import (
"bytes"
"io/ioutil"
"net/http"
"testing"
)

func TestTickerAll(t *testing.T) {
httpDo := func(_ *http.Client, req *http.Request) (*http.Response, error) {
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]]`
resp := http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(msg)),
StatusCode: 200,
}
return &resp, nil
}

ticker, err := NewClientWithHttpDo(httpDo).Ticker.All()

if err != nil {
t.Fatal(err)
}
if len(ticker.Snapshot) != 2 {
t.Fatalf("expected 2 ticker entries, but got %d", len(ticker.Snapshot))
}
if ticker.Snapshot[1].Symbol != "symbol2" {
t.Fatalf("expected symbol2 symbol, but got %s", ticker.Snapshot[1].Symbol)
}
if ticker.Snapshot[1].Low != 0.5 {
t.Fatalf("expected low equal to 0.5, but got %f", ticker.Snapshot[1].Low)
}
if ticker.Snapshot[0].BidPeriod != 4 {
t.Fatalf("expected bit period equal to 4, but got %d", ticker.Snapshot[0].BidPeriod)
}
if ticker.Snapshot[0].AskPeriod != 5 {
t.Fatalf("expected ask period equal to 5, but got %d", ticker.Snapshot[0].AskPeriod)
}
}
1 change: 1 addition & 0 deletions v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,7 @@ func NewNotificationFromRaw(raw []interface{}) (o *Notification, err error) {

type Ticker struct {
Symbol string
FRR float64
Bid float64
BidPeriod int64
BidSize float64
Expand Down

0 comments on commit 942171d

Please sign in to comment.