Skip to content

Commit

Permalink
Merge pull request #232 from Daniel1984/feature/rest-ticker-hist
Browse files Browse the repository at this point in the history
Feature/rest ticker hist
  • Loading branch information
robertkowalski authored May 3, 2021
2 parents f31c609 + 2bc57ad commit b4f52b0
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
3.0.4
- Adds new rest v2 functions
- tickers/hist

3.0.3
- Features
- new websocket manager. Please refer to `/examples/ws` for usage examples.
Expand Down
83 changes: 83 additions & 0 deletions examples/v2/rest-ticker-history/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package main

import (
"log"
"time"

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

func main() {
c := rest.NewClient()

getSinglePair(c)
getSinglePairWithLimit(c)
getMultiPair(c)
getForRange(c)
}

func getSinglePair(c *rest.Client) {
args := rest.GetTickerHistPayload{
Symbols: []string{"tLTCUSD"},
}

t, err := c.TickersHistory.Get(args)

if err != nil {
log.Fatalf("get single pair: %s", err)
}

spew.Dump("get single pair:> ", t)
}

func getSinglePairWithLimit(c *rest.Client) {
args := rest.GetTickerHistPayload{
Symbols: []string{"tLTCUSD"},
Limit: 10,
}

t, err := c.TickersHistory.Get(args)

if err != nil {
log.Fatalf("get single pair with limit: %s", err)
}

spew.Dump("get single pair with limit:> ", t)
}

func getMultiPair(c *rest.Client) {
args := rest.GetTickerHistPayload{
Symbols: []string{"tLTCUSD", "tBTCUSD"},
}

t, err := c.TickersHistory.Get(args)

if err != nil {
log.Fatalf("get multi pair: %s", err)
}

spew.Dump("get multi pair:> ", t)
}

func getForRange(c *rest.Client) {
now := time.Now()
yesterday := now.Add(time.Duration(-24) * time.Hour)
end := now.UnixNano() / 1000000
start := yesterday.UnixNano() / 1000000

args := rest.GetTickerHistPayload{
Symbols: []string{"tBTCUSD"},
Limit: 5,
Start: start,
End: end,
}

t, err := c.TickersHistory.Get(args)

if err != nil {
log.Fatalf("get for range: %s", err)
}

spew.Dump("get for range:> ", t)
}
14 changes: 7 additions & 7 deletions examples/v2/rest-ticker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ import (
func main() {
c := rest.NewClient()

get(c)
getSingle(c)
getMulti(c)
all(c)
}

func get(c *rest.Client) {
func getSingle(c *rest.Client) {
t, err := c.Tickers.Get("tBTCUSD")

if err != nil {
log.Fatalf("get: %s", err)
log.Fatalf("getSingle: %s", err)
}

spew.Dump(t)
spew.Dump("getSingle:> ", t)
}

func getMulti(c *rest.Client) {
symbols := []string{"tBTCUSD", "tEOSBTC"}
symbols := []string{"tBTCUSD", "fUSD"}
tm, err := c.Tickers.GetMulti(symbols)

if err != nil {
log.Fatalf("getMulti: %s", err)
}

spew.Dump(tm)
spew.Dump("getMulti:> ", tm)
}

func all(c *rest.Client) {
Expand All @@ -43,5 +43,5 @@ func all(c *rest.Client) {
log.Fatalf("all: %s", err)
}

spew.Dump(t)
spew.Dump("all:> ", t)
}
67 changes: 67 additions & 0 deletions pkg/models/tickerhist/tickerhist.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package tickerhist

import (
"fmt"

"github.com/bitfinexcom/bitfinex-api-go/pkg/convert"
)

type TickerHist struct {
Symbol string
Bid float64
// PLACEHOLDER,
Ask float64
// PLACEHOLDER,
// PLACEHOLDER,
// PLACEHOLDER,
// PLACEHOLDER,
// PLACEHOLDER,
// PLACEHOLDER,
// PLACEHOLDER,
// PLACEHOLDER,
MTS int64
}

var tickerHistFields = map[string]int{
"Symbol": 0,
"Bid": 1,
"Ask": 3,
"Mts": 12,
}

type Snapshot struct {
Snapshot []TickerHist
}

func SnapshotFromRaw(raw [][]interface{}) (ss Snapshot) {
if len(raw) == 0 {
return
}

snap := make([]TickerHist, 0)
for _, r := range raw {
th, err := FromRaw(r)
if err != nil {
continue
}
snap = append(snap, th)
}

return Snapshot{Snapshot: snap}
}

func FromRaw(raw []interface{}) (t TickerHist, err error) {
// to avoid index out of range issue
if len(raw) < 13 {
err = fmt.Errorf("data slice too short for ticker history, data:%#v", raw)
return
}

t = TickerHist{
Symbol: convert.SValOrEmpty(raw[tickerHistFields["Symbol"]]),
Bid: convert.F64ValOrZero(raw[tickerHistFields["Bid"]]),
Ask: convert.F64ValOrZero(raw[tickerHistFields["Ask"]]),
MTS: convert.I64ValOrZero(raw[tickerHistFields["Mts"]]),
}
return
}
87 changes: 87 additions & 0 deletions pkg/models/tickerhist/tickerhist_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package tickerhist_test

import (
"testing"

"github.com/bitfinexcom/bitfinex-api-go/pkg/models/tickerhist"
"github.com/stretchr/testify/assert"
)

func TestTickerHistFromRaw(t *testing.T) {
cases := map[string]struct {
pld []interface{}
expected tickerhist.TickerHist
err func(*testing.T, error)
}{
"invalid payload": {
pld: []interface{}{402088407},
expected: tickerhist.TickerHist{},
err: func(t *testing.T, err error) {
assert.Error(t, err)
},
},
"valid payload": {
pld: []interface{}{
"tBTCUSD", 54281, nil, 54282, nil, nil, nil, nil, nil, nil, nil, nil, 1619769715000,
},
expected: tickerhist.TickerHist{
Symbol: "tBTCUSD",
Bid: 54281,
Ask: 54282,
MTS: 1619769715000,
},
err: func(t *testing.T, err error) {
assert.NoError(t, err)
},
},
}

for k, v := range cases {
t.Run(k, func(t *testing.T) {
got, err := tickerhist.FromRaw(v.pld)
v.err(t, err)
assert.Equal(t, v.expected, got)
})
}
}

func TestTickerHistSnapshotFromRaw(t *testing.T) {
cases := map[string]struct {
pld [][]interface{}
expected tickerhist.Snapshot
}{
"invalid payload": {
pld: [][]interface{}{},
expected: tickerhist.Snapshot{},
},
"valid payload": {
pld: [][]interface{}{
{"tBTCUSD", 54281, nil, 54282, nil, nil, nil, nil, nil, nil, nil, nil, 1619769715000},
{"tLTCUSD", 264.66, nil, 264.9, nil, nil, nil, nil, nil, nil, nil, nil, 1619770205000},
},
expected: tickerhist.Snapshot{
Snapshot: []tickerhist.TickerHist{
{
Symbol: "tBTCUSD",
Bid: 54281,
Ask: 54282,
MTS: 1619769715000,
},
{
Symbol: "tLTCUSD",
Bid: 264.66,
Ask: 264.9,
MTS: 1619770205000,
},
},
},
},
}

for k, v := range cases {
t.Run(k, func(t *testing.T) {
got := tickerhist.SnapshotFromRaw(v.pld)
assert.Equal(t, v.expected, got)
})
}
}
36 changes: 19 additions & 17 deletions v2/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,24 @@ type Client struct {
nonce utils.NonceGenerator

// service providers
Candles CandleService
Orders OrderService
Positions PositionService
Trades TradeService
Tickers TickerService
Currencies CurrenciesService
Platform PlatformService
Book BookService
Wallet WalletService
Ledgers LedgerService
Stats StatsService
Status StatusService
Derivatives DerivativesService
Funding FundingService
Pulse PulseService
Invoice InvoiceService
Market MarketService
Candles CandleService
Orders OrderService
Positions PositionService
Trades TradeService
Tickers TickerService
TickersHistory TickerHistoryService
Currencies CurrenciesService
Platform PlatformService
Book BookService
Wallet WalletService
Ledgers LedgerService
Stats StatsService
Status StatusService
Derivatives DerivativesService
Funding FundingService
Pulse PulseService
Invoice InvoiceService
Market MarketService

Synchronous
}
Expand Down Expand Up @@ -113,6 +114,7 @@ func NewClientWithSynchronousURLNonce(sync Synchronous, url string, nonce utils.
c.Candles = CandleService{Synchronous: c}
c.Trades = TradeService{Synchronous: c, requestFactory: c}
c.Tickers = TickerService{Synchronous: c, requestFactory: c}
c.TickersHistory = TickerHistoryService{Synchronous: c, requestFactory: c}
c.Currencies = CurrenciesService{Synchronous: c, requestFactory: c}
c.Platform = PlatformService{Synchronous: c}
c.Positions = PositionService{Synchronous: c, requestFactory: c}
Expand Down
6 changes: 3 additions & 3 deletions v2/rest/tickers.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *TickerService) getTickers(symbols []string) ([]*ticker.Ticker, error) {
}

// Get - retrieves the ticker for the given symbol
// see https://docs.bitfinex.com/reference#rest-public-ticker for more info
// see https://docs.bitfinex.com/reference#rest-public-tickers for more info
func (s *TickerService) Get(symbol string) (*ticker.Ticker, error) {
t, err := s.getTickers([]string{symbol})
if err != nil {
Expand All @@ -46,13 +46,13 @@ func (s *TickerService) Get(symbol string) (*ticker.Ticker, error) {
}

// GetMulti - retrieves the tickers for the given symbols
// see https://docs.bitfinex.com/reference#rest-public-ticker for more info
// see https://docs.bitfinex.com/reference#rest-public-tickers for more info
func (s *TickerService) GetMulti(symbols []string) ([]*ticker.Ticker, error) {
return s.getTickers(symbols)
}

// All - retrieves all tickers for all symbols
// see https://docs.bitfinex.com/reference#rest-public-ticker for more info
// see https://docs.bitfinex.com/reference#rest-public-tickers for more info
func (s *TickerService) All() ([]*ticker.Ticker, error) {
return s.getTickers([]string{"ALL"})
}
Loading

0 comments on commit b4f52b0

Please sign in to comment.