diff --git a/examples/okex-book/main.go b/examples/okex-book/main.go index 9d846b065..69f73f261 100644 --- a/examples/okex-book/main.go +++ b/examples/okex-book/main.go @@ -54,7 +54,12 @@ var rootCmd = &cobra.Command{ } log.Infof("instruments: %+v", instruments) - return nil + + fundingRate, err := client.PublicDataService.NewGetFundingRate().InstrumentID("BTC-USDT-SWAP").Do(ctx) + if err != nil { + return err + } + log.Infof("funding rate: %+v", fundingRate) log.Infof("ACCOUNT BALANCES:") balanceSummaries, err := client.AccountBalances() diff --git a/pkg/exchange/okex/okexapi/public.go b/pkg/exchange/okex/okexapi/public.go index e158d6c32..b877fe637 100644 --- a/pkg/exchange/okex/okexapi/public.go +++ b/pkg/exchange/okex/okexapi/public.go @@ -6,6 +6,7 @@ import ( "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" + "github.com/pkg/errors" ) type PublicDataService struct { @@ -18,6 +19,62 @@ func (s *PublicDataService) NewGetInstrumentsRequest() *GetInstrumentsRequest { } } +func (s *PublicDataService) NewGetFundingRate() *GetFundingRateRequest { + return &GetFundingRateRequest{ + client: s.client, + } +} + +type FundingRate struct { + InstrumentType string `json:"instType"` + InstrumentID string `json:"instId"` + FundingRate fixedpoint.Value `json:"fundingRate"` + NextFundingRate fixedpoint.Value `json:"nextFundingRate"` + FundingTime types.MillisecondTimestamp `json:"fundingTime"` +} + +type GetFundingRateRequest struct { + client *RestClient + + instId string +} + +func (r *GetFundingRateRequest) InstrumentID(instId string) *GetFundingRateRequest { + r.instId = instId + return r +} + +func (r *GetFundingRateRequest) Do(ctx context.Context) (*FundingRate, error) { + // SPOT, SWAP, FUTURES, OPTION + var params = url.Values{} + params.Add("instId", string(r.instId)) + + req, err := r.client.newRequest("GET", "/api/v5/public/funding-rate", params, nil) + if err != nil { + return nil, err + } + + response, err := r.client.sendRequest(req) + if err != nil { + return nil, err + } + + var apiResponse struct { + Code string `json:"code"` + Message string `json:"msg"` + Data []FundingRate `json:"data"` + } + if err := response.DecodeJSON(&apiResponse); err != nil { + return nil, err + } + + if len(apiResponse.Data) == 0 { + return nil, errors.New("empty funding rate data") + } + + return &apiResponse.Data[0], nil +} + type Instrument struct { InstrumentType string `json:"instType"` InstrumentID string `json:"instId"`