exchanges/futures: Implement open interest (#1417)

* adds open interest to exchanges

* ADDS TESTING YEAH

* New endpoints, BTSE, RPCS, cached

* slight design change, begin gateio

You will need to get cached for
each exchange that supports it

* gateio, huobi, rpc

* fix up kraken, cache retrieval

* okx, gateio

* finalising all implementations and tests

* definitely my final ever commit on this

* Well, well, well

* final v2

* quick fix of bug

* test coverage, assert notempty, test helper

Added a new testhelper for currency
management because its very annoying
in a parallel test setting which wastes
so much space otherwise

* minimises REST requests for Open Interest

* types.Number merge misses

* Minimises Kraken REST calls

* len change, value -> pointer receiver

* further fixup

* fixes gateio, batch calculates open interest

* single gateio, lint const fixes

* rejig and more thorough oi for huobi

* formatting expansion

* minor fix for handling expiring contracts

* rm unused Binance strings

* add bybit support, fix bybit issues

* oopsie doopsie, dont look at my whoopsie

* Fix issue, remove feature

* move an irrelevant function for the pr

* mini bybit upgrades

* fixes cli request bug
This commit is contained in:
Scott
2024-01-12 15:27:35 +11:00
committed by GitHub
parent 614042110a
commit b71bf1f3d1
62 changed files with 22660 additions and 10095 deletions

View File

@@ -275,28 +275,16 @@ type FuturesTradeHistoryData struct {
} `json:"history"`
}
// FuturesTickersData stores info for futures tickers
type FuturesTickersData struct {
Tickers []FuturesTicker `json:"tickers"`
ServerTime string `json:"serverTime"`
}
// FuturesTickerData stores info for futures ticker
type FuturesTickerData struct {
Tickers []struct {
Tag string `json:"tag"`
Pair string `json:"pair"`
Symbol string `json:"symbol"`
MarkPrice float64 `json:"markPrice"`
Bid float64 `json:"bid"`
BidSize float64 `json:"bidSize"`
Ask float64 `json:"ask"`
AskSize float64 `json:"askSize"`
Vol24h float64 `json:"vol24h"`
OpenInterest float64 `json:"openInterest"`
Open24H float64 `json:"open24h"`
Last float64 `json:"last"`
LastTime string `json:"lastTime"`
LastSize float64 `json:"lastSize"`
Suspended bool `json:"suspended"`
FundingRate float64 `json:"fundingRate"`
FundingRatePrediction float64 `json:"fundingRatePrediction"`
} `json:"tickers"`
ServerTime string `json:"serverTime"`
Ticker FuturesTicker `json:"ticker"`
ServerTime string `json:"serverTime"`
}
// FuturesEditedOrderData stores an edited order's data
@@ -315,6 +303,27 @@ type FuturesEditedOrderData struct {
} `json:"editStatus"`
}
// FuturesTicker holds futures ticker data
type FuturesTicker struct {
Tag string `json:"tag"`
Pair string `json:"pair"`
Symbol string `json:"symbol"`
MarkPrice float64 `json:"markPrice"`
Bid float64 `json:"bid"`
BidSize float64 `json:"bidSize"`
Ask float64 `json:"ask"`
AskSize float64 `json:"askSize"`
Vol24h float64 `json:"vol24h"`
OpenInterest float64 `json:"openInterest"`
Open24H float64 `json:"open24h"`
Last float64 `json:"last"`
LastTime string `json:"lastTime"`
LastSize float64 `json:"lastSize"`
Suspended bool `json:"suspended"`
FundingRate float64 `json:"fundingRate"`
FundingRatePrediction float64 `json:"fundingRatePrediction"`
}
// FuturesSendOrderData stores send order data
type FuturesSendOrderData struct {
SendStatus struct {

View File

@@ -79,11 +79,17 @@ func (k *Kraken) GetInstruments(ctx context.Context) (FuturesInstrumentData, err
}
// GetFuturesTickers gets a list of futures tickers and their data
func (k *Kraken) GetFuturesTickers(ctx context.Context) (FuturesTickerData, error) {
var resp FuturesTickerData
func (k *Kraken) GetFuturesTickers(ctx context.Context) (FuturesTickersData, error) {
var resp FuturesTickersData
return resp, k.SendHTTPRequest(ctx, exchange.RestFutures, futuresTickers, &resp)
}
// GetFuturesTickerBySymbol returns futures ticker data by symbol
func (k *Kraken) GetFuturesTickerBySymbol(ctx context.Context, symbol string) (FuturesTickerData, error) {
var resp FuturesTickerData
return resp, k.SendHTTPRequest(ctx, exchange.RestFutures, futuresTickers+"/"+symbol, &resp)
}
// GetFuturesTradeHistory gets public trade history data for futures
func (k *Kraken) GetFuturesTradeHistory(ctx context.Context, symbol currency.Pair, lastTime time.Time) (FuturesTradeHistoryData, error) {
var resp FuturesTradeHistoryData

View File

@@ -16,6 +16,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/core"
"github.com/thrasher-corp/gocryptotrader/currency"
@@ -2222,7 +2223,7 @@ func TestGetLatestFundingRates(t *testing.T) {
cp := currency.NewPair(currency.PF, currency.NewCode("XBTUSD"))
cp.Delimiter = "_"
err = k.CurrencyPairs.EnablePair(asset.Futures, cp)
if !errors.Is(err, nil) {
if err != nil && !errors.Is(err, currency.ErrPairAlreadyEnabled) {
t.Fatal(err)
}
_, err = k.GetLatestFundingRates(context.Background(), &fundingrate.LatestRateRequest{
@@ -2261,3 +2262,43 @@ func TestIsPerpetualFutureCurrency(t *testing.T) {
t.Error("expected true")
}
}
func TestGetOpenInterest(t *testing.T) {
t.Parallel()
_, err := k.GetOpenInterest(context.Background(), key.PairAsset{
Base: currency.ETH.Item,
Quote: currency.USDT.Item,
Asset: asset.USDTMarginedFutures,
})
assert.ErrorIs(t, err, asset.ErrNotSupported)
cp1 := currency.NewPair(currency.PF, currency.NewCode("ETHUSD"))
cp2 := currency.NewPair(currency.PF, currency.NewCode("XBTUSD"))
sharedtestvalues.SetupCurrencyPairsForExchangeAsset(t, k, asset.Futures, cp1, cp2)
resp, err := k.GetOpenInterest(context.Background(), key.PairAsset{
Base: cp1.Base.Item,
Quote: cp1.Quote.Item,
Asset: asset.Futures,
})
assert.NoError(t, err)
assert.NotEmpty(t, resp)
resp, err = k.GetOpenInterest(context.Background(),
key.PairAsset{
Base: cp1.Base.Item,
Quote: cp1.Quote.Item,
Asset: asset.Futures,
},
key.PairAsset{
Base: cp2.Base.Item,
Quote: cp2.Quote.Item,
Asset: asset.Futures,
})
assert.NoError(t, err)
assert.NotEmpty(t, resp)
_, err = k.GetOpenInterest(context.Background())
assert.NoError(t, err)
assert.NotEmpty(t, resp)
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/shopspring/decimal"
"github.com/thrasher-corp/gocryptotrader/common"
"github.com/thrasher-corp/gocryptotrader/common/convert"
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
@@ -167,6 +168,11 @@ func (k *Kraken) SetDefaults() {
FundingRateBatching: map[asset.Item]bool{
asset.Futures: true,
},
OpenInterest: exchange.OpenInterestSupport{
Supported: true,
SupportsRestBatch: true,
SupportedViaTicker: true,
},
},
},
Enabled: exchange.FeaturesEnabled{
@@ -544,6 +550,7 @@ func (k *Kraken) UpdateTickers(ctx context.Context, a asset.Item) error {
Ask: t.Tickers[x].Ask,
Volume: t.Tickers[x].Vol24h,
Open: t.Tickers[x].Open24H,
OpenInterest: t.Tickers[x].OpenInterest,
Pair: pair,
ExchangeName: k.Name,
AssetType: a})
@@ -1856,3 +1863,49 @@ func (k *Kraken) GetLatestFundingRates(ctx context.Context, r *fundingrate.Lates
func (k *Kraken) IsPerpetualFutureCurrency(a asset.Item, cp currency.Pair) (bool, error) {
return cp.Base.Equal(currency.PF) && a == asset.Futures, nil
}
// GetOpenInterest returns the open interest rate for a given asset pair
func (k *Kraken) GetOpenInterest(ctx context.Context, keys ...key.PairAsset) ([]futures.OpenInterest, error) {
for i := range keys {
if keys[i].Asset != asset.Futures {
// avoid API calls or returning errors after a successful retrieval
return nil, fmt.Errorf("%w %v %v", asset.ErrNotSupported, keys[i].Asset, keys[i].Pair())
}
}
futuresTickersData, err := k.GetFuturesTickers(ctx)
if err != nil {
return nil, err
}
resp := make([]futures.OpenInterest, 0, len(futuresTickersData.Tickers))
for i := range futuresTickersData.Tickers {
var p currency.Pair
var isEnabled bool
p, isEnabled, err = k.MatchSymbolCheckEnabled(futuresTickersData.Tickers[i].Symbol, asset.Futures, true)
if err != nil && !errors.Is(err, currency.ErrPairNotFound) {
return nil, err
}
if !isEnabled {
continue
}
var appendData bool
for j := range keys {
if keys[j].Pair().Equal(p) {
appendData = true
break
}
}
if len(keys) > 0 && !appendData {
continue
}
resp = append(resp, futures.OpenInterest{
Key: key.ExchangePairAsset{
Exchange: k.Name,
Base: p.Base.Item,
Quote: p.Quote.Item,
Asset: asset.Futures,
},
OpenInterest: futuresTickersData.Tickers[i].OpenInterest,
})
}
return resp, nil
}