2023-11-03 16:29:17 +02:00
|
|
|
package bybit_connector
|
2023-11-05 18:33:56 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2023-11-05 23:35:56 +02:00
|
|
|
"github.com/wuhewuhe/bybit.go.api/handlers"
|
2023-11-06 00:18:48 +02:00
|
|
|
"github.com/wuhewuhe/bybit.go.api/models"
|
2023-11-05 18:33:56 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
type TradeClient struct {
|
2023-11-05 23:35:56 +02:00
|
|
|
c *Client
|
|
|
|
params map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) PlaceOrder(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) AmendOrder(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/amend",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) CancelOrder(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/cancel",
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return res, nil
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) GetOpenOrders(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-05 18:33:56 +02:00
|
|
|
r := &request{
|
2023-11-06 00:18:48 +02:00
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/order/realtime",
|
2023-11-05 18:33:56 +02:00
|
|
|
secType: secTypeSigned,
|
|
|
|
}
|
2023-11-06 00:18:48 +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-06 00:18:48 +02:00
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) GetOrderHistory(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/order/history",
|
|
|
|
secType: secTypeSigned,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +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-06 00:18:48 +02:00
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) GetSpotBorrowQuota(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodGet,
|
|
|
|
endpoint: "/v5/order/spot-borrow-check",
|
|
|
|
secType: secTypeSigned,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +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-06 00:18:48 +02:00
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) CancelAllOrders(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/cancel-all",
|
|
|
|
secType: secTypeSigned,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +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-06 00:18:48 +02:00
|
|
|
res = new(ServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) SetDisconnectCancelAll(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/disconnected-cancel-all",
|
|
|
|
secType: secTypeSigned,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +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
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) PlaceBatchOrder(ctx context.Context, opts ...RequestOption) (res *models.BatchOrderServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/create-batch",
|
|
|
|
secType: secTypeSigned,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +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-06 00:18:48 +02:00
|
|
|
res = new(models.BatchOrderServerResponse)
|
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) AmendBatchOrder(ctx context.Context, opts ...RequestOption) (res *models.BatchOrderServerResponse, err error) {
|
2023-11-06 00:18:48 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/amend-batch",
|
|
|
|
secType: secTypeSigned,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
r.setParams(s.params)
|
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
2023-11-05 23:35:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
res = new(models.BatchOrderServerResponse)
|
2023-11-05 23:35:56 +02:00
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2023-11-06 00:42:17 +02:00
|
|
|
func (s *TradeClient) CancelBatchOrder(ctx context.Context, opts ...RequestOption) (res *models.BatchOrderServerResponse, err error) {
|
2023-11-05 23:35:56 +02:00
|
|
|
if err = handlers.ValidateParams(s.params); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
2023-11-06 00:18:48 +02:00
|
|
|
endpoint: "/v5/order/cancel-batch",
|
2023-11-05 23:35:56 +02:00
|
|
|
secType: secTypeSigned,
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2023-11-06 00:18:48 +02:00
|
|
|
res = new(models.BatchOrderServerResponse)
|
2023-11-05 18:33:56 +02:00
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|