mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-30 23:16:52 +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:
@@ -10,6 +10,7 @@ import (
|
||||
|
||||
"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"
|
||||
@@ -53,6 +54,7 @@ func TestMain(m *testing.M) {
|
||||
if err = b.Setup(btseConfig); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
os.Exit(m.Run())
|
||||
}
|
||||
|
||||
@@ -737,3 +739,43 @@ func updatePairsOnce(tb testing.TB) {
|
||||
assert.NoError(tb, err, "UpdateTradablePairs should not error")
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetOpenInterest(t *testing.T) {
|
||||
t.Parallel()
|
||||
cp1 := currency.NewPair(currency.BTC, currency.PFC)
|
||||
cp2 := currency.NewPair(currency.ETH, currency.PFC)
|
||||
sharedtestvalues.SetupCurrencyPairsForExchangeAsset(t, b, asset.Futures, futuresPair, cp1, cp2)
|
||||
|
||||
resp, err := b.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 = b.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)
|
||||
|
||||
resp, err = b.GetOpenInterest(context.Background())
|
||||
assert.NoError(t, err)
|
||||
assert.NotEmpty(t, resp)
|
||||
|
||||
_, err = b.GetOpenInterest(context.Background(), key.PairAsset{
|
||||
Base: currency.BTC.Item,
|
||||
Quote: currency.USDT.Item,
|
||||
Asset: asset.Spot,
|
||||
})
|
||||
assert.ErrorIs(t, err, asset.ErrNotSupported)
|
||||
}
|
||||
|
||||
@@ -13,6 +13,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"
|
||||
@@ -137,6 +138,11 @@ func (b *BTSE) SetDefaults() {
|
||||
FundingRateBatching: map[asset.Item]bool{
|
||||
asset.Futures: true,
|
||||
},
|
||||
OpenInterest: exchange.OpenInterestSupport{
|
||||
Supported: true,
|
||||
SupportsRestBatch: true,
|
||||
SupportedViaTicker: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
Enabled: exchange.FeaturesEnabled{
|
||||
@@ -333,6 +339,7 @@ func (b *BTSE) UpdateTickers(ctx context.Context, a asset.Item) error {
|
||||
Last: tickers[x].Last,
|
||||
Volume: tickers[x].Volume,
|
||||
High: tickers[x].High24Hr,
|
||||
OpenInterest: tickers[x].OpenInterest,
|
||||
ExchangeName: b.Name,
|
||||
AssetType: a})
|
||||
}
|
||||
@@ -1315,3 +1322,49 @@ func (b *BTSE) IsPerpetualFutureCurrency(a asset.Item, p currency.Pair) (bool, e
|
||||
func (b *BTSE) UpdateOrderExecutionLimits(_ context.Context, _ asset.Item) error {
|
||||
return common.ErrNotYetImplemented
|
||||
}
|
||||
|
||||
// GetOpenInterest returns the open interest rate for a given asset pair
|
||||
func (b *BTSE) 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())
|
||||
}
|
||||
}
|
||||
tickers, err := b.GetMarketSummary(ctx, "", false)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := make([]futures.OpenInterest, 0, len(tickers))
|
||||
for i := range tickers {
|
||||
var symbol currency.Pair
|
||||
var enabled bool
|
||||
symbol, enabled, err = b.MatchSymbolCheckEnabled(tickers[i].Symbol, asset.Futures, false)
|
||||
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: b.Name,
|
||||
Base: symbol.Base.Item,
|
||||
Quote: symbol.Quote.Item,
|
||||
Asset: asset.Futures,
|
||||
},
|
||||
OpenInterest: tickers[i].OpenInterest,
|
||||
})
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user