-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #232 from Daniel1984/feature/rest-ticker-hist
Feature/rest ticker hist
- Loading branch information
Showing
9 changed files
with
400 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.