mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-06-07 07:26:48 +00:00
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:
@@ -54,7 +54,7 @@ type Contract struct {
|
||||
Status string `json:"status"`
|
||||
FundingFeeRate float64 `json:"fundingFeeRate"`
|
||||
PredictedFundingFeeRate float64 `json:"predictedFundingFeeRate"`
|
||||
OpenInterest string `json:"openInterest"`
|
||||
OpenInterest types.Number `json:"openInterest"`
|
||||
TurnoverOf24h float64 `json:"turnoverOf24h"`
|
||||
VolumeOf24h float64 `json:"volumeOf24h"`
|
||||
MarkPrice float64 `json:"markPrice"`
|
||||
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"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"
|
||||
@@ -2598,3 +2599,42 @@ func TestUpdateOrderExecutionLimits(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetOpenInterest(t *testing.T) {
|
||||
t.Parallel()
|
||||
_, err := ku.GetOpenInterest(context.Background(), key.PairAsset{
|
||||
Base: currency.ETH.Item,
|
||||
Quote: currency.USDT.Item,
|
||||
Asset: asset.USDTMarginedFutures,
|
||||
})
|
||||
assert.ErrorIs(t, err, asset.ErrNotSupported)
|
||||
|
||||
resp, err := ku.GetOpenInterest(context.Background(), key.PairAsset{
|
||||
Base: futuresTradablePair.Base.Item,
|
||||
Quote: futuresTradablePair.Quote.Item,
|
||||
Asset: asset.Futures,
|
||||
})
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, resp)
|
||||
|
||||
cp1 := currency.NewPair(currency.ETH, currency.USDTM)
|
||||
sharedtestvalues.SetupCurrencyPairsForExchangeAsset(t, ku, asset.Futures, cp1)
|
||||
resp, err = ku.GetOpenInterest(context.Background(),
|
||||
key.PairAsset{
|
||||
Base: futuresTradablePair.Base.Item,
|
||||
Quote: futuresTradablePair.Quote.Item,
|
||||
Asset: asset.Futures,
|
||||
},
|
||||
key.PairAsset{
|
||||
Base: cp1.Base.Item,
|
||||
Quote: cp1.Quote.Item,
|
||||
Asset: asset.Futures,
|
||||
},
|
||||
)
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, resp)
|
||||
|
||||
resp, err = ku.GetOpenInterest(context.Background())
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, resp)
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
"github.com/thrasher-corp/gocryptotrader/common"
|
||||
"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"
|
||||
@@ -132,6 +133,11 @@ func (ku *Kucoin) SetDefaults() {
|
||||
FundingRateBatching: map[asset.Item]bool{
|
||||
asset.Futures: true,
|
||||
},
|
||||
OpenInterest: exchange.OpenInterestSupport{
|
||||
Supported: true,
|
||||
SupportedViaTicker: true,
|
||||
SupportsRestBatch: true,
|
||||
},
|
||||
},
|
||||
MaximumOrderHistory: kline.OneDay.Duration() * 7,
|
||||
WithdrawPermissions: exchange.AutoWithdrawCrypto,
|
||||
@@ -379,6 +385,7 @@ func (ku *Kucoin) UpdateTickers(ctx context.Context, assetType asset.Item) error
|
||||
High: ticks[x].HighPrice,
|
||||
Low: ticks[x].LowPrice,
|
||||
Volume: ticks[x].VolumeOf24h,
|
||||
OpenInterest: ticks[x].OpenInterest.Float64(),
|
||||
Pair: pair,
|
||||
ExchangeName: ku.Name,
|
||||
AssetType: assetType,
|
||||
@@ -1987,3 +1994,49 @@ func (ku *Kucoin) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item)
|
||||
|
||||
return ku.LoadLimits(limits)
|
||||
}
|
||||
|
||||
// GetOpenInterest returns the open interest rate for a given asset pair
|
||||
func (ku *Kucoin) GetOpenInterest(ctx context.Context, k ...key.PairAsset) ([]futures.OpenInterest, error) {
|
||||
for i := range k {
|
||||
if k[i].Asset != asset.Futures {
|
||||
// avoid API calls or returning errors after a successful retrieval
|
||||
return nil, fmt.Errorf("%w %v %v", asset.ErrNotSupported, k[i].Asset, k[i].Pair())
|
||||
}
|
||||
}
|
||||
contracts, err := ku.GetFuturesOpenContracts(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := make([]futures.OpenInterest, 0, len(contracts))
|
||||
for i := range contracts {
|
||||
var symbol currency.Pair
|
||||
var enabled bool
|
||||
symbol, enabled, err = ku.MatchSymbolCheckEnabled(contracts[i].Symbol, asset.Futures, true)
|
||||
if err != nil && !errors.Is(err, currency.ErrPairNotFound) {
|
||||
return nil, err
|
||||
}
|
||||
if !enabled {
|
||||
continue
|
||||
}
|
||||
var appendData bool
|
||||
for j := range k {
|
||||
if k[j].Pair().Equal(symbol) {
|
||||
appendData = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if len(k) > 0 && !appendData {
|
||||
continue
|
||||
}
|
||||
resp = append(resp, futures.OpenInterest{
|
||||
Key: key.ExchangePairAsset{
|
||||
Exchange: ku.Name,
|
||||
Base: symbol.Base.Item,
|
||||
Quote: symbol.Quote.Item,
|
||||
Asset: asset.Futures,
|
||||
},
|
||||
OpenInterest: contracts[i].OpenInterest.Float64(),
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user