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-05 18:33:56 +02:00
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type OrderResult struct {
|
|
|
|
OrderId string `json:"orderId"`
|
|
|
|
OrderLinkId string `json:"orderLinkId"`
|
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
type Trade struct {
|
|
|
|
c *Client
|
|
|
|
params map[string]interface{}
|
|
|
|
}
|
|
|
|
|
2023-11-05 18:33:56 +02:00
|
|
|
type Order struct {
|
|
|
|
c *Client
|
|
|
|
category string
|
|
|
|
symbol string
|
|
|
|
isLeverage *int
|
|
|
|
side string
|
|
|
|
orderType string
|
|
|
|
qty string
|
|
|
|
price *string
|
|
|
|
triggerDirection *int
|
|
|
|
orderFilter *string
|
|
|
|
triggerPrice *string
|
|
|
|
triggerBy *string
|
|
|
|
orderIv *string
|
|
|
|
timeInForce *string
|
|
|
|
positionIdx *int
|
|
|
|
orderLinkId *string
|
|
|
|
takeProfit *string
|
|
|
|
stopLoss *string
|
|
|
|
tpTriggerBy *string
|
|
|
|
slTriggerBy *string
|
|
|
|
reduceOnly *bool
|
|
|
|
closeOnTrigger *bool
|
|
|
|
smpType *string
|
|
|
|
mmp *bool
|
|
|
|
tpslMode *string
|
|
|
|
tpLimitPrice *string
|
|
|
|
slLimitPrice *string
|
|
|
|
tpOrderType *string
|
|
|
|
slOrderType *string
|
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TimeInForce(tif string) *Order {
|
|
|
|
order.timeInForce = &tif
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) IsLeverage(isLeverage int) *Order {
|
|
|
|
order.isLeverage = &isLeverage
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TriggerPrice(triggerPrice string) *Order {
|
|
|
|
order.triggerPrice = &triggerPrice
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) OrderLinkId(orderLinkId string) *Order {
|
|
|
|
order.orderLinkId = &orderLinkId
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) Price(price string) *Order {
|
|
|
|
order.price = &price
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TriggerDirection(direction int) *Order {
|
|
|
|
order.triggerDirection = &direction
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) OrderFilter(filter string) *Order {
|
|
|
|
order.orderFilter = &filter
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TriggerBy(triggerBy string) *Order {
|
|
|
|
order.triggerBy = &triggerBy
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) OrderIv(iv string) *Order {
|
|
|
|
order.orderIv = &iv
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) PositionIdx(idx int) *Order {
|
|
|
|
order.positionIdx = &idx
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TakeProfit(profit string) *Order {
|
|
|
|
order.takeProfit = &profit
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) StopLoss(loss string) *Order {
|
|
|
|
order.stopLoss = &loss
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TpTriggerBy(triggerBy string) *Order {
|
|
|
|
order.tpTriggerBy = &triggerBy
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) SlTriggerBy(triggerBy string) *Order {
|
|
|
|
order.slTriggerBy = &triggerBy
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) ReduceOnly(reduce bool) *Order {
|
|
|
|
order.reduceOnly = &reduce
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) CloseOnTrigger(close bool) *Order {
|
|
|
|
order.closeOnTrigger = &close
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) SmpType(smp string) *Order {
|
|
|
|
order.smpType = &smp
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) Mmp(mmp bool) *Order {
|
|
|
|
order.mmp = &mmp
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TpslMode(mode string) *Order {
|
|
|
|
order.tpslMode = &mode
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TpLimitPrice(price string) *Order {
|
|
|
|
order.tpLimitPrice = &price
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) SlLimitPrice(price string) *Order {
|
|
|
|
order.slLimitPrice = &price
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) TpOrderType(orderType string) *Order {
|
|
|
|
order.tpOrderType = &orderType
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) SlOrderType(orderType string) *Order {
|
|
|
|
order.slOrderType = &orderType
|
|
|
|
return order
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 23:35:56 +02:00
|
|
|
func (order *Order) Do(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
|
2023-11-05 18:33:56 +02:00
|
|
|
r := &request{
|
|
|
|
method: http.MethodPost,
|
|
|
|
endpoint: "/v5/order/create",
|
|
|
|
secType: secTypeSigned,
|
|
|
|
}
|
|
|
|
m := params{
|
2023-11-05 23:35:56 +02:00
|
|
|
"category": order.category,
|
|
|
|
"symbol": order.symbol,
|
|
|
|
"side": order.side,
|
|
|
|
"orderType": order.orderType,
|
|
|
|
"qty": order.qty,
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.price != nil {
|
|
|
|
m["price"] = *order.price
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.triggerDirection != nil {
|
|
|
|
m["triggerDirection"] = *order.triggerDirection
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.orderFilter != nil {
|
|
|
|
m["orderFilter"] = *order.orderFilter
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.triggerPrice != nil {
|
|
|
|
m["triggerPrice"] = *order.triggerPrice
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.triggerBy != nil {
|
|
|
|
m["triggerBy"] = *order.triggerBy
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.orderIv != nil {
|
|
|
|
m["orderIv"] = *order.orderIv
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.timeInForce != nil {
|
|
|
|
m["timeInForce"] = *order.timeInForce
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.positionIdx != nil {
|
|
|
|
m["positionIdx"] = *order.positionIdx
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.orderLinkId != nil {
|
|
|
|
m["orderLinkId"] = *order.orderLinkId
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.takeProfit != nil {
|
|
|
|
m["takeProfit"] = *order.takeProfit
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.stopLoss != nil {
|
|
|
|
m["stopLoss"] = *order.stopLoss
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.tpTriggerBy != nil {
|
|
|
|
m["tpTriggerBy"] = *order.tpTriggerBy
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.slTriggerBy != nil {
|
|
|
|
m["slTriggerBy"] = *order.slTriggerBy
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.reduceOnly != nil {
|
|
|
|
m["reduceOnly"] = *order.reduceOnly
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.closeOnTrigger != nil {
|
|
|
|
m["closeOnTrigger"] = *order.closeOnTrigger
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.smpType != nil {
|
|
|
|
m["smpType"] = *order.smpType
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.mmp != nil {
|
|
|
|
m["mmp"] = *order.mmp
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.tpslMode != nil {
|
|
|
|
m["tpslMode"] = *order.tpslMode
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.tpLimitPrice != nil {
|
|
|
|
m["tpLimitPrice"] = *order.tpLimitPrice
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.slLimitPrice != nil {
|
|
|
|
m["slLimitPrice"] = *order.slLimitPrice
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.tpOrderType != nil {
|
|
|
|
m["tpOrderType"] = *order.tpOrderType
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
if order.slOrderType != nil {
|
|
|
|
m["slOrderType"] = *order.slOrderType
|
2023-11-05 18:33:56 +02:00
|
|
|
}
|
|
|
|
r.setParams(m)
|
2023-11-05 23:35:56 +02:00
|
|
|
data, err := order.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 *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)
|
2023-11-05 18:33:56 +02:00
|
|
|
data, err := s.c.callAPI(ctx, r, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-05 23:35:56 +02:00
|
|
|
res = new(ServerResponse)
|
2023-11-05 18:33:56 +02:00
|
|
|
err = json.Unmarshal(data, res)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|