From a759f83e26ed4d1ce790eb572ac96b02a7e2ffae Mon Sep 17 00:00:00 2001 From: Ryan O'Hara-Reid Date: Fri, 23 Feb 2018 16:58:53 +1100 Subject: [PATCH] Added new function on bitfinex for V2 API trade history request. --- exchanges/bitfinex/bitfinex.go | 51 ++++++++++++++++++++++++++++ exchanges/bitfinex/bitfinex_test.go | 9 +++++ exchanges/bitfinex/bitfinex_types.go | 10 ++++++ 3 files changed, 70 insertions(+) diff --git a/exchanges/bitfinex/bitfinex.go b/exchanges/bitfinex/bitfinex.go index be2e9aed..8cbb60ba 100644 --- a/exchanges/bitfinex/bitfinex.go +++ b/exchanges/bitfinex/bitfinex.go @@ -29,6 +29,7 @@ const ( bitfinexOrderbookV2 = "book" bitfinexOrderbook = "book/" bitfinexTrades = "trades/" + bitfinexTradesV2 = "https://api.bitfinex.com/v2/trades/%s/hist?limit=1000&start=%s&end=%s" bitfinexKeyPermissions = "key_info" bitfinexLends = "lends/" bitfinexSymbols = "symbols/" @@ -336,6 +337,56 @@ func (b *Bitfinex) GetTrades(currencyPair string, values url.Values) ([]TradeStr return response, common.SendHTTPGetRequest(path, true, b.Verbose, &response) } +// GetTradesV2 uses the V2 API to get historic trades that occured on the +// exchange +// +// currencyPair e.g. "tBTCUSD" v2 prefixes currency pairs with t. (?) +// timestampStart is an int64 unix epoch time +// timestampEnd is an int64 unix epoch time, make sure this is always there or +// you will get the most recent trades. +// reOrderResp reorders the returned data. +func (b *Bitfinex) GetTradesV2(currencyPair string, timestampStart, timestampEnd int64, reOrderResp bool) ([]TradeStructureV2, error) { + var resp [][]interface{} + var actualHistory []TradeStructureV2 + + path := fmt.Sprintf(bitfinexTradesV2, + currencyPair, + strconv.FormatInt(timestampStart, 10), + strconv.FormatInt(timestampEnd, 10)) + + err := common.SendHTTPGetRequest(path, true, b.Verbose, &resp) + if err != nil { + return actualHistory, err + } + + var tempHistory TradeStructureV2 + for _, data := range resp { + tempHistory.TID = int64(data[0].(float64)) + tempHistory.Timestamp = int64(data[1].(float64)) + tempHistory.Amount = data[2].(float64) + tempHistory.Price = data[3].(float64) + tempHistory.Exchange = b.Name + tempHistory.Type = "BUY" + + if tempHistory.Amount < 0 { + tempHistory.Type = "SELL" + tempHistory.Amount = tempHistory.Amount * -1 + } + + actualHistory = append(actualHistory, tempHistory) + } + + //re-order index + if reOrderResp { + orderedHistory := make([]TradeStructureV2, len(actualHistory)) + for i, quickRange := range actualHistory { + orderedHistory[len(actualHistory)-i-1] = quickRange + } + return orderedHistory, nil + } + return actualHistory, nil +} + // GetLendbook returns a list of the most recent funding data for the given // currency: total amount provided and Flash Return Rate (in % by 365 days) over // time diff --git a/exchanges/bitfinex/bitfinex_test.go b/exchanges/bitfinex/bitfinex_test.go index 9bc77e7f..b33b1f09 100644 --- a/exchanges/bitfinex/bitfinex_test.go +++ b/exchanges/bitfinex/bitfinex_test.go @@ -162,6 +162,15 @@ func TestGetTrades(t *testing.T) { } } +func TestGetTradesv2(t *testing.T) { + t.Parallel() + + _, err := b.GetTradesV2("tBTCUSD", 0, 0, true) + if err != nil { + t.Error("BitfinexGetTrades init error: ", err) + } +} + func TestGetLends(t *testing.T) { t.Parallel() diff --git a/exchanges/bitfinex/bitfinex_types.go b/exchanges/bitfinex/bitfinex_types.go index c4b570cb..bcfef4a7 100644 --- a/exchanges/bitfinex/bitfinex_types.go +++ b/exchanges/bitfinex/bitfinex_types.go @@ -78,6 +78,16 @@ type TradeStructure struct { Type string `json:"sell"` } +// TradeStructureV2 holds resp information +type TradeStructureV2 struct { + Timestamp int64 + TID int64 + Price float64 + Amount float64 + Exchange string + Type string +} + // Lendbook holds most recent funding data for a relevant currency type Lendbook struct { Bids []Book `json:"bids"`