2023-11-03 16:29:17 +02:00
|
|
|
package bybit_connector
|
2023-10-30 20:26:54 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-11-03 16:29:17 +02:00
|
|
|
"encoding/json"
|
2023-11-05 23:35:56 +02:00
|
|
|
"github.com/wuhewuhe/bybit.go.api/handlers"
|
2023-10-30 20:26:54 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
type MarketClient struct {
|
|
|
|
c *Client
|
|
|
|
klineType string
|
|
|
|
params map[string]interface{}
|
2023-11-03 16:29:17 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetServerTime(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/time",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-10-30 20:26:54 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetMarketKline(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-30 20:26:54 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
2023-11-05 23:35:56 +02:00
|
|
|
endpoint: "/v5/market/" + s.klineType,
|
2023-10-30 20:26:54 +02:00
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
r.setParams(s.params)
|
2023-11-03 16:29:17 +02:00
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-10-30 20:26:54 +02:00
|
|
|
}
|
2023-11-05 18:33:56 +02:00
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetInstrumentInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/instruments-info",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetOrderBookInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/orderbook",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetMarketTickers(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/tickers",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetFundingRates(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/tickers",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetPublicRecentTrades(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/recent-trade",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetOpenInterests(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/open-interest",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (s *MarketClient) GetHistoricalVolatility(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-05 18:33:56 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
2023-11-05 23:35:56 +02:00
|
|
|
endpoint: "/v5/market/historical-volatility",
|
2023-11-05 18:33:56 +02:00
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MarketClient) GetInsuranceInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/insurance",
|
|
|
|
secType: secTypeNone,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MarketClient) GetRiskLimit(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/risk-limit",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MarketClient) GetDeliveryPrice(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/delivery-price",
|
|
|
|
secType: secTypeNone,
|
|
|
|
}
|
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *MarketClient) GetMarketLSRatio(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/market/account-ratio",
|
|
|
|
secType: secTypeNone,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
r.setParams(s.params)
|
2023-11-05 18:33:56 +02:00
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|