Skip to content

Commit

Permalink
Binance: convert timestamps to time.Time (thrasher-corp#601)
Browse files Browse the repository at this point in the history
* add custom Unmarshal function for JSON types
* pass KlinesRequestParams as pointer, because now the object is bigger
  • Loading branch information
Rots authored Nov 30, 2020
1 parent 46747c1 commit b7d99f7
Show file tree
Hide file tree
Showing 6 changed files with 573 additions and 235 deletions.
20 changes: 10 additions & 10 deletions exchanges/binance/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,10 @@ func (b *Binance) GetAggregatedTrades(arg *AggregatedTradeRequestParams) ([]Aggr
params.Set("fromId", strconv.FormatInt(arg.FromID, 10))
}
if !arg.StartTime.IsZero() {
params.Set("startTime", strconv.FormatInt(convert.UnixMillis(arg.StartTime), 10))
params.Set("startTime", timeString(arg.StartTime))
}
if !arg.EndTime.IsZero() {
params.Set("endTime", strconv.FormatInt(convert.UnixMillis(arg.EndTime), 10))
params.Set("endTime", timeString(arg.EndTime))
}

// startTime and endTime are set and time between startTime and endTime is more than 1 hour
Expand Down Expand Up @@ -231,8 +231,8 @@ func (b *Binance) batchAggregateTrades(arg *AggregatedTradeRequestParams, params
// All requests returned empty
return nil, nil
}
params.Set("startTime", strconv.FormatInt(convert.UnixMillis(start), 10))
params.Set("endTime", strconv.FormatInt(convert.UnixMillis(start.Add(time.Hour)), 10))
params.Set("startTime", timeString(start))
params.Set("endTime", timeString(start.Add(time.Hour)))
path := b.API.Endpoints.URL + aggregatedTrades + "?" + params.Encode()
err := b.SendHTTPRequest(path, limitDefault, &resp)
if err != nil {
Expand Down Expand Up @@ -260,7 +260,7 @@ func (b *Binance) batchAggregateTrades(arg *AggregatedTradeRequestParams, params
if !arg.EndTime.IsZero() {
// get index for truncating to end time
lastIndex = sort.Search(len(additionalTrades), func(i int) bool {
return convert.UnixMillis(arg.EndTime) < additionalTrades[i].TimeStamp
return arg.EndTime.Before(additionalTrades[i].TimeStamp)
})
}
// don't include the first as the request was inclusive from last ATradeID
Expand All @@ -286,7 +286,7 @@ func (b *Binance) batchAggregateTrades(arg *AggregatedTradeRequestParams, params
// interval: the interval time for the data
// startTime: startTime filter for kline data
// endTime: endTime filter for the kline data
func (b *Binance) GetSpotKline(arg KlinesRequestParams) ([]CandleStick, error) {
func (b *Binance) GetSpotKline(arg *KlinesRequestParams) ([]CandleStick, error) {
var resp interface{}
var klineData []CandleStick

Expand All @@ -296,11 +296,11 @@ func (b *Binance) GetSpotKline(arg KlinesRequestParams) ([]CandleStick, error) {
if arg.Limit != 0 {
params.Set("limit", strconv.Itoa(arg.Limit))
}
if arg.StartTime != 0 {
params.Set("startTime", strconv.FormatInt(arg.StartTime, 10))
if !arg.StartTime.IsZero() {
params.Set("startTime", timeString(arg.StartTime))
}
if arg.EndTime != 0 {
params.Set("endTime", strconv.FormatInt(arg.EndTime, 10))
if !arg.EndTime.IsZero() {
params.Set("endTime", timeString(arg.EndTime))
}

path := fmt.Sprintf("%s%s?%s", b.API.Endpoints.URL, candleStick, params.Encode())
Expand Down
17 changes: 11 additions & 6 deletions exchanges/binance/binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ func setFeeBuilder() *exchange.FeeBuilder {

func TestGetExchangeInfo(t *testing.T) {
t.Parallel()
_, err := b.GetExchangeInfo()
info, err := b.GetExchangeInfo()
if err != nil {
t.Error(err)
}
if mockTests {
serverTime := time.Date(2020, 4, 15, 23, 44, 38, int(861*time.Millisecond), time.UTC)
if !info.Servertime.Equal(serverTime) {
t.Errorf("Expected %v, got %v", serverTime, info.Servertime)
}
}
}

func TestFetchTradablePairs(t *testing.T) {
Expand Down Expand Up @@ -104,12 +110,12 @@ func TestGetAggregatedTrades(t *testing.T) {

func TestGetSpotKline(t *testing.T) {
t.Parallel()
_, err := b.GetSpotKline(KlinesRequestParams{
_, err := b.GetSpotKline(&KlinesRequestParams{
Symbol: "BTCUSDT",
Interval: kline.FiveMin.Short(),
Limit: 24,
StartTime: time.Unix(1577836800, 0).Unix() * 1000,
EndTime: time.Unix(1580515200, 0).Unix() * 1000,
StartTime: time.Unix(1577836800, 0),
EndTime: time.Unix(1580515200, 0),
})
if err != nil {
t.Error("Binance GetSpotKline() error", err)
Expand Down Expand Up @@ -514,8 +520,7 @@ func TestGetAggregatedTradesBatched(t *testing.T) {
if len(result) != tt.numExpected {
t.Errorf("GetAggregatedTradesBatched() expected %v entries, got %v", tt.numExpected, len(result))
}
lastTrade := result[len(result)-1]
lastTradeTime := time.Unix(0, lastTrade.TimeStamp*int64(time.Millisecond))
lastTradeTime := result[len(result)-1].TimeStamp
if !lastTradeTime.Equal(tt.lastExpected) {
t.Errorf("last trade expected %v, got %v", tt.lastExpected, lastTradeTime)
}
Expand Down
Loading

0 comments on commit b7d99f7

Please sign in to comment.