mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-16 23:16:48 +00:00
* few fixes and add ratelimiter * adds test * revert configtest.json changes * configtest updated * WIP: adds public endpoint support * WIP: adds public endpoint support * adds public endpoint support * WIP: adds auth. endpoint support * adds test for auth. endpoint * fixes * adds auth. endpoint support * WIP: ws support * WIP * WIP * WIP * WIP * WIP * WIP * WIP * Testing * Complete WS spot testing * adds support for ws events * minor change * WIP: adds REST support for CoinMarginedFutures * Fixes * WIP: adds REST support for CoinMarginedFutures * Fixes * improvement in SPOT REST * Typo fix * WIP: add REST support for CMF Account API * minor fixes * WIP: add support for CMF conditional orders and few minor fixes * complete support for CMF conditional orders * adds support for public CMF endpoint * adds support for CMF position API * Complete REST CMF support * WIP * Testing REST CMF support * Testing REST CMF support * Testing REST CMF support completed * WIP: add support for UMF * completed non-auth UMF * WIP: add support for REST Auth. UMF * WIP: add support for REST Auth. UMF and some improvements * WIP * WIP * WIP * completed REST UMF * renaming * adds REST support for futures * add testcases for UMF and some optimizations * add testcases for futures * Testing UMF, futures and its changes * Fixes * Fixes after testing * WIP * WIP * WIP * completed ws USDT futures support * WIP: ws support for futures * fixes in WS futures * fixes in WS support * roll back changes made for WS CMF, USDT and Futures * fixes * WIP * WIP * fixes * Steps for new PR * WIP * WIP * WIP * WIP * complete PR setup * fixes for successfully running tests * update in symbol for futures pair in test file * WIP * Fixes in test file and other minor fix * fix testdata/configtest.json * reset CONTRIBUTORS file * review changes * remove unwanted file * remove redundant code * improvisation * adds comment for exported functions * remove unwanted TODO and commented code * fix * improvisation * fix * defined errors * improvisation * improvisation * improvisation * updates test * adds comment for exported types * review changes * review changes * fix * fixes * Changes for making BYBIT compatible with existing code base * Test file changes * Changes for making BYBIT compatible with existing code base * Changes for making BYBIT compatible with existing code base * fix lint issues * fix * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * review changes * WIP * add test cases for new API's * minor improvements * add missing API and their tests * minor fixes * add bybitTime * add bybitTimeSec, bybitTimeMilliSec, bybitTimeNanoSec and necessary support * fix GetTradeHistory function * error handling * test fixes * add GetServerTime API * adds GetHistoricCandlesExtended and review changes * test fixes * minor fix * integrating CMF Bybit recent change log * minor fixes * adds extractCurrencyPair * minor fixes * minor fix * review changes * adds variable declaration of error * review commit * adds embeddable type in API response for all API and integrate it * fixes * adds authentication WS connection * review changes * review changes * compatible changes * adds asset to GetWithdrawalsHistory * adds asset_type in rpc.proto * adds asset argument in gctcli withdrawal request command * improve error handling in exchange API error * web socket fix * review changes * improvements * improvements * minor fix * review changes * fixing wrapper issues * fixes * fixes * review changes * add test cases * fix for GetActiveOrders * lint fixes * fixes in websocket * adds wrapper testcases * adds wrapper testcases * adds wrapper testcases * fixes * fix issue with GetHistoricCandlesExtended * fix merge issues * improving error reporting * adds wrapper testcases and a minor fix * gctrpc changes * adds test cases fixes in websocket * review changes for ws * review changes in WS * fix gctrpc * merge fixes * review changes * WIP * updates pair in configs * adds new asset USDCMarginedFutures * adds URL const for USDCMarginedFutures * adds API support * minor fixes * adds kline API * minor fix * adds API * adds API * adds API * WIP * WIP * WIP * adds support for USDC auth requests to SendAuthHTTPRequest * adds SendUSDCAuthHTTPRequest * run test and fix them * rollback support added for Auth. USDC request inside SendAuthHTTPRequest * adds API and test cases * adds API and test cases * adds APIs and test cases * adds APIs * adds rate limit for USDC * adds USDCMarginedFutures to wrapper * adds USDC testcases in wrapper and fix few issues * minor test fixes * minor test fixes * fix lint issues * WIP * Merge changes * minor fixes * remove "else" and optimize * review changes * review changes * review changes * fix lint issue * merge fix * fix test * fix templates and run them * changes after merge * review changes and improvements * code improvement * fixes with respect to changes in API response in documentation * fixed review change in test * adds check in CancelExistingOrder * update exchange template * review changes * adds GetDepositAddress API * WIP: adds GetOrderHistory * complete GetOrderHistory * fixes * adds test case * fixes and add WithdrawFund API * WIP * WIP * updating all SendAuthHTTPRequest call * adds WithdrawCryptocurrencyFunds * update test cases * fix lint issues * fixes after merge * adds GetAvailableTransferChains and few fixes * minor fix in GetDepositAddress * minor fix with WS ping/pong handling * add ping handler for WS Auth. * fix typo mistake * update doc
758 lines
25 KiB
Go
758 lines
25 KiB
Go
package bybit
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/url"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
)
|
|
|
|
const (
|
|
|
|
// auth endpoint
|
|
futuresCreateOrder = "/futures/private/order/create"
|
|
futuresGetActiveOrders = "/futures/private/order/list"
|
|
futuresCancelActiveOrder = "/futures/private/order/cancel"
|
|
futuresCancelAllActiveOrders = "/futures/private/order/cancelAll"
|
|
futuresReplaceActiveOrder = "/futures/private/order/replace"
|
|
futuresGetActiveRealtimeOrders = "/futures/private/order"
|
|
|
|
futuresCreateConditionalOrder = "/futures/private/stop-order/create"
|
|
futuresGetConditionalOrders = "/futures/private/stop-order/list"
|
|
futuresCancelConditionalOrder = "/futures/private/stop-order/cancel"
|
|
futuresCancelAllConditionalOrders = "/futures/private/stop-order/cancelAll"
|
|
futuresReplaceConditionalOrder = "/futures/private/stop-order/replace"
|
|
futuresGetConditionalRealtimeOrders = "/futures/private/stop-order"
|
|
|
|
futuresPosition = "/futures/private/position/list"
|
|
futuresUpdateMargin = "/futures/private/position/change-position-margin"
|
|
futuresSetTradingStop = "/futures/private/position/trading-stop"
|
|
futuresSetLeverage = "/futures/private/position/leverage/save"
|
|
futuresSwitchPositionMode = "/futures/private/position/switch-mode"
|
|
futuresSwitchPosition = "/futures/private/tpsl/switch-mode"
|
|
futuresSwitchMargin = "/futures/private/position/switch-isolated"
|
|
futuresGetTrades = "/futures/private/execution/list"
|
|
futuresGetClosedTrades = "/futures/private/trade/closed-pnl/list"
|
|
futuresSetRiskLimit = "/futures/private/position/risk-limit"
|
|
)
|
|
|
|
// CreateFuturesOrder sends a new futures order to the exchange
|
|
func (by *Bybit) CreateFuturesOrder(ctx context.Context, positionMode int64, symbol currency.Pair, side, orderType, timeInForce,
|
|
orderLinkID, takeProfitTriggerBy, stopLossTriggerBy string,
|
|
quantity, price, takeProfit, stopLoss float64, closeOnTrigger, reduceOnly bool) (FuturesOrderDataResp, error) {
|
|
resp := struct {
|
|
Result FuturesOrderDataResp `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
params := url.Values{}
|
|
if positionMode < 0 || positionMode > 2 {
|
|
return resp.Result, errInvalidPositionMode
|
|
}
|
|
params.Set("position_idx", strconv.FormatInt(positionMode, 10))
|
|
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
params.Set("side", side)
|
|
params.Set("order_type", orderType)
|
|
if quantity <= 0 {
|
|
return resp.Result, errInvalidQuantity
|
|
}
|
|
params.Set("qty", strconv.FormatFloat(quantity, 'f', -1, 64))
|
|
|
|
if price != 0 {
|
|
params.Set("price", strconv.FormatFloat(price, 'f', -1, 64))
|
|
}
|
|
if timeInForce == "" {
|
|
return resp.Result, errInvalidTimeInForce
|
|
}
|
|
params.Set("time_in_force", timeInForce)
|
|
|
|
if closeOnTrigger {
|
|
params.Set("close_on_trigger", "true")
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
if takeProfit != 0 {
|
|
params.Set("take_profit", strconv.FormatFloat(takeProfit, 'f', -1, 64))
|
|
}
|
|
if stopLoss != 0 {
|
|
params.Set("stop_loss", strconv.FormatFloat(stopLoss, 'f', -1, 64))
|
|
}
|
|
if takeProfitTriggerBy != "" {
|
|
params.Set("tp_trigger_by", takeProfitTriggerBy)
|
|
}
|
|
if stopLossTriggerBy != "" {
|
|
params.Set("sl_trigger_by", stopLossTriggerBy)
|
|
}
|
|
if reduceOnly {
|
|
params.Set("reduce_only", "true")
|
|
}
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresCreateOrder, params, nil, &resp, futuresCreateOrderRate)
|
|
}
|
|
|
|
// GetActiveFuturesOrders gets list of futures active orders
|
|
func (by *Bybit) GetActiveFuturesOrders(ctx context.Context, symbol currency.Pair, orderStatus, direction, cursor string, limit int64) ([]FuturesActiveOrder, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
Data []FuturesActiveOrder `json:"data"`
|
|
Cursor string `json:"cursor"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result.Data, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if orderStatus != "" {
|
|
params.Set("order_status", orderStatus)
|
|
}
|
|
if direction != "" {
|
|
params.Set("direction", direction)
|
|
}
|
|
if limit > 0 && limit <= 50 {
|
|
params.Set("limit", strconv.FormatInt(limit, 10))
|
|
}
|
|
if cursor != "" {
|
|
params.Set("cursor", cursor)
|
|
}
|
|
return resp.Result.Data, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetActiveOrders, params, nil, &resp, futuresGetActiveOrderRate)
|
|
}
|
|
|
|
// CancelActiveFuturesOrders cancels futures unfilled or partially filled orders
|
|
func (by *Bybit) CancelActiveFuturesOrders(ctx context.Context, symbol currency.Pair, orderID, orderLinkID string) (FuturesOrderCancelResp, error) {
|
|
resp := struct {
|
|
Result FuturesOrderCancelResp `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if orderID == "" && orderLinkID == "" {
|
|
return resp.Result, errOrderOrOrderLinkIDMissing
|
|
}
|
|
if orderID != "" {
|
|
params.Set("order_id", orderID)
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresCancelActiveOrder, params, nil, &resp, futuresCancelOrderRate)
|
|
}
|
|
|
|
// CancelAllActiveFuturesOrders cancels all futures unfilled or partially filled orders
|
|
func (by *Bybit) CancelAllActiveFuturesOrders(ctx context.Context, symbol currency.Pair) ([]FuturesCancelOrderData, error) {
|
|
resp := struct {
|
|
Result []FuturesCancelOrderData `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresCancelAllActiveOrders, params, nil, &resp, futuresCancelAllOrderRate)
|
|
}
|
|
|
|
// ReplaceActiveFuturesOrders modify unfilled or partially filled orders
|
|
func (by *Bybit) ReplaceActiveFuturesOrders(ctx context.Context, symbol currency.Pair, orderID, orderLinkID, takeProfitTriggerBy, stopLossTriggerBy string,
|
|
updatedQty, updatedPrice, takeProfitPrice, stopLossPrice float64) (string, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
OrderID string `json:"order_id"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if orderID == "" && orderLinkID == "" {
|
|
return "", errOrderOrOrderLinkIDMissing
|
|
}
|
|
if orderID != "" {
|
|
params.Set("order_id", orderID)
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
if updatedQty != 0 {
|
|
params.Set("p_r_qty", strconv.FormatFloat(updatedQty, 'f', -1, 64))
|
|
}
|
|
if updatedPrice != 0 {
|
|
params.Set("p_r_price", strconv.FormatFloat(updatedPrice, 'f', -1, 64))
|
|
}
|
|
if takeProfitPrice != 0 {
|
|
params.Set("take_profit", strconv.FormatFloat(takeProfitPrice, 'f', -1, 64))
|
|
}
|
|
if stopLossPrice != 0 {
|
|
params.Set("stop_loss", strconv.FormatFloat(stopLossPrice, 'f', -1, 64))
|
|
}
|
|
if takeProfitTriggerBy != "" {
|
|
params.Set("tp_trigger_by", takeProfitTriggerBy)
|
|
}
|
|
if stopLossTriggerBy != "" {
|
|
params.Set("sl_trigger_by", stopLossTriggerBy)
|
|
}
|
|
return resp.Result.OrderID, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresReplaceActiveOrder, params, nil, &resp, futuresReplaceOrderRate)
|
|
}
|
|
|
|
// GetActiveRealtimeOrders query real time order data
|
|
func (by *Bybit) GetActiveRealtimeOrders(ctx context.Context, symbol currency.Pair, orderID, orderLinkID string) ([]FuturesActiveRealtimeOrder, error) {
|
|
var data []FuturesActiveRealtimeOrder
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if orderID != "" {
|
|
params.Set("order_id", orderID)
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
|
|
if orderID == "" && orderLinkID == "" {
|
|
resp := struct {
|
|
Result []FuturesActiveRealtimeOrder `json:"result"`
|
|
Error
|
|
}{}
|
|
err = by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetActiveRealtimeOrders, params, nil, &resp, futuresGetActiveRealtimeOrderRate)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
data = append(data, resp.Result...)
|
|
} else {
|
|
resp := struct {
|
|
Result FuturesActiveRealtimeOrder `json:"result"`
|
|
Error
|
|
}{}
|
|
err = by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetActiveRealtimeOrders, params, nil, &resp, futuresGetActiveRealtimeOrderRate)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
data = append(data, resp.Result)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// CreateConditionalFuturesOrder sends a new conditional futures order to the exchange
|
|
func (by *Bybit) CreateConditionalFuturesOrder(ctx context.Context, positionMode int64, symbol currency.Pair, side, orderType, timeInForce,
|
|
orderLinkID, takeProfitTriggerBy, stopLossTriggerBy, triggerBy string,
|
|
quantity, price, takeProfit, stopLoss, basePrice, stopPrice float64, closeOnTrigger bool) (FuturesConditionalOrderResp, error) {
|
|
resp := struct {
|
|
Result FuturesConditionalOrderResp `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
if positionMode < 0 || positionMode > 2 {
|
|
return resp.Result, errInvalidPositionMode
|
|
}
|
|
params.Set("position_idx", strconv.FormatInt(positionMode, 10))
|
|
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
params.Set("side", side)
|
|
params.Set("order_type", orderType)
|
|
if quantity <= 0 {
|
|
return resp.Result, errInvalidQuantity
|
|
}
|
|
params.Set("qty", strconv.FormatFloat(quantity, 'f', -1, 64))
|
|
|
|
if price != 0 {
|
|
params.Set("price", strconv.FormatFloat(price, 'f', -1, 64))
|
|
}
|
|
if basePrice <= 0 {
|
|
return resp.Result, errInvalidBasePrice
|
|
}
|
|
params.Set("base_price", strconv.FormatFloat(basePrice, 'f', -1, 64))
|
|
|
|
if stopPrice <= 0 {
|
|
return resp.Result, errInvalidStopPrice
|
|
}
|
|
params.Set("stop_px", strconv.FormatFloat(stopPrice, 'f', -1, 64))
|
|
|
|
if timeInForce == "" {
|
|
return resp.Result, errInvalidTimeInForce
|
|
}
|
|
params.Set("time_in_force", timeInForce)
|
|
|
|
if triggerBy != "" {
|
|
params.Set("trigger_by", triggerBy)
|
|
}
|
|
if closeOnTrigger {
|
|
params.Set("close_on_trigger", "true")
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
if takeProfit != 0 {
|
|
params.Set("take_profit", strconv.FormatFloat(takeProfit, 'f', -1, 64))
|
|
}
|
|
if stopLoss != 0 {
|
|
params.Set("stop_loss", strconv.FormatFloat(stopLoss, 'f', -1, 64))
|
|
}
|
|
if takeProfitTriggerBy != "" {
|
|
params.Set("tp_trigger_by", takeProfitTriggerBy)
|
|
}
|
|
if stopLossTriggerBy != "" {
|
|
params.Set("sl_trigger_by", stopLossTriggerBy)
|
|
}
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresCreateConditionalOrder, params, nil, &resp, futuresCreateConditionalOrderRate)
|
|
}
|
|
|
|
// GetConditionalFuturesOrders gets list of futures conditional orders
|
|
func (by *Bybit) GetConditionalFuturesOrders(ctx context.Context, symbol currency.Pair, stopOrderStatus, direction, cursor string, limit int64) ([]FuturesConditionalOrders, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
Result []FuturesConditionalOrders `json:"data"`
|
|
Cursor string `json:"cursor"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if stopOrderStatus != "" {
|
|
params.Set("stop_order_status", stopOrderStatus)
|
|
}
|
|
if direction != "" {
|
|
params.Set("direction", direction)
|
|
}
|
|
if limit > 0 && limit <= 50 {
|
|
params.Set("limit", strconv.FormatInt(limit, 10))
|
|
}
|
|
if cursor != "" {
|
|
params.Set("cursor", cursor)
|
|
}
|
|
return resp.Result.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetConditionalOrders, params, nil, &resp, futuresGetConditionalOrderRate)
|
|
}
|
|
|
|
// CancelConditionalFuturesOrders cancels untriggered conditional orders
|
|
func (by *Bybit) CancelConditionalFuturesOrders(ctx context.Context, symbol currency.Pair, stopOrderID, orderLinkID string) (string, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
StopOrderID string `json:"stop_order_id"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if stopOrderID == "" && orderLinkID == "" {
|
|
return "", errStopOrderOrOrderLinkIDMissing
|
|
}
|
|
if stopOrderID != "" {
|
|
params.Set("stop_order_id", stopOrderID)
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
return resp.Result.StopOrderID, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresCancelConditionalOrder, params, nil, &resp, futuresCancelConditionalOrderRate)
|
|
}
|
|
|
|
// CancelAllConditionalFuturesOrders cancels all untriggered conditional orders
|
|
func (by *Bybit) CancelAllConditionalFuturesOrders(ctx context.Context, symbol currency.Pair) ([]FuturesCancelOrderResp, error) {
|
|
resp := struct {
|
|
Result []FuturesCancelOrderResp `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresCancelAllConditionalOrders, params, nil, &resp, futuresCancelAllConditionalOrderRate)
|
|
}
|
|
|
|
// ReplaceConditionalFuturesOrders modify unfilled or partially filled conditional orders
|
|
func (by *Bybit) ReplaceConditionalFuturesOrders(ctx context.Context, symbol currency.Pair, stopOrderID, orderLinkID, takeProfitTriggerBy, stopLossTriggerBy string,
|
|
updatedQty, updatedPrice, takeProfitPrice, stopLossPrice, orderTriggerPrice float64) (string, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
OrderID string `json:"stop_order_id"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if stopOrderID == "" && orderLinkID == "" {
|
|
return "", errStopOrderOrOrderLinkIDMissing
|
|
}
|
|
if stopOrderID != "" {
|
|
params.Set("stop_order_id", stopOrderID)
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
if updatedQty != 0 {
|
|
params.Set("p_r_qty", strconv.FormatFloat(updatedQty, 'f', -1, 64))
|
|
}
|
|
if updatedPrice != 0 {
|
|
params.Set("p_r_price", strconv.FormatFloat(updatedPrice, 'f', -1, 64))
|
|
}
|
|
if orderTriggerPrice != 0 {
|
|
params.Set("p_r_trigger_price", strconv.FormatFloat(orderTriggerPrice, 'f', -1, 64))
|
|
}
|
|
if takeProfitPrice != 0 {
|
|
params.Set("take_profit", strconv.FormatFloat(takeProfitPrice, 'f', -1, 64))
|
|
}
|
|
if stopLossPrice != 0 {
|
|
params.Set("stop_loss", strconv.FormatFloat(stopLossPrice, 'f', -1, 64))
|
|
}
|
|
if takeProfitTriggerBy != "" {
|
|
params.Set("tp_trigger_by", takeProfitTriggerBy)
|
|
}
|
|
if stopLossTriggerBy != "" {
|
|
params.Set("sl_trigger_by", stopLossTriggerBy)
|
|
}
|
|
return resp.Result.OrderID, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresReplaceConditionalOrder, params, nil, &resp, futuresReplaceConditionalOrderRate)
|
|
}
|
|
|
|
// GetConditionalRealtimeOrders query real time conditional order data
|
|
func (by *Bybit) GetConditionalRealtimeOrders(ctx context.Context, symbol currency.Pair, stopOrderID, orderLinkID string) ([]FuturesConditionalRealtimeOrder, error) {
|
|
var data []FuturesConditionalRealtimeOrder
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if stopOrderID != "" {
|
|
params.Set("stop_order_id", stopOrderID)
|
|
}
|
|
if orderLinkID != "" {
|
|
params.Set("order_link_id", orderLinkID)
|
|
}
|
|
|
|
if stopOrderID == "" && orderLinkID == "" {
|
|
resp := struct {
|
|
Result []FuturesConditionalRealtimeOrder `json:"result"`
|
|
Error
|
|
}{}
|
|
err = by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetConditionalRealtimeOrders, params, nil, &resp, futuresGetConditionalRealtimeOrderRate)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
data = append(data, resp.Result...)
|
|
} else {
|
|
resp := struct {
|
|
Result FuturesConditionalRealtimeOrder `json:"result"`
|
|
Error
|
|
}{}
|
|
err = by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetConditionalRealtimeOrders, params, nil, &resp, futuresGetConditionalRealtimeOrderRate)
|
|
if err != nil {
|
|
return data, err
|
|
}
|
|
data = append(data, resp.Result)
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// GetPositions returns list of user positions
|
|
func (by *Bybit) GetPositions(ctx context.Context, symbol currency.Pair) ([]PositionResp, error) {
|
|
params := url.Values{}
|
|
resp := struct {
|
|
Result []struct {
|
|
Data PositionResp `json:"data"`
|
|
IsValid bool `json:"is_valid"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
if !symbol.IsEmpty() {
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
}
|
|
err := by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresPosition, params, nil, &resp, futuresPositionRate)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
data := make([]PositionResp, len(resp.Result))
|
|
for x := range resp.Result {
|
|
data[x] = resp.Result[x].Data
|
|
}
|
|
return data, nil
|
|
}
|
|
|
|
// SetMargin updates margin
|
|
func (by *Bybit) SetMargin(ctx context.Context, positionMode int64, symbol currency.Pair, margin string) (float64, error) {
|
|
resp := struct {
|
|
Result float64 `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if positionMode < 0 || positionMode > 2 {
|
|
return resp.Result, errInvalidPositionMode
|
|
}
|
|
params.Set("position_idx", strconv.FormatInt(positionMode, 10))
|
|
|
|
if margin == "" {
|
|
return resp.Result, errInvalidMargin
|
|
}
|
|
params.Set("margin", margin)
|
|
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresUpdateMargin, params, nil, &resp, futuresUpdateMarginRate)
|
|
}
|
|
|
|
// SetTradingAndStop sets take profit, stop loss, and trailing stop for your open position
|
|
func (by *Bybit) SetTradingAndStop(ctx context.Context, positionMode int64, symbol currency.Pair, takeProfit, stopLoss, trailingStop, newTrailingActive, stopLossQty, takeProfitQty float64, takeProfitTriggerBy, stopLossTriggerBy string) (SetTradingAndStopResp, error) {
|
|
resp := struct {
|
|
Result SetTradingAndStopResp `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if positionMode < 0 || positionMode > 2 {
|
|
return resp.Result, errInvalidPositionMode
|
|
}
|
|
params.Set("position_idx", strconv.FormatInt(positionMode, 10))
|
|
|
|
if takeProfit >= 0 {
|
|
params.Set("take_profit", strconv.FormatFloat(takeProfit, 'f', -1, 64))
|
|
}
|
|
if stopLoss >= 0 {
|
|
params.Set("stop_loss", strconv.FormatFloat(stopLoss, 'f', -1, 64))
|
|
}
|
|
if trailingStop >= 0 {
|
|
params.Set("trailing_stop", strconv.FormatFloat(trailingStop, 'f', -1, 64))
|
|
}
|
|
if newTrailingActive != 0 {
|
|
params.Set("new_trailing_active", strconv.FormatFloat(newTrailingActive, 'f', -1, 64))
|
|
}
|
|
if stopLossQty != 0 {
|
|
params.Set("sl_size", strconv.FormatFloat(stopLossQty, 'f', -1, 64))
|
|
}
|
|
if takeProfitQty != 0 {
|
|
params.Set("tp_size", strconv.FormatFloat(takeProfitQty, 'f', -1, 64))
|
|
}
|
|
if takeProfitTriggerBy != "" {
|
|
params.Set("tp_trigger_by", takeProfitTriggerBy)
|
|
}
|
|
if stopLossTriggerBy != "" {
|
|
params.Set("sl_trigger_by", stopLossTriggerBy)
|
|
}
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresSetTradingStop, params, nil, &resp, futuresSetTradingStopRate)
|
|
}
|
|
|
|
// SetLeverage sets leverage
|
|
func (by *Bybit) SetLeverage(ctx context.Context, symbol currency.Pair, buyLeverage, sellLeverage float64) (float64, error) {
|
|
resp := struct {
|
|
Result float64 `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
params.Set("buy_leverage", strconv.FormatFloat(buyLeverage, 'f', -1, 64))
|
|
params.Set("sell_leverage", strconv.FormatFloat(sellLeverage, 'f', -1, 64))
|
|
|
|
return resp.Result, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresSetLeverage, params, nil, &resp, futuresSetLeverageRate)
|
|
}
|
|
|
|
// ChangePositionMode switches mode between One-Way or Hedge Mode
|
|
func (by *Bybit) ChangePositionMode(ctx context.Context, symbol currency.Pair, mode int64) error {
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
params.Set("mode", strconv.FormatInt(mode, 10))
|
|
|
|
return by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresSwitchPositionMode, params, nil, nil, futuresSwitchPositionModeRate)
|
|
}
|
|
|
|
// ChangeMode switches mode between full or partial position
|
|
func (by *Bybit) ChangeMode(ctx context.Context, symbol currency.Pair, takeProfitStopLoss string) (string, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
Mode string `json:"tp_sl_mode"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result.Mode, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
if takeProfitStopLoss == "" {
|
|
return resp.Result.Mode, errInvalidTakeProfitStopLoss
|
|
}
|
|
params.Set("tp_sl_mode", takeProfitStopLoss)
|
|
|
|
return resp.Result.Mode, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresSwitchPosition, params, nil, &resp, futuresSwitchPositionRate)
|
|
}
|
|
|
|
// ChangeMargin switches margin between cross or isolated
|
|
func (by *Bybit) ChangeMargin(ctx context.Context, symbol currency.Pair, buyLeverage, sellLeverage float64, isIsolated bool) error {
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
params.Set("buy_leverage", strconv.FormatFloat(buyLeverage, 'f', -1, 64))
|
|
params.Set("sell_leverage", strconv.FormatFloat(sellLeverage, 'f', -1, 64))
|
|
|
|
if isIsolated {
|
|
params.Set("is_isolated", "true")
|
|
} else {
|
|
params.Set("is_isolated", "false")
|
|
}
|
|
|
|
return by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresSwitchMargin, params, nil, nil, futuresSwitchMarginRate)
|
|
}
|
|
|
|
// GetTradeRecords returns list of user trades
|
|
func (by *Bybit) GetTradeRecords(ctx context.Context, symbol currency.Pair, orderID, order string, startTime, page, limit int64) ([]TradeResp, error) {
|
|
params := url.Values{}
|
|
resp := struct {
|
|
Data struct {
|
|
OrderID string `json:"order_id"`
|
|
Trades []TradeResp `json:"trade_list"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Data.Trades, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
|
|
if orderID != "" {
|
|
params.Set("order_id", orderID)
|
|
}
|
|
if order != "" {
|
|
params.Set("order", order)
|
|
}
|
|
if startTime != 0 {
|
|
params.Set("start_time", strconv.FormatInt(startTime, 10))
|
|
}
|
|
if page != 0 {
|
|
params.Set("page", strconv.FormatInt(page, 10))
|
|
}
|
|
if limit > 0 && limit <= 200 {
|
|
params.Set("limit", strconv.FormatInt(limit, 10))
|
|
}
|
|
|
|
return resp.Data.Trades, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetTrades, params, nil, &resp, futuresGetTradeRate)
|
|
}
|
|
|
|
// GetClosedTrades returns closed profit and loss records
|
|
func (by *Bybit) GetClosedTrades(ctx context.Context, symbol currency.Pair, executionType string, startTime, endTime time.Time, page, limit int64) ([]ClosedTrades, error) {
|
|
params := url.Values{}
|
|
resp := struct {
|
|
Data struct {
|
|
CurrentPage int64 `json:"current_page"`
|
|
Trades []ClosedTrades `json:"data"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Data.Trades, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
|
|
if executionType != "" {
|
|
params.Set("execution_type", executionType)
|
|
}
|
|
if !startTime.IsZero() {
|
|
params.Set("start_time", strconv.FormatInt(startTime.Unix(), 10))
|
|
}
|
|
if !endTime.IsZero() {
|
|
params.Set("end_time", strconv.FormatInt(endTime.Unix(), 10))
|
|
}
|
|
if page > 0 && page <= 50 {
|
|
params.Set("page", strconv.FormatInt(page, 10))
|
|
}
|
|
if limit > 0 && limit <= 50 {
|
|
params.Set("limit", strconv.FormatInt(limit, 10))
|
|
}
|
|
|
|
return resp.Data.Trades, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodGet, futuresGetClosedTrades, params, nil, &resp, futuresDefaultRate)
|
|
}
|
|
|
|
// SetRiskLimit sets risk limit
|
|
func (by *Bybit) SetRiskLimit(ctx context.Context, symbol currency.Pair, riskID, positionMode int64) (int64, error) {
|
|
resp := struct {
|
|
Result struct {
|
|
RiskID int64 `json:"risk_id"`
|
|
} `json:"result"`
|
|
Error
|
|
}{}
|
|
|
|
params := url.Values{}
|
|
symbolValue, err := by.FormatSymbol(symbol, asset.Futures)
|
|
if err != nil {
|
|
return resp.Result.RiskID, err
|
|
}
|
|
params.Set("symbol", symbolValue)
|
|
|
|
if riskID <= 0 {
|
|
return resp.Result.RiskID, errInvalidRiskID
|
|
}
|
|
params.Set("risk_id", strconv.FormatInt(riskID, 10))
|
|
|
|
if positionMode < 0 || positionMode > 2 {
|
|
return resp.Result.RiskID, errInvalidPositionMode
|
|
}
|
|
params.Set("position_idx", strconv.FormatInt(positionMode, 10))
|
|
return resp.Result.RiskID, by.SendAuthHTTPRequest(ctx, exchange.RestFutures, http.MethodPost, futuresSetRiskLimit, params, nil, &resp, futuresDefaultRate)
|
|
}
|