market endpoints
This commit is contained in:
parent
8695a773c9
commit
19e79e0676
|
@ -67,6 +67,13 @@ func (c *Client) debug(format string, v ...interface{}) {
|
|||
}
|
||||
}
|
||||
|
||||
func GetCurrentTime() int64 {
|
||||
now := time.Now()
|
||||
unixNano := now.UnixNano()
|
||||
timeStamp := unixNano / int64(time.Millisecond)
|
||||
return timeStamp
|
||||
}
|
||||
|
||||
// NewBybitHttpClient NewClient Create client function for initialising new Bybit client
|
||||
func NewBybitHttpClient(apiKey string, APISecret string, options ...ClientOption) *Client {
|
||||
c := &Client{
|
||||
|
@ -109,9 +116,7 @@ func (c *Client) parseRequest(r *request, opts ...RequestOption) (err error) {
|
|||
header.Set("User-Agent", fmt.Sprintf("%s/%s", Name, Version))
|
||||
|
||||
if r.secType == secTypeSigned {
|
||||
now := time.Now()
|
||||
unixNano := now.UnixNano()
|
||||
timeStamp := unixNano / 1000000
|
||||
timeStamp := GetCurrentTime()
|
||||
header.Set(signTypeKey, "2")
|
||||
header.Set(apiRequestKey, c.APIKey)
|
||||
header.Set(timestampKey, strconv.FormatInt(timeStamp, 10))
|
||||
|
@ -188,11 +193,6 @@ func (c *Client) callAPI(ctx context.Context, r *request, opts ...RequestOption)
|
|||
return data, nil
|
||||
}
|
||||
|
||||
// NewServerTimeService Market Endpoints
|
||||
func (c *Client) NewServerTimeService() *ServerTime {
|
||||
return &ServerTime{c: c}
|
||||
}
|
||||
|
||||
// NewMarketKlineService Market Endpoints
|
||||
func (c *Client) NewMarketKlineService(klineType, category, symbol, interval string) *Klines {
|
||||
return &Klines{
|
||||
|
@ -204,6 +204,27 @@ func (c *Client) NewMarketKlineService(klineType, category, symbol, interval str
|
|||
}
|
||||
}
|
||||
|
||||
func (c *Client) NewMarketKLinesService(klineType string, params map[string]interface{}) *MarketClient {
|
||||
return &MarketClient{
|
||||
c: c,
|
||||
klineType: klineType,
|
||||
params: params,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) NewMarketInfoServiceNoParams() *MarketClient {
|
||||
return &MarketClient{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) NewMarketInfoService(params map[string]interface{}) *MarketClient {
|
||||
return &MarketClient{
|
||||
c: c,
|
||||
params: params,
|
||||
}
|
||||
}
|
||||
|
||||
// NewPlaceOrderService Trade Endpoints
|
||||
func (c *Client) NewPlaceOrderService(category, symbol, side, orderType, qty string) *Order {
|
||||
return &Order{
|
||||
|
@ -215,3 +236,10 @@ func (c *Client) NewPlaceOrderService(category, symbol, side, orderType, qty str
|
|||
qty: qty,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) NewPlaceTradeService(params map[string]interface{}) *Trade {
|
||||
return &Trade{
|
||||
c: c,
|
||||
params: params,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
bybit "github.com/wuhewuhe/bybit.go.api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
PlaceTrade()
|
||||
}
|
||||
|
||||
func PlaceTrade() {
|
||||
client := bybit.NewBybitHttpClient("8wYkmpLsMg10eNQyPm", "Ouxc34myDnXvei54XsBZgoQzfGxO4bkr2Zsj", bybit.WithBaseURL(bybit.TESTNET))
|
||||
params := map[string]interface{}{"category": "linear", "symbol": "BTCUSDT", "side": "Buy", "positionIdx": 0, "orderType": "Limit", "qty": "0.001", "price": "10000", "timeInForce": "GTC"}
|
||||
orderResult, err := client.NewPlaceTradeService(params).Do(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(bybit.PrettyPrint(orderResult))
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
bybit "github.com/wuhewuhe/bybit.go.api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
InstrumentInfo()
|
||||
}
|
||||
|
||||
func InstrumentInfo() {
|
||||
|
||||
client := bybit.NewBybitHttpClient("", "")
|
||||
|
||||
// NewServerTimeService
|
||||
params := map[string]interface{}{"category": "linear", "symbol": "BTCUSDT", "status": "Trading"}
|
||||
marketKline, err := client.NewMarketInfoService(params).GetInstrumentInfo(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(bybit.PrettyPrint(marketKline))
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
bybit "github.com/wuhewuhe/bybit.go.api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
IndexKline()
|
||||
}
|
||||
|
||||
func IndexKline() {
|
||||
|
||||
client := bybit.NewBybitHttpClient("", "")
|
||||
|
||||
// NewServerTimeService
|
||||
params := map[string]interface{}{"category": "linear", "symbol": "BTCUSDT", "interval": "1", "Limit": 2}
|
||||
marketKline, err := client.NewMarketKLinesService("index-price-kline", params).GetMarketKline(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Println(bybit.PrettyPrint(marketKline))
|
||||
}
|
|
@ -15,7 +15,7 @@ func ServerTime() {
|
|||
client := bybit.NewBybitHttpClient("", "")
|
||||
|
||||
// NewServerTimeService
|
||||
serverTime, err := client.NewServerTimeService().Do(context.Background())
|
||||
serverTime, err := client.NewMarketInfoServiceNoParams().GetServerTime(context.Background())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
bybit "bybit.go.api"
|
||||
"fmt"
|
||||
bybit "github.com/wuhewuhe/bybit.go.api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
bybit "bybit.go.api"
|
||||
"fmt"
|
||||
bybit "github.com/wuhewuhe/bybit.go.api"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
|
@ -0,0 +1,23 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func ValidateParams(params map[string]interface{}) error {
|
||||
seenKeys := make(map[string]bool)
|
||||
|
||||
for key, value := range params {
|
||||
if key == "" {
|
||||
return fmt.Errorf("empty key found in parameters")
|
||||
}
|
||||
if seenKeys[key] {
|
||||
return fmt.Errorf("duplicate key found in parameters: %s", key)
|
||||
}
|
||||
if value == nil {
|
||||
return fmt.Errorf("parameter for key '%s' is nil", key)
|
||||
}
|
||||
seenKeys[key] = true
|
||||
}
|
||||
return nil
|
||||
}
|
321
market.go
321
market.go
|
@ -3,21 +3,17 @@ package bybit_connector
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/wuhewuhe/bybit.go.api/handlers"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type ServerTimeResult struct {
|
||||
TimeSecond string `json:"timeSecond"`
|
||||
TimeNano string `json:"timeNano"`
|
||||
}
|
||||
|
||||
// ServerTime Binance Check Server Time endpoint (GET /v5/market/time)
|
||||
type ServerTime struct {
|
||||
type MarketClient struct {
|
||||
c *Client
|
||||
klineType string
|
||||
params map[string]interface{}
|
||||
}
|
||||
|
||||
// Do Send the request
|
||||
func (s *ServerTime) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
||||
func (s *MarketClient) GetServerTime(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
||||
r := &request{
|
||||
method: http.MethodGet,
|
||||
endpoint: "/v5/market/time",
|
||||
|
@ -35,71 +31,258 @@ func (s *ServerTime) Do(ctx context.Context, opts ...RequestOption) (res *Server
|
|||
return res, nil
|
||||
}
|
||||
|
||||
type MarketKlineCandle struct {
|
||||
StartTime string `json:"startTime"`
|
||||
OpenPrice string `json:"openPrice"`
|
||||
HighPrice string `json:"highPrice"`
|
||||
LowPrice string `json:"lowPrice"`
|
||||
ClosePrice string `json:"closePrice"`
|
||||
Volume string `json:"volume"`
|
||||
Turnover string `json:"turnover"`
|
||||
}
|
||||
|
||||
type MarketKlineResponse struct {
|
||||
Category string `json:"category"`
|
||||
Symbol string `json:"symbol"`
|
||||
List []MarketKlineCandle `json:"list"`
|
||||
}
|
||||
|
||||
// Klines Market Kline (GET /v5/market/kline)
|
||||
type Klines struct {
|
||||
c *Client
|
||||
klineType string
|
||||
category string
|
||||
symbol string
|
||||
interval string
|
||||
limit *int
|
||||
start *uint64
|
||||
end *uint64
|
||||
}
|
||||
|
||||
// Limit set limit
|
||||
func (s *Klines) Limit(limit int) *Klines {
|
||||
s.limit = &limit
|
||||
return s
|
||||
}
|
||||
|
||||
// Start set startTime
|
||||
func (s *Klines) Start(startTime uint64) *Klines {
|
||||
s.start = &startTime
|
||||
return s
|
||||
}
|
||||
|
||||
// End set endTime
|
||||
func (s *Klines) End(endTime uint64) *Klines {
|
||||
s.end = &endTime
|
||||
return s
|
||||
}
|
||||
|
||||
// Do Send the request
|
||||
func (s *Klines) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
||||
func (s *MarketClient) GetMarketKline(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/" + s.klineType,
|
||||
secType: secTypeNone,
|
||||
}
|
||||
r.setParam("category", s.category)
|
||||
r.setParam("symbol", s.symbol)
|
||||
r.setParam("interval", s.interval)
|
||||
if s.limit != nil {
|
||||
r.setParam("limit", *s.limit)
|
||||
}
|
||||
if s.start != nil {
|
||||
r.setParam("start", *s.start)
|
||||
}
|
||||
if s.end != nil {
|
||||
r.setParam("end", *s.end)
|
||||
}
|
||||
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) 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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (s *MarketClient) GetHistoricalVolatility(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/historical-volatility",
|
||||
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) 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,
|
||||
}
|
||||
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) 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,
|
||||
}
|
||||
r.setParams(s.params)
|
||||
data, err := s.c.callAPI(ctx, r, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package bybit_connector
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// Klines Market Kline (GET /v5/market/kline)
|
||||
type Klines struct {
|
||||
c *Client
|
||||
klineType string
|
||||
category string
|
||||
symbol string
|
||||
interval string
|
||||
limit *int
|
||||
start *uint64
|
||||
end *uint64
|
||||
}
|
||||
|
||||
// Limit set limit
|
||||
func (s *Klines) Limit(limit int) *Klines {
|
||||
s.limit = &limit
|
||||
return s
|
||||
}
|
||||
|
||||
// Start set startTime
|
||||
func (s *Klines) Start(startTime uint64) *Klines {
|
||||
s.start = &startTime
|
||||
return s
|
||||
}
|
||||
|
||||
// End set endTime
|
||||
func (s *Klines) End(endTime uint64) *Klines {
|
||||
s.end = &endTime
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Klines) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
||||
r := &request{
|
||||
method: http.MethodGet,
|
||||
endpoint: "/v5/market/" + s.klineType,
|
||||
secType: secTypeNone,
|
||||
}
|
||||
r.setParam("category", s.category)
|
||||
r.setParam("symbol", s.symbol)
|
||||
r.setParam("interval", s.interval)
|
||||
if s.limit != nil {
|
||||
r.setParam("limit", *s.limit)
|
||||
}
|
||||
if s.start != nil {
|
||||
r.setParam("start", *s.start)
|
||||
}
|
||||
if s.end != nil {
|
||||
r.setParam("end", *s.end)
|
||||
}
|
||||
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
|
||||
}
|
|
@ -0,0 +1,197 @@
|
|||
package models
|
||||
|
||||
type ServerTimeResult struct {
|
||||
TimeSecond string `json:"timeSecond"`
|
||||
TimeNano string `json:"timeNano"`
|
||||
}
|
||||
|
||||
type MarketKlineCandle struct {
|
||||
StartTime string `json:"startTime"`
|
||||
OpenPrice string `json:"openPrice"`
|
||||
HighPrice string `json:"highPrice"`
|
||||
LowPrice string `json:"lowPrice"`
|
||||
ClosePrice string `json:"closePrice"`
|
||||
Volume string `json:"volume"`
|
||||
Turnover string `json:"turnover"`
|
||||
}
|
||||
|
||||
type MarketKlineResponse struct {
|
||||
Category string `json:"category"`
|
||||
Symbol string `json:"symbol"`
|
||||
List []MarketKlineCandle `json:"list"`
|
||||
}
|
||||
|
||||
type InstrumentInfo struct {
|
||||
Category string `json:"category"`
|
||||
NextPageCursor string `json:"nextPageCursor"`
|
||||
List []Instrument `json:"list"`
|
||||
}
|
||||
|
||||
type Instrument struct {
|
||||
Symbol string `json:"symbol"`
|
||||
ContractType string `json:"contractType"`
|
||||
Status string `json:"status"`
|
||||
BaseCoin string `json:"baseCoin"`
|
||||
QuoteCoin string `json:"quoteCoin"`
|
||||
LaunchTime string `json:"launchTime"`
|
||||
DeliveryTime string `json:"deliveryTime"`
|
||||
DeliveryFeeRate string `json:"deliveryFeeRate"`
|
||||
PriceScale string `json:"priceScale"`
|
||||
LeverageFilter LeverageFilter `json:"leverageFilter"`
|
||||
PriceFilter PriceFilter `json:"priceFilter"`
|
||||
LotSizeFilter LotSizeFilter `json:"lotSizeFilter"`
|
||||
UnifiedMarginTrade bool `json:"unifiedMarginTrade"`
|
||||
FundingInterval int `json:"fundingInterval"`
|
||||
SettleCoin string `json:"settleCoin"`
|
||||
CopyTrading string `json:"copyTrading"`
|
||||
}
|
||||
|
||||
type LeverageFilter struct {
|
||||
MinLeverage string `json:"minLeverage"`
|
||||
MaxLeverage string `json:"maxLeverage"`
|
||||
LeverageStep string `json:"leverageStep"`
|
||||
}
|
||||
|
||||
type PriceFilter struct {
|
||||
MinPrice string `json:"minPrice"`
|
||||
MaxPrice string `json:"maxPrice"`
|
||||
TickSize string `json:"tickSize"`
|
||||
}
|
||||
|
||||
type LotSizeFilter struct {
|
||||
MaxOrderQty string `json:"maxOrderQty"`
|
||||
MinOrderQty string `json:"minOrderQty"`
|
||||
QtyStep string `json:"qtyStep"`
|
||||
PostOnlyMaxOrderQty string `json:"postOnlyMaxOrderQty"`
|
||||
}
|
||||
|
||||
type OrderBookEntry struct {
|
||||
Price string `json:"0"`
|
||||
Size string `json:"1"`
|
||||
}
|
||||
|
||||
type OrderBookInfo struct {
|
||||
Symbol string `json:"s"`
|
||||
Bids []OrderBookEntry `json:"b"`
|
||||
Asks []OrderBookEntry `json:"a"`
|
||||
Timestamp int64 `json:"ts"`
|
||||
UpdateID int64 `json:"u"`
|
||||
}
|
||||
|
||||
type TickerInfo struct {
|
||||
Symbol string `json:"symbol"`
|
||||
LastPrice string `json:"lastPrice"`
|
||||
IndexPrice string `json:"indexPrice"`
|
||||
MarkPrice string `json:"markPrice"`
|
||||
PrevPrice24h string `json:"prevPrice24h"`
|
||||
Price24hPcnt string `json:"price24hPcnt"`
|
||||
HighPrice24h string `json:"highPrice24h"`
|
||||
LowPrice24h string `json:"lowPrice24h"`
|
||||
PrevPrice1h string `json:"prevPrice1h"`
|
||||
OpenInterest string `json:"openInterest"`
|
||||
OpenInterestValue string `json:"openInterestValue"`
|
||||
Turnover24h string `json:"turnover24h"`
|
||||
Volume24h string `json:"volume24h"`
|
||||
FundingRate string `json:"fundingRate"`
|
||||
NextFundingTime string `json:"nextFundingTime"`
|
||||
PredictedDeliveryPrice string `json:"predictedDeliveryPrice"`
|
||||
BasisRate string `json:"basisRate"`
|
||||
Basis string `json:"basis"`
|
||||
DeliveryFeeRate string `json:"deliveryFeeRate"`
|
||||
DeliveryTime string `json:"deliveryTime"`
|
||||
Ask1Size string `json:"ask1Size"`
|
||||
Bid1Price string `json:"bid1Price"`
|
||||
Ask1Price string `json:"ask1Price"`
|
||||
Bid1Size string `json:"bid1Size"`
|
||||
}
|
||||
|
||||
type MarketTickers struct {
|
||||
Category string `json:"category"`
|
||||
List []TickerInfo `json:"list"`
|
||||
}
|
||||
|
||||
type FundingRateInfo struct {
|
||||
Symbol string `json:"symbol"`
|
||||
FundingRate string `json:"fundingRate"`
|
||||
FundingRateTimestamp string `json:"fundingRateTimestamp"`
|
||||
}
|
||||
|
||||
type FundingRate struct {
|
||||
Category string `json:"category"`
|
||||
List []FundingRateInfo `json:"list"`
|
||||
}
|
||||
|
||||
type TradeInfo struct {
|
||||
ExecId string `json:"execId"`
|
||||
Symbol string `json:"symbol"`
|
||||
Price string `json:"price"`
|
||||
Size string `json:"size"`
|
||||
Side string `json:"side"`
|
||||
Time string `json:"time"`
|
||||
IsBlockTrade bool `json:"isBlockTrade"`
|
||||
}
|
||||
|
||||
type PublicRecentTradeHistory struct {
|
||||
Category string `json:"category"`
|
||||
List []TradeInfo `json:"list"`
|
||||
}
|
||||
|
||||
type VolatilityData struct {
|
||||
Period int `json:"period"`
|
||||
Value string `json:"value"`
|
||||
Time string `json:"time"`
|
||||
}
|
||||
|
||||
type HistoricalVolatilityInfo struct {
|
||||
Category string `json:"category"`
|
||||
List []VolatilityData `json:"list"`
|
||||
}
|
||||
|
||||
type InsuranceData struct {
|
||||
Coin string `json:"coin"`
|
||||
Balance string `json:"balance"`
|
||||
Value string `json:"value"`
|
||||
}
|
||||
|
||||
type MarketInsuranceInfo struct {
|
||||
UpdatedTime string `json:"updatedTime"`
|
||||
List []InsuranceData `json:"list"`
|
||||
}
|
||||
|
||||
type RiskLimitData struct {
|
||||
Id int `json:"id"`
|
||||
Symbol string `json:"symbol"`
|
||||
RiskLimitValue string `json:"riskLimitValue"`
|
||||
MaintenanceMargin float64 `json:"maintenanceMargin"`
|
||||
InitialMargin float64 `json:"initialMargin"`
|
||||
IsLowestRisk int `json:"isLowestRisk"`
|
||||
MaxLeverage string `json:"maxLeverage"`
|
||||
}
|
||||
|
||||
type MarketRiskLimitInfo struct {
|
||||
Category string `json:"category"`
|
||||
List []RiskLimitData `json:"list"`
|
||||
}
|
||||
|
||||
type DeliveryPriceData struct {
|
||||
Symbol string `json:"symbol"`
|
||||
DeliveryPrice string `json:"deliveryPrice"`
|
||||
DeliveryTime string `json:"deliveryTime"`
|
||||
}
|
||||
|
||||
type DeliveryPriceInfo struct {
|
||||
Category string `json:"category"`
|
||||
List []DeliveryPriceData `json:"list"`
|
||||
NextPageCursor string `json:"nextPageCursor"`
|
||||
}
|
||||
|
||||
type LongShortRatioData struct {
|
||||
Symbol string `json:"symbol"`
|
||||
BuyRatio string `json:"buyRatio"`
|
||||
SellRatio string `json:"sellRatio"`
|
||||
Timestamp string `json:"timestamp"`
|
||||
}
|
||||
|
||||
type MarketLongShortRatioInfo struct {
|
||||
List []LongShortRatioData `json:"list"`
|
||||
}
|
270
trade.go
270
trade.go
|
@ -3,6 +3,7 @@ package bybit_connector
|
|||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/wuhewuhe/bybit.go.api/handlers"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
|
@ -11,6 +12,11 @@ type OrderResult struct {
|
|||
OrderLinkId string `json:"orderLinkId"`
|
||||
}
|
||||
|
||||
type Trade struct {
|
||||
c *Client
|
||||
params map[string]interface{}
|
||||
}
|
||||
|
||||
type Order struct {
|
||||
c *Client
|
||||
category string
|
||||
|
@ -43,206 +49,228 @@ type Order struct {
|
|||
slOrderType *string
|
||||
}
|
||||
|
||||
func (o *Order) TimeInForce(tif string) *Order {
|
||||
o.timeInForce = &tif
|
||||
return o
|
||||
func (order *Order) TimeInForce(tif string) *Order {
|
||||
order.timeInForce = &tif
|
||||
return order
|
||||
}
|
||||
|
||||
func (s *Order) IsLeverage(isLeverage int) *Order {
|
||||
s.isLeverage = &isLeverage
|
||||
return s
|
||||
func (order *Order) IsLeverage(isLeverage int) *Order {
|
||||
order.isLeverage = &isLeverage
|
||||
return order
|
||||
}
|
||||
|
||||
func (s *Order) TriggerPrice(triggerPrice string) *Order {
|
||||
s.triggerPrice = &triggerPrice
|
||||
return s
|
||||
func (order *Order) TriggerPrice(triggerPrice string) *Order {
|
||||
order.triggerPrice = &triggerPrice
|
||||
return order
|
||||
}
|
||||
|
||||
func (s *Order) OrderLinkId(orderLinkId string) *Order {
|
||||
s.orderLinkId = &orderLinkId
|
||||
return s
|
||||
func (order *Order) OrderLinkId(orderLinkId string) *Order {
|
||||
order.orderLinkId = &orderLinkId
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) Price(price string) *Order {
|
||||
o.price = &price
|
||||
return o
|
||||
func (order *Order) Price(price string) *Order {
|
||||
order.price = &price
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TriggerDirection(direction int) *Order {
|
||||
o.triggerDirection = &direction
|
||||
return o
|
||||
func (order *Order) TriggerDirection(direction int) *Order {
|
||||
order.triggerDirection = &direction
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) OrderFilter(filter string) *Order {
|
||||
o.orderFilter = &filter
|
||||
return o
|
||||
func (order *Order) OrderFilter(filter string) *Order {
|
||||
order.orderFilter = &filter
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TriggerBy(triggerBy string) *Order {
|
||||
o.triggerBy = &triggerBy
|
||||
return o
|
||||
func (order *Order) TriggerBy(triggerBy string) *Order {
|
||||
order.triggerBy = &triggerBy
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) OrderIv(iv string) *Order {
|
||||
o.orderIv = &iv
|
||||
return o
|
||||
func (order *Order) OrderIv(iv string) *Order {
|
||||
order.orderIv = &iv
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) PositionIdx(idx int) *Order {
|
||||
o.positionIdx = &idx
|
||||
return o
|
||||
func (order *Order) PositionIdx(idx int) *Order {
|
||||
order.positionIdx = &idx
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TakeProfit(profit string) *Order {
|
||||
o.takeProfit = &profit
|
||||
return o
|
||||
func (order *Order) TakeProfit(profit string) *Order {
|
||||
order.takeProfit = &profit
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) StopLoss(loss string) *Order {
|
||||
o.stopLoss = &loss
|
||||
return o
|
||||
func (order *Order) StopLoss(loss string) *Order {
|
||||
order.stopLoss = &loss
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TpTriggerBy(triggerBy string) *Order {
|
||||
o.tpTriggerBy = &triggerBy
|
||||
return o
|
||||
func (order *Order) TpTriggerBy(triggerBy string) *Order {
|
||||
order.tpTriggerBy = &triggerBy
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) SlTriggerBy(triggerBy string) *Order {
|
||||
o.slTriggerBy = &triggerBy
|
||||
return o
|
||||
func (order *Order) SlTriggerBy(triggerBy string) *Order {
|
||||
order.slTriggerBy = &triggerBy
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) ReduceOnly(reduce bool) *Order {
|
||||
o.reduceOnly = &reduce
|
||||
return o
|
||||
func (order *Order) ReduceOnly(reduce bool) *Order {
|
||||
order.reduceOnly = &reduce
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) CloseOnTrigger(close bool) *Order {
|
||||
o.closeOnTrigger = &close
|
||||
return o
|
||||
func (order *Order) CloseOnTrigger(close bool) *Order {
|
||||
order.closeOnTrigger = &close
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) SmpType(smp string) *Order {
|
||||
o.smpType = &smp
|
||||
return o
|
||||
func (order *Order) SmpType(smp string) *Order {
|
||||
order.smpType = &smp
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) Mmp(mmp bool) *Order {
|
||||
o.mmp = &mmp
|
||||
return o
|
||||
func (order *Order) Mmp(mmp bool) *Order {
|
||||
order.mmp = &mmp
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TpslMode(mode string) *Order {
|
||||
o.tpslMode = &mode
|
||||
return o
|
||||
func (order *Order) TpslMode(mode string) *Order {
|
||||
order.tpslMode = &mode
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TpLimitPrice(price string) *Order {
|
||||
o.tpLimitPrice = &price
|
||||
return o
|
||||
func (order *Order) TpLimitPrice(price string) *Order {
|
||||
order.tpLimitPrice = &price
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) SlLimitPrice(price string) *Order {
|
||||
o.slLimitPrice = &price
|
||||
return o
|
||||
func (order *Order) SlLimitPrice(price string) *Order {
|
||||
order.slLimitPrice = &price
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) TpOrderType(orderType string) *Order {
|
||||
o.tpOrderType = &orderType
|
||||
return o
|
||||
func (order *Order) TpOrderType(orderType string) *Order {
|
||||
order.tpOrderType = &orderType
|
||||
return order
|
||||
}
|
||||
|
||||
func (o *Order) SlOrderType(orderType string) *Order {
|
||||
o.slOrderType = &orderType
|
||||
return o
|
||||
func (order *Order) SlOrderType(orderType string) *Order {
|
||||
order.slOrderType = &orderType
|
||||
return order
|
||||
}
|
||||
|
||||
func (s *Order) Do(ctx context.Context, opts ...RequestOption) (*ServerResponse, error) {
|
||||
func (order *Order) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
||||
r := &request{
|
||||
method: http.MethodPost,
|
||||
endpoint: "/v5/order/create",
|
||||
secType: secTypeSigned,
|
||||
}
|
||||
m := params{
|
||||
"category": s.category,
|
||||
"symbol": s.symbol,
|
||||
"side": s.side,
|
||||
"orderType": s.orderType,
|
||||
"qty": s.qty,
|
||||
"category": order.category,
|
||||
"symbol": order.symbol,
|
||||
"side": order.side,
|
||||
"orderType": order.orderType,
|
||||
"qty": order.qty,
|
||||
}
|
||||
if s.price != nil {
|
||||
m["price"] = *s.price
|
||||
if order.price != nil {
|
||||
m["price"] = *order.price
|
||||
}
|
||||
if s.triggerDirection != nil {
|
||||
m["triggerDirection"] = *s.triggerDirection
|
||||
if order.triggerDirection != nil {
|
||||
m["triggerDirection"] = *order.triggerDirection
|
||||
}
|
||||
if s.orderFilter != nil {
|
||||
m["orderFilter"] = *s.orderFilter
|
||||
if order.orderFilter != nil {
|
||||
m["orderFilter"] = *order.orderFilter
|
||||
}
|
||||
if s.triggerPrice != nil {
|
||||
m["triggerPrice"] = *s.triggerPrice
|
||||
if order.triggerPrice != nil {
|
||||
m["triggerPrice"] = *order.triggerPrice
|
||||
}
|
||||
if s.triggerBy != nil {
|
||||
m["triggerBy"] = *s.triggerBy
|
||||
if order.triggerBy != nil {
|
||||
m["triggerBy"] = *order.triggerBy
|
||||
}
|
||||
if s.orderIv != nil {
|
||||
m["orderIv"] = *s.orderIv
|
||||
if order.orderIv != nil {
|
||||
m["orderIv"] = *order.orderIv
|
||||
}
|
||||
if s.timeInForce != nil {
|
||||
m["timeInForce"] = *s.timeInForce
|
||||
if order.timeInForce != nil {
|
||||
m["timeInForce"] = *order.timeInForce
|
||||
}
|
||||
if s.positionIdx != nil {
|
||||
m["positionIdx"] = *s.positionIdx
|
||||
if order.positionIdx != nil {
|
||||
m["positionIdx"] = *order.positionIdx
|
||||
}
|
||||
if s.orderLinkId != nil {
|
||||
m["orderLinkId"] = *s.orderLinkId
|
||||
if order.orderLinkId != nil {
|
||||
m["orderLinkId"] = *order.orderLinkId
|
||||
}
|
||||
if s.takeProfit != nil {
|
||||
m["takeProfit"] = *s.takeProfit
|
||||
if order.takeProfit != nil {
|
||||
m["takeProfit"] = *order.takeProfit
|
||||
}
|
||||
if s.stopLoss != nil {
|
||||
m["stopLoss"] = *s.stopLoss
|
||||
if order.stopLoss != nil {
|
||||
m["stopLoss"] = *order.stopLoss
|
||||
}
|
||||
if s.tpTriggerBy != nil {
|
||||
m["tpTriggerBy"] = *s.tpTriggerBy
|
||||
if order.tpTriggerBy != nil {
|
||||
m["tpTriggerBy"] = *order.tpTriggerBy
|
||||
}
|
||||
if s.slTriggerBy != nil {
|
||||
m["slTriggerBy"] = *s.slTriggerBy
|
||||
if order.slTriggerBy != nil {
|
||||
m["slTriggerBy"] = *order.slTriggerBy
|
||||
}
|
||||
if s.reduceOnly != nil {
|
||||
m["reduceOnly"] = *s.reduceOnly
|
||||
if order.reduceOnly != nil {
|
||||
m["reduceOnly"] = *order.reduceOnly
|
||||
}
|
||||
if s.closeOnTrigger != nil {
|
||||
m["closeOnTrigger"] = *s.closeOnTrigger
|
||||
if order.closeOnTrigger != nil {
|
||||
m["closeOnTrigger"] = *order.closeOnTrigger
|
||||
}
|
||||
if s.smpType != nil {
|
||||
m["smpType"] = *s.smpType
|
||||
if order.smpType != nil {
|
||||
m["smpType"] = *order.smpType
|
||||
}
|
||||
if s.mmp != nil {
|
||||
m["mmp"] = *s.mmp
|
||||
if order.mmp != nil {
|
||||
m["mmp"] = *order.mmp
|
||||
}
|
||||
if s.tpslMode != nil {
|
||||
m["tpslMode"] = *s.tpslMode
|
||||
if order.tpslMode != nil {
|
||||
m["tpslMode"] = *order.tpslMode
|
||||
}
|
||||
if s.tpLimitPrice != nil {
|
||||
m["tpLimitPrice"] = *s.tpLimitPrice
|
||||
if order.tpLimitPrice != nil {
|
||||
m["tpLimitPrice"] = *order.tpLimitPrice
|
||||
}
|
||||
if s.slLimitPrice != nil {
|
||||
m["slLimitPrice"] = *s.slLimitPrice
|
||||
if order.slLimitPrice != nil {
|
||||
m["slLimitPrice"] = *order.slLimitPrice
|
||||
}
|
||||
if s.tpOrderType != nil {
|
||||
m["tpOrderType"] = *s.tpOrderType
|
||||
if order.tpOrderType != nil {
|
||||
m["tpOrderType"] = *order.tpOrderType
|
||||
}
|
||||
if s.slOrderType != nil {
|
||||
m["slOrderType"] = *s.slOrderType
|
||||
if order.slOrderType != nil {
|
||||
m["slOrderType"] = *order.slOrderType
|
||||
}
|
||||
r.setParams(m)
|
||||
data, err := s.c.callAPI(ctx, r, opts...)
|
||||
data, err := order.c.callAPI(ctx, r, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res := new(ServerResponse)
|
||||
res = new(ServerResponse)
|
||||
err = json.Unmarshal(data, res)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func (s *Trade) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
||||
if err = handlers.ValidateParams(s.params); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
r := &request{
|
||||
method: http.MethodPost,
|
||||
endpoint: "/v5/order/create",
|
||||
secType: secTypeSigned,
|
||||
}
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue