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

@@ -263,6 +263,15 @@ func (b *Bitmex) GetFullFundingHistory(ctx context.Context, symbol, count, filte
&fundingHistory)
}
// GetInstrument returns instrument data
func (b *Bitmex) GetInstrument(ctx context.Context, params *GenericRequestParams) ([]Instrument, error) {
var instruments []Instrument
return instruments, b.SendHTTPRequest(ctx, exchange.RestSpot, bitmexEndpointInstruments,
params,
&instruments)
}
// GetInstruments returns instrument data
func (b *Bitmex) GetInstruments(ctx context.Context, params *GenericRequestParams) ([]Instrument, error) {
var instruments []Instrument

View File

@@ -11,7 +11,9 @@ import (
"time"
"github.com/gorilla/websocket"
"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"
@@ -1315,3 +1317,43 @@ func TestIsPerpetualFutureCurrency(t *testing.T) {
t.Error("expected true")
}
}
func TestGetOpenInterest(t *testing.T) {
t.Parallel()
cp1 := currency.NewPair(currency.XBT, currency.USD)
cp2 := currency.NewPair(currency.DOGE, currency.USD)
sharedtestvalues.SetupCurrencyPairsForExchangeAsset(t, b, asset.PerpetualContract, cp1, cp2)
resp, err := b.GetOpenInterest(context.Background(), key.PairAsset{
Base: currency.XBT.Item,
Quote: currency.USD.Item,
Asset: asset.PerpetualContract,
})
assert.NoError(t, err)
assert.NotEmpty(t, resp)
resp, err = b.GetOpenInterest(context.Background(),
key.PairAsset{
Base: currency.XBT.Item,
Quote: currency.USD.Item,
Asset: asset.PerpetualContract,
},
key.PairAsset{
Base: currency.DOGE.Item,
Quote: currency.USD.Item,
Asset: asset.PerpetualContract,
})
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)
}

View File

@@ -163,18 +163,18 @@ type Instrument struct {
LimitDownPrice float64 `json:"limitDownPrice"`
LimitUpPrice float64 `json:"limitUpPrice"`
Listing string `json:"listing"`
LotSize int64 `json:"lotSize"`
LotSize float64 `json:"lotSize"`
LowPrice float64 `json:"lowPrice"`
MaintMargin float64 `json:"maintMargin"`
MakerFee float64 `json:"makerFee"`
MarkMethod string `json:"markMethod"`
MarkPrice float64 `json:"markPrice"`
MaxOrderQty int64 `json:"maxOrderQty"`
MaxOrderQty float64 `json:"maxOrderQty"`
MaxPrice float64 `json:"maxPrice"`
MidPrice float64 `json:"midPrice"`
Multiplier int64 `json:"multiplier"`
OpenInterest int64 `json:"openInterest"`
OpenValue int64 `json:"openValue"`
Multiplier float64 `json:"multiplier"`
OpenInterest float64 `json:"openInterest"`
OpenValue float64 `json:"openValue"`
OpeningTimestamp time.Time `json:"openingTimestamp"`
OptionMultiplier float64 `json:"optionMultiplier"`
OptionStrikePcnt float64 `json:"optionStrikePcnt"`
@@ -184,8 +184,8 @@ type Instrument struct {
PositionCurrency string `json:"positionCurrency"`
PrevClosePrice float64 `json:"prevClosePrice"`
PrevPrice24h float64 `json:"prevPrice24h"`
PrevTotalTurnover int64 `json:"prevTotalTurnover"`
PrevTotalVolume int64 `json:"prevTotalVolume"`
PrevTotalTurnover float64 `json:"prevTotalTurnover"`
PrevTotalVolume float64 `json:"prevTotalVolume"`
PublishInterval string `json:"publishInterval"`
PublishTime string `json:"publishTime"`
QuoteCurrency string `json:"quoteCurrency"`
@@ -195,8 +195,8 @@ type Instrument struct {
Reference string `json:"reference"`
ReferenceSymbol string `json:"referenceSymbol"`
RelistInterval string `json:"relistInterval"`
RiskLimit int64 `json:"riskLimit"`
RiskStep int64 `json:"riskStep"`
RiskLimit float64 `json:"riskLimit"`
RiskStep float64 `json:"riskStep"`
RootSymbol string `json:"rootSymbol"`
SellLeg string `json:"sellLeg"`
SessionInterval string `json:"sessionInterval"`
@@ -210,15 +210,15 @@ type Instrument struct {
Taxed bool `json:"taxed"`
TickSize float64 `json:"tickSize"`
Timestamp time.Time `json:"timestamp"`
TotalTurnover int64 `json:"totalTurnover"`
TotalVolume int64 `json:"totalVolume"`
Turnover int64 `json:"turnover"`
Turnover24h int64 `json:"turnover24h"`
TotalTurnover float64 `json:"totalTurnover"`
TotalVolume float64 `json:"totalVolume"`
Turnover float64 `json:"turnover"`
Turnover24h float64 `json:"turnover24h"`
Typ string `json:"typ"`
Underlying string `json:"underlying"`
UnderlyingSymbol string `json:"underlyingSymbol"`
UnderlyingToPositionMultiplier int64 `json:"underlyingToPositionMultiplier"`
UnderlyingToSettleMultiplier int64 `json:"underlyingToSettleMultiplier"`
UnderlyingToPositionMultiplier float64 `json:"underlyingToPositionMultiplier"`
UnderlyingToSettleMultiplier float64 `json:"underlyingToSettleMultiplier"`
Volume float64 `json:"volume"`
Volume24h float64 `json:"volume24h"`
Vwap float64 `json:"vwap"`

View File

@@ -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"
@@ -145,6 +146,11 @@ func (b *Bitmex) SetDefaults() {
FundingRateBatching: map[asset.Item]bool{
asset.PerpetualContract: true,
},
OpenInterest: exchange.OpenInterestSupport{
Supported: true,
SupportedViaTicker: true,
SupportsRestBatch: true,
},
},
WithdrawPermissions: exchange.AutoWithdrawCryptoWithAPIPermission |
exchange.WithdrawCryptoWithEmail |
@@ -431,6 +437,7 @@ instruments:
Pair: pair,
LastUpdated: tick[j].Timestamp,
ExchangeName: b.Name,
OpenInterest: tick[j].OpenInterest,
AssetType: a})
if err != nil {
return err
@@ -1172,7 +1179,7 @@ func (b *Bitmex) GetFuturesContractDetails(ctx context.Context, item asset.Item)
Type: futures.Perpetual,
SettlementType: contractSettlementType,
SettlementCurrencies: currency.Currencies{currency.NewCode(marketInfo[x].SettlCurrency)},
Multiplier: float64(marketInfo[x].Multiplier),
Multiplier: marketInfo[x].Multiplier,
LatestRate: fundingrate.Rate{
Time: marketInfo[x].FundingTimestamp,
Rate: decimal.NewFromFloat(marketInfo[x].FundingRate),
@@ -1242,7 +1249,7 @@ func (b *Bitmex) GetFuturesContractDetails(ctx context.Context, item asset.Item)
Status: marketInfo[x].State,
Type: ct,
SettlementCurrencies: currency.Currencies{currency.NewCode(marketInfo[x].SettlCurrency)},
Multiplier: float64(marketInfo[x].Multiplier),
Multiplier: marketInfo[x].Multiplier,
SettlementType: contractSettlementType,
})
}
@@ -1337,3 +1344,82 @@ func (b *Bitmex) IsPerpetualFutureCurrency(a asset.Item, _ currency.Pair) (bool,
func (b *Bitmex) UpdateOrderExecutionLimits(_ context.Context, _ asset.Item) error {
return common.ErrNotYetImplemented
}
// GetOpenInterest returns the open interest rate for a given asset pair
func (b *Bitmex) GetOpenInterest(ctx context.Context, k ...key.PairAsset) ([]futures.OpenInterest, error) {
for i := range k {
if k[i].Asset == asset.Spot || k[i].Asset == asset.Index {
// 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())
}
}
if len(k) != 1 {
activeInstruments, err := b.GetActiveAndIndexInstruments(ctx)
if err != nil {
return nil, err
}
resp := make([]futures.OpenInterest, 0, len(activeInstruments))
for i := range activeInstruments {
for _, a := range b.CurrencyPairs.GetAssetTypes(true) {
var symbol currency.Pair
var enabled bool
symbol, enabled, err = b.MatchSymbolCheckEnabled(activeInstruments[i].Symbol, a, 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) && k[j].Asset == a {
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: a,
},
OpenInterest: activeInstruments[i].OpenInterest,
})
}
}
return resp, nil
}
_, isEnabled, err := b.MatchSymbolCheckEnabled(k[0].Pair().String(), k[0].Asset, false)
if err != nil && !errors.Is(err, currency.ErrPairNotFound) {
return nil, err
}
if !isEnabled {
return nil, fmt.Errorf("%w %v %v", currency.ErrPairNotEnabled, k[0].Asset, k[0].Pair())
}
symbolStr, err := b.FormatSymbol(k[0].Pair(), k[0].Asset)
if err != nil {
return nil, err
}
instrument, err := b.GetInstrument(ctx, &GenericRequestParams{Symbol: symbolStr})
if err != nil {
return nil, err
}
if len(instrument) != 1 {
return nil, fmt.Errorf("%w %v", currency.ErrPairNotFound, k[0].Pair())
}
resp := make([]futures.OpenInterest, 1)
resp[0] = futures.OpenInterest{
Key: key.ExchangePairAsset{
Exchange: b.Name,
Base: k[0].Base,
Quote: k[0].Quote,
Asset: k[0].Asset,
},
OpenInterest: instrument[0].OpenInterest,
}
return resp, nil
}