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

@@ -488,6 +488,30 @@ var futuresCommands = &cli.Command{
},
},
},
{
Name: "getopeninterest",
Aliases: []string{"goi", "oi"},
Usage: "gets the open interest for provided exchange asset pair, if asset pair is not present, return all available if supported",
ArgsUsage: "<exchange> <asset> <pair>",
Action: getOpenInterest,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "exchange",
Aliases: []string{"e"},
Usage: "the exchange to retrieve open interest from",
},
&cli.StringFlag{
Name: "asset",
Aliases: []string{"a"},
Usage: "optional - the asset type of the currency pair, must be a futures type",
},
&cli.StringFlag{
Name: "pair",
Aliases: []string{"p"},
Usage: "optional - the currency pair",
},
},
},
},
}
@@ -1611,3 +1635,78 @@ func setMarginType(c *cli.Context) error {
jsonOutput(result)
return nil
}
func getOpenInterest(c *cli.Context) error {
if c.NArg() == 0 && c.NumFlags() == 0 {
return cli.ShowSubcommandHelp(c)
}
var (
exchangeName, assetType, currencyPair string
err error
)
if c.IsSet("exchange") {
exchangeName = c.String("exchange")
} else {
exchangeName = c.Args().First()
}
if c.IsSet("asset") {
assetType = c.String("asset")
} else {
assetType = c.Args().Get(1)
}
if assetType != "" {
err = isFuturesAsset(assetType)
if err != nil {
return err
}
}
if c.IsSet("pair") {
currencyPair = c.String("pair")
} else {
currencyPair = c.Args().Get(2)
}
var pair currency.Pair
if currencyPair != "" {
if !validPair(currencyPair) {
return fmt.Errorf("%w currencypair:%v", errInvalidPair, currencyPair)
}
pair, err = currency.NewPairDelimiter(currencyPair, pairDelimiter)
if err != nil {
return err
}
}
data := make([]*gctrpc.OpenInterestDataRequest, 0, 1)
if !pair.IsEmpty() {
data = append(data, &gctrpc.OpenInterestDataRequest{
Asset: assetType,
Pair: &gctrpc.CurrencyPair{
Delimiter: pair.Delimiter,
Base: pair.Base.String(),
Quote: pair.Quote.String(),
},
})
}
conn, cancel, err := setupClient(c)
if err != nil {
return err
}
defer closeConn(conn, cancel)
client := gctrpc.NewGoCryptoTraderServiceClient(conn)
result, err := client.GetOpenInterest(c.Context,
&gctrpc.GetOpenInterestRequest{
Exchange: exchangeName,
Data: data,
})
if err != nil {
return err
}
jsonOutput(result)
return nil
}