bybit.go.api/trade.go

257 lines
5.9 KiB
Go
Raw Normal View History

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-05 23:35:56 +02:00
type Trade struct {
c *Client
params map[string]interface{}
}
2023-11-06 00:18:48 +02:00
func (s *Trade) PlaceOrder(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
}
return res, nil
2023-11-05 18:33:56 +02:00
}
2023-11-06 00:18:48 +02:00
func (s *Trade) AmendOrder(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/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:18:48 +02:00
func (s *Trade) CancelOrder(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/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:18:48 +02:00
func (s *Trade) GetOpenOrders(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{
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
}
func (s *Trade) GetOrderHistory(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
}
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
}
func (s *Trade) GetSpotBorrowQuota(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
}
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
}
func (s *Trade) CancelAllOrders(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
}
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
}
func (s *Trade) SetDisconnectCancelAll(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
}
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
}
func (s *Trade) PlaceBatchOrder(ctx context.Context, opts ...RequestOption) (res *models.BatchOrderServerResponse, err error) {
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
}
func (s *Trade) AmendBatchOrder(ctx context.Context, opts ...RequestOption) (res *models.BatchOrderServerResponse, err error) {
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:18:48 +02:00
func (s *Trade) 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
}