Binance,Okx: Add Leverage, MarginType, Positions and CollateralMode support (#1220)

* init

* surprise train commit

* basic distinctions

* the terms of binance are confusing

* renames and introduction of allocatedMargin

* add new margin funcs

* pulling out wires

* implement proper getposition stuff

* bad coding day

* investigate order manager next

* a broken mess, but a progressing one

* finally completes some usdtmargined stuff

* coinMfutures eludes me

* expand to okx

* imports fix

* completes okx wrapper implementations

* cleans and polishes before rpc implementations

* rpc setup, order manager features, exch features

* more rpc, collateral and margin things

* mini test

* looking at rpc response, expansion of features

* reorganising before the storm

* changing how futures requests work

* cleanup and tests of cli usage

* remove silly client side logic

* cleanup

* collateral package, typo fix, margin err, rpc derive

* uses convert.StringToFloat ONLY ON STRUCTS FROM THIS PR

* fix binance order history bug

* niteroos

* adds new funcs to exchange standards testing

* more post merge fixes

* fix binance

* replace simepletimeformat

* fix for merge

* merge fixes

* micro fixes

* order side now required for leverage

* fix up the rest

* global -> portfolio collateral

* Update exchanges/collateral/collateral_test.go

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>

* adds fields and todos

* rm field redundancy

* lint fix oopsie daisy

* fixes panic, expands error and cli explanations (sorry shaz)

* ensures casing is appropriate for underlying

* Adds a shiny TODO

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
This commit is contained in:
Scott
2023-09-26 16:16:31 +10:00
committed by GitHub
parent a2ae99ed7f
commit 5f2f6f884b
67 changed files with 11558 additions and 4475 deletions

View File

@@ -174,7 +174,7 @@ func (by *Bybit) GetTrades(ctx context.Context, symbol string, limit int64) ([]T
params.Set("symbol", symbol)
strLimit := "60" // default limit
if limit > 0 && limit < 60 {
if limit > 0 {
strLimit = strconv.FormatInt(limit, 10)
}
params.Set("limit", strLimit)

View File

@@ -131,7 +131,7 @@ func (by *Bybit) GetFuturesKlineData(ctx context.Context, symbol currency.Pair,
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -178,7 +178,7 @@ func (by *Bybit) GetPublicTrades(ctx context.Context, symbol currency.Pair, limi
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 1000 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -208,7 +208,7 @@ func (by *Bybit) GetMarkPriceKline(ctx context.Context, symbol currency.Pair, in
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -237,7 +237,7 @@ func (by *Bybit) GetIndexPriceKline(ctx context.Context, symbol currency.Pair, i
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -266,7 +266,7 @@ func (by *Bybit) GetPremiumIndexPriceKline(ctx context.Context, symbol currency.
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -295,7 +295,7 @@ func (by *Bybit) GetOpenInterest(ctx context.Context, symbol currency.Pair, peri
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesPeriods, period) {
@@ -320,7 +320,7 @@ func (by *Bybit) GetLatestBigDeal(ctx context.Context, symbol currency.Pair, lim
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 1000 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -341,7 +341,7 @@ func (by *Bybit) GetAccountRatio(ctx context.Context, symbol currency.Pair, peri
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 500 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesPeriods, period) {
@@ -491,7 +491,7 @@ func (by *Bybit) GetActiveCoinFuturesOrders(ctx context.Context, symbol currency
if direction != "" {
params.Set("direction", direction)
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if cursor != "" {
@@ -709,7 +709,7 @@ func (by *Bybit) GetConditionalCoinFuturesOrders(ctx context.Context, symbol cur
if direction != "" {
params.Set("direction", direction)
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if cursor != "" {
@@ -999,7 +999,7 @@ func (by *Bybit) GetCoinTradeRecords(ctx context.Context, symbol currency.Pair,
if page != 0 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -1036,7 +1036,7 @@ func (by *Bybit) GetClosedCoinTrades(ctx context.Context, symbol currency.Pair,
if page > 0 && page <= 50 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -1247,7 +1247,7 @@ func (by *Bybit) GetWalletFundRecords(ctx context.Context, startDate, endDate, c
if page != 0 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -1280,7 +1280,7 @@ func (by *Bybit) GetWalletWithdrawalRecords(ctx context.Context, startDate, endD
if page != 0 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -1303,7 +1303,7 @@ func (by *Bybit) GetAssetExchangeRecords(ctx context.Context, direction string,
if from != 0 {
params.Set("from", strconv.FormatInt(from, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}

View File

@@ -122,7 +122,7 @@ func (by *Bybit) GetActiveFuturesOrders(ctx context.Context, symbol currency.Pai
if direction != "" {
params.Set("direction", direction)
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if cursor != "" {
@@ -345,7 +345,7 @@ func (by *Bybit) GetConditionalFuturesOrders(ctx context.Context, symbol currenc
if direction != "" {
params.Set("direction", direction)
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if cursor != "" {
@@ -583,8 +583,8 @@ func (by *Bybit) SetTradingAndStop(ctx context.Context, positionMode int64, symb
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) {
// SetLeverageLevel sets leverage
func (by *Bybit) SetLeverageLevel(ctx context.Context, symbol currency.Pair, buyLeverage, sellLeverage float64) (float64, error) {
resp := struct {
Result float64 `json:"result"`
Error
@@ -685,7 +685,7 @@ func (by *Bybit) GetTradeRecords(ctx context.Context, symbol currency.Pair, orde
if page != 0 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -721,7 +721,7 @@ func (by *Bybit) GetClosedTrades(ctx context.Context, symbol currency.Pair, exec
if page > 0 && page <= 50 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}

View File

@@ -1870,7 +1870,7 @@ func TestSetLeverage(t *testing.T) {
t.Fatal(err)
}
_, err = b.SetLeverage(context.Background(), pair, 10, 10)
_, err = b.SetLeverageLevel(context.Background(), pair, 10, 10)
if err != nil {
t.Error(err)
}

View File

@@ -72,7 +72,7 @@ func (by *Bybit) GetUSDTFuturesKlineData(ctx context.Context, symbol currency.Pa
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -101,7 +101,7 @@ func (by *Bybit) GetUSDTPublicTrades(ctx context.Context, symbol currency.Pair,
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 1000 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -122,7 +122,7 @@ func (by *Bybit) GetUSDTMarkPriceKline(ctx context.Context, symbol currency.Pair
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -151,7 +151,7 @@ func (by *Bybit) GetUSDTIndexPriceKline(ctx context.Context, symbol currency.Pai
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -180,7 +180,7 @@ func (by *Bybit) GetUSDTPremiumIndexPriceKline(ctx context.Context, symbol curre
return resp.Data, err
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if !common.StringDataCompare(validFuturesIntervals, interval) {
@@ -327,7 +327,7 @@ func (by *Bybit) GetActiveUSDTFuturesOrders(ctx context.Context, symbol currency
if page > 0 && page <= 50 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if orderID != "" {
@@ -559,7 +559,7 @@ func (by *Bybit) GetConditionalUSDTFuturesOrders(ctx context.Context, symbol cur
if direction != "" {
params.Set("order", direction)
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
if page != 0 {
@@ -940,7 +940,7 @@ func (by *Bybit) GetUSDTTradeRecords(ctx context.Context, symbol currency.Pair,
if page != 0 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data.Trades, by.SendAuthHTTPRequest(ctx, exchange.RestUSDTMargined, http.MethodGet, ufuturesGetTrades, params, nil, &resp, uFuturesGetTradesRate)
@@ -975,7 +975,7 @@ func (by *Bybit) GetClosedUSDTTrades(ctx context.Context, symbol currency.Pair,
if page > 0 && page <= 50 {
params.Set("page", strconv.FormatInt(page, 10))
}
if limit > 0 && limit <= 50 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data.Trades, by.SendAuthHTTPRequest(ctx, exchange.RestUSDTMargined, http.MethodGet, ufuturesGetClosedTrades, params, nil, &resp, uFuturesGetClosedTradesRate)

View File

@@ -115,7 +115,7 @@ func (by *Bybit) GetUSDCContracts(ctx context.Context, symbol currency.Pair, dir
if direction != "" {
params.Set("direction", direction)
}
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
@@ -169,7 +169,7 @@ func (by *Bybit) GetUSDCKlines(ctx context.Context, symbol currency.Pair, period
}
params.Set("startTime", strconv.FormatInt(startTime.Unix(), 10))
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetKlines, params), usdcPublicRate, &resp)
@@ -202,7 +202,7 @@ func (by *Bybit) GetUSDCMarkPriceKlines(ctx context.Context, symbol currency.Pai
}
params.Set("startTime", strconv.FormatInt(startTime.Unix(), 10))
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetMarkPriceKlines, params), usdcPublicRate, &resp)
@@ -235,7 +235,7 @@ func (by *Bybit) GetUSDCIndexPriceKlines(ctx context.Context, symbol currency.Pa
}
params.Set("startTime", strconv.FormatInt(startTime.Unix(), 10))
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetIndexPriceKlines, params), usdcPublicRate, &resp)
@@ -268,7 +268,7 @@ func (by *Bybit) GetUSDCPremiumIndexKlines(ctx context.Context, symbol currency.
}
params.Set("startTime", strconv.FormatInt(startTime.Unix(), 10))
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetPremiumIndexKlines, params), usdcPublicRate, &resp)
@@ -296,7 +296,7 @@ func (by *Bybit) GetUSDCOpenInterest(ctx context.Context, symbol currency.Pair,
}
params.Set("period", period)
if limit > 0 && limit <= 200 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetOpenInterest, params), usdcPublicRate, &resp)
@@ -319,7 +319,7 @@ func (by *Bybit) GetUSDCLargeOrders(ctx context.Context, symbol currency.Pair, l
}
params.Set("symbol", symbolValue)
if limit > 0 && limit <= 100 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetLargeOrders, params), usdcPublicRate, &resp)
@@ -347,7 +347,7 @@ func (by *Bybit) GetUSDCAccountRatio(ctx context.Context, symbol currency.Pair,
}
params.Set("period", period)
if limit > 0 && limit <= 500 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetAccountRatio, params), usdcPublicRate, &resp)
@@ -378,7 +378,7 @@ func (by *Bybit) GetUSDCLatestTrades(ctx context.Context, symbol currency.Pair,
params.Set("symbol", symbolValue)
}
if limit > 0 && limit <= 500 {
if limit > 0 {
params.Set("limit", strconv.FormatInt(limit, 10))
}
return resp.Result.Data, by.SendHTTPRequest(ctx, exchange.RestUSDCMargined, common.EncodeURLValues(usdcfuturesGetLatestTrades, params), usdcPublicRate, &resp)
@@ -749,7 +749,7 @@ func (by *Bybit) GetUSDCTradeHistory(ctx context.Context, symbol currency.Pair,
req["direction"] = direction
}
if limit > 0 && limit <= 50 {
if limit > 0 {
req["limit"] = strconv.FormatInt(limit, 10)
}
@@ -792,7 +792,7 @@ func (by *Bybit) GetUSDCTransactionLog(ctx context.Context, startTime, endTime t
req["direction"] = direction
}
if limit > 0 && limit <= 50 {
if limit > 0 {
req["limit"] = strconv.FormatInt(limit, 10)
}
@@ -875,7 +875,7 @@ func (by *Bybit) GetUSDCPosition(ctx context.Context, symbol currency.Pair, cate
req["direction"] = direction
}
if limit > 0 && limit <= 50 {
if limit > 0 {
req["limit"] = strconv.FormatInt(limit, 10)
}
@@ -939,7 +939,7 @@ func (by *Bybit) GetUSDCSettlementHistory(ctx context.Context, symbol currency.P
req["direction"] = direction
}
if limit > 0 && limit <= 50 {
if limit > 0 {
req["limit"] = strconv.FormatInt(limit, 10)
}