Skip to content

Instantly share code, notes, and snippets.

@gloriousCode
Last active April 19, 2024 02:17
Show Gist options
  • Save gloriousCode/ddd86eacc2793be177fe61d7027314a0 to your computer and use it in GitHub Desktop.
Save gloriousCode/ddd86eacc2793be177fe61d7027314a0 to your computer and use it in GitHub Desktop.
historical rates
diff --git a/exchanges/deribit/deribit_test.go b/exchanges/deribit/deribit_test.go
index 619ce7cae..63360cc67 100644
--- a/exchanges/deribit/deribit_test.go
+++ b/exchanges/deribit/deribit_test.go
@@ -3019,14 +3019,13 @@ func TestGetHistoricalFundingRates(t *testing.T) {
Asset: asset.Futures,
Pair: cp,
PaymentCurrency: currency.USDT,
- StartDate: time.Now().Add(-time.Hour * 24 * 7),
+ StartDate: time.Now().Add(-time.Hour * 24 * 2),
EndDate: time.Now(),
}
_, err = d.GetHistoricalFundingRates(context.Background(), r)
require.NoError(t, err)
- r.StartDate = time.Now().Add(-time.Hour * 24 * 20)
-
- r.RespectHistoryLimits = true
+ r.StartDate = time.Now().Add(-time.Hour * 24 * 90)
+ r.EndDate = r.StartDate.Add(time.Hour * 24 * 7)
result, err := d.GetHistoricalFundingRates(context.Background(), r)
require.NoError(t, err)
require.NotNil(t, result)
diff --git a/exchanges/deribit/deribit_wrapper.go b/exchanges/deribit/deribit_wrapper.go
index a90786f26..2c12dce6b 100644
--- a/exchanges/deribit/deribit_wrapper.go
+++ b/exchanges/deribit/deribit_wrapper.go
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
+ "sort"
"strconv"
"strings"
"time"
@@ -1557,20 +1558,6 @@ func (d *Deribit) GetHistoricalFundingRates(ctx context.Context, r *fundingrate.
if r.Pair.IsEmpty() {
return nil, currency.ErrCurrencyPairEmpty
}
- if d.Features.Supports.FuturesCapabilities.MaximumFundingRateHistory != 0 {
- maxLookback := time.Now().Add(-d.Features.Supports.FuturesCapabilities.MaximumFundingRateHistory)
- if r.StartDate.Before(maxLookback) {
- if r.RespectHistoryLimits {
- r.StartDate = maxLookback
- } else {
- return nil, fmt.Errorf("%w earliest date is %v", fundingrate.ErrFundingRateOutsideLimits, maxLookback)
- }
- if r.EndDate.Before(maxLookback) {
- return nil, futures.ErrGetFundingDataRequired
- }
- r.StartDate = maxLookback
- }
- }
if !r.StartDate.IsZero() && !r.EndDate.IsZero() {
err := common.StartEndTimeCheck(r.StartDate, r.EndDate)
if err != nil {
@@ -1584,45 +1571,48 @@ func (d *Deribit) GetHistoricalFundingRates(ctx context.Context, r *fundingrate.
if err != nil {
return nil, err
}
- startDate := r.StartDate
+ ed := r.EndDate
- fundingRates := []fundingrate.Rate{}
- latestRate := new(fundingrate.Rate)
+ var fundingRates []fundingrate.Rate
+ mfr := make(map[int64]struct{})
for {
- if startDate.Equal(r.EndDate) || startDate.After(r.EndDate) {
+ if ed.Equal(r.StartDate) || ed.Before(r.StartDate) {
break
}
var records []FundingRateHistory
if d.Websocket.IsConnected() {
- records, err = d.WSRetrieveFundingRateHistory(fPair.String(), startDate, r.EndDate)
+ records, err = d.WSRetrieveFundingRateHistory(fPair.String(), r.StartDate, ed)
} else {
- records, err = d.GetFundingRateHistory(ctx, fPair.String(), startDate, r.EndDate)
+ records, err = d.GetFundingRateHistory(ctx, fPair.String(), r.StartDate, ed)
}
if err != nil {
return nil, err
}
- if len(records) == 0 {
+ if len(records) == 0 || ed.Equal(records[0].Timestamp.Time()) {
break
}
for i := range records {
- if r.EndDate.Before(records[i].Timestamp.Time()) ||
- r.StartDate.After(records[i].Timestamp.Time()) {
+ rt := records[i].Timestamp.Time()
+ if rt.Before(r.StartDate) || rt.After(r.EndDate) {
continue
}
- if records[i].Timestamp.Time().Unix() > latestRate.Time.Unix() {
- latestRate.Time = records[i].Timestamp.Time()
- latestRate.Rate = decimal.NewFromFloat(records[i].Interest1H)
+ if _, ok := mfr[rt.UnixMilli()]; ok {
+ continue
}
fundingRates = append(fundingRates, fundingrate.Rate{
Rate: decimal.NewFromFloat(records[i].Interest1H),
- Time: records[i].Timestamp.Time(),
+ Time: rt,
})
+ mfr[rt.UnixMilli()] = struct{}{}
}
- startDate = latestRate.Time
+ ed = records[0].Timestamp.Time()
}
if len(fundingRates) == 0 {
return nil, fundingrate.ErrNoFundingRatesFound
}
+ sort.Slice(fundingRates, func(i, j int) bool {
+ return fundingRates[i].Time.Before(fundingRates[j].Time)
+ })
return &fundingrate.HistoricalRates{
Exchange: d.Name,
Asset: r.Asset,
@@ -1630,7 +1620,7 @@ func (d *Deribit) GetHistoricalFundingRates(ctx context.Context, r *fundingrate.
FundingRates: fundingRates,
StartDate: fundingRates[0].Time,
EndDate: r.EndDate,
- LatestRate: *latestRate,
+ LatestRate: fundingRates[len(fundingRates)-1],
PaymentCurrency: r.PaymentCurrency,
}, nil
}
@gloriousCode
Copy link
Author

Its because the API endpoint GetFundingRateHistory responds to the end date changes rather than start date. So we go in descending order instead, then sort the results

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment