exchange/order/limits: Migrate to new package and integrate with exchanges (#1860)

* move limits, transition to key gen

* rollout NewExchangePairAssetKey everywhere

* test improvements

* self-review fixes

* ok, lets go

* fix merge issue

* slower value func,assertify,drop IsValidPairString

* remove binance reference for backtesting test

* Redundant nil checks removed due to redundancy

* Update order_test.go

* Move limits back into /exchanges/

* puts limits in a different box again

* SHAZBERT SPECIAL SUGGESTIONS

* Update gateio_wrapper.go

* fixes all build issues

* Many niteroos!

* something has gone awry

* bugfix

* gk's everywhere nits

* lint

* extra lint

* re-remove IsValidPairString

* lint fix

* standardise test

* revert some bads

* dupe rm

* another revert 360 mcgee

* un-in-revertify

* Update exchange/order/limits/levels_test.go

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

* fix

* Update exchanges/binance/binance_test.go

HERE'S HOPING GITHUB FORMATS THIS CORRECTLY!

Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>

* update text

* rn func, same line err gk4202000

---------

Co-authored-by: Adrian Gallagher <adrian.gallagher@thrasher.io>
Co-authored-by: Gareth Kirwan <gbjkirwan@gmail.com>
This commit is contained in:
Scott
2025-08-26 12:30:21 +10:00
committed by GitHub
parent fc0f262c42
commit 85403fe801
103 changed files with 1751 additions and 2168 deletions

View File

@@ -18,6 +18,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/common/crypto"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchange/order/limits"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/kline"
"github.com/thrasher-corp/gocryptotrader/exchanges/order"
@@ -489,7 +490,7 @@ func (e *Exchange) PlaceOrder(ctx context.Context, arg *PlaceOrderParams) (*Orde
return nil, order.ErrTypeIsInvalid
}
if arg.OrderQuantity <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
switch arg.TriggerDirection {
case 0, 1, 2: // 0: None, 1: triggered when market price rises to triggerPrice, 2: triggered when market price falls to triggerPrice
@@ -698,7 +699,7 @@ func (e *Exchange) PlaceBatchOrder(ctx context.Context, arg *PlaceBatchOrderPara
return nil, order.ErrTypeIsInvalid
}
if arg.Request[a].OrderQuantity <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
}
var resp BatchOrdersList
@@ -1854,7 +1855,7 @@ func (e *Exchange) WithdrawCurrency(ctx context.Context, arg *WithdrawalParam) (
return "", errMissingAddressInfo
}
if arg.Amount <= 0 {
return "", order.ErrAmountBelowMin
return "", limits.ErrAmountBelowMin
}
if arg.Timestamp == 0 {
arg.Timestamp = time.Now().UnixMilli()
@@ -2067,7 +2068,7 @@ func (e *Exchange) PurchaseLeverageToken(ctx context.Context, ltCoin currency.Co
return nil, fmt.Errorf("%w, 'ltCoin' is required", currency.ErrCurrencyCodeEmpty)
}
if amount <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
arg := &struct {
LTCoin string `json:"ltCoin"`
@@ -2088,7 +2089,7 @@ func (e *Exchange) RedeemLeverageToken(ctx context.Context, ltCoin currency.Code
return nil, fmt.Errorf("%w, 'ltCoin' is required", currency.ErrCurrencyCodeEmpty)
}
if quantity <= 0 {
return nil, fmt.Errorf("%w, quantity=%f", order.ErrAmountBelowMin, quantity)
return nil, fmt.Errorf("%w, quantity=%f", limits.ErrAmountBelowMin, quantity)
}
arg := &struct {
LTCoin string `json:"ltCoin"`
@@ -2219,7 +2220,7 @@ func (e *Exchange) Borrow(ctx context.Context, arg *LendArgument) (*BorrowRespon
return nil, currency.ErrCurrencyCodeEmpty
}
if arg.AmountToBorrow <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
var resp *BorrowResponse
return resp, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodPost, "/v5/spot-cross-margin-trade/loan", nil, arg, &resp, spotCrossMarginTradeLoanEPL)
@@ -2234,7 +2235,7 @@ func (e *Exchange) Repay(ctx context.Context, arg *LendArgument) (*RepayResponse
return nil, currency.ErrCurrencyCodeEmpty
}
if arg.AmountToBorrow <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
var resp *RepayResponse
return resp, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodPost, "/v5/spot-cross-margin-trade/repay", nil, arg, &resp, spotCrossMarginTradeRepayEPL)
@@ -2404,7 +2405,7 @@ func (e *Exchange) C2CDepositFunds(ctx context.Context, arg *C2CLendingFundsPara
return nil, currency.ErrCurrencyCodeEmpty
}
if arg.Quantity <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
var resp *C2CLendingFundResponse
return resp, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodPost, "/v5/lending/purchase", nil, &arg, &resp, defaultEPL)
@@ -2419,7 +2420,7 @@ func (e *Exchange) C2CRedeemFunds(ctx context.Context, arg *C2CLendingFundsParam
return nil, currency.ErrCurrencyCodeEmpty
}
if arg.Quantity <= 0 {
return nil, order.ErrAmountBelowMin
return nil, limits.ErrAmountBelowMin
}
var resp *C2CLendingFundResponse
return resp, e.SendAuthHTTPRequestV5(ctx, exchange.RestSpot, http.MethodPost, "/v5/lending/redeem", nil, &arg, &resp, defaultEPL)

View File

@@ -19,6 +19,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/encoding/json"
"github.com/thrasher-corp/gocryptotrader/exchange/order/limits"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
"github.com/thrasher-corp/gocryptotrader/exchanges/account"
@@ -771,28 +772,16 @@ func TestGetDeliveryPrice(t *testing.T) {
func TestUpdateOrderExecutionLimits(t *testing.T) {
t.Parallel()
err := e.UpdateOrderExecutionLimits(t.Context(), asset.Futures)
assert.ErrorIs(t, err, asset.ErrNotSupported)
err = e.UpdateOrderExecutionLimits(t.Context(), asset.Options)
assert.NoError(t, err)
err = e.UpdateOrderExecutionLimits(t.Context(), asset.USDCMarginedFutures)
assert.NoError(t, err)
err = e.UpdateOrderExecutionLimits(t.Context(), asset.USDTMarginedFutures)
assert.NoError(t, err)
err = e.UpdateOrderExecutionLimits(t.Context(), asset.Spot)
assert.NoError(t, err)
availablePairs, err := e.GetAvailablePairs(asset.Spot)
if err != nil {
t.Fatal("Bybit GetAvailablePairs() error", err)
}
for x := range availablePairs {
var limits order.MinMaxLevel
limits, err = e.GetOrderExecutionLimits(asset.Spot, availablePairs[x])
require.NoError(t, err)
if limits == (order.MinMaxLevel{}) {
t.Fatal("Bybit GetOrderExecutionLimits() error cannot be nil")
}
for _, a := range e.GetAssetTypes(false) {
t.Run(a.String(), func(t *testing.T) {
t.Parallel()
require.NoError(t, e.UpdateOrderExecutionLimits(t.Context(), a), "UpdateOrderExecutionLimits must not error")
pairs, err := e.CurrencyPairs.GetPairs(a, true)
require.NoError(t, err, "GetPairs must not error")
l, err := e.GetOrderExecutionLimits(a, pairs[0])
require.NoError(t, err, "GetOrderExecutionLimits must not error")
assert.Positive(t, l.MinimumBaseAmount, "MinimumBaseAmount should be positive")
})
}
}
@@ -838,7 +827,7 @@ func TestPlaceOrder(t *testing.T) {
Side: "buy",
OrderType: "limit",
})
require.ErrorIs(t, err, order.ErrAmountBelowMin)
require.ErrorIs(t, err, limits.ErrAmountBelowMin)
_, err = e.PlaceOrder(ctx, &PlaceOrderParams{
Category: "spot",
@@ -2297,7 +2286,7 @@ func TestWithdrawCurrency(t *testing.T) {
require.ErrorIs(t, err, errMissingAddressInfo)
_, err = e.WithdrawCurrency(t.Context(), &WithdrawalParam{Coin: currency.LTC, Chain: "LTC", Address: "234234234"})
require.ErrorIs(t, err, order.ErrAmountBelowMin)
require.ErrorIs(t, err, limits.ErrAmountBelowMin)
_, err = e.WithdrawCurrency(t.Context(), &WithdrawalParam{Coin: currency.LTC, Chain: "LTC", Address: "234234234", Amount: 123})
if err != nil {
@@ -2664,7 +2653,7 @@ func TestBorrow(t *testing.T) {
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
_, err = e.Borrow(t.Context(), &LendArgument{Coin: currency.BTC})
assert.ErrorIs(t, err, order.ErrAmountBelowMin)
assert.ErrorIs(t, err, limits.ErrAmountBelowMin)
_, err = e.Borrow(t.Context(), &LendArgument{Coin: currency.BTC, AmountToBorrow: 0.1})
if err != nil {
@@ -2685,7 +2674,7 @@ func TestRepay(t *testing.T) {
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
_, err = e.Repay(t.Context(), &LendArgument{Coin: currency.BTC})
assert.ErrorIs(t, err, order.ErrAmountBelowMin)
assert.ErrorIs(t, err, limits.ErrAmountBelowMin)
_, err = e.Repay(t.Context(), &LendArgument{Coin: currency.BTC, AmountToBorrow: 0.1})
if err != nil {
@@ -2806,7 +2795,7 @@ func TestC2CDepositFunds(t *testing.T) {
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
_, err = e.C2CDepositFunds(t.Context(), &C2CLendingFundsParams{Coin: currency.BTC})
assert.ErrorIs(t, err, order.ErrAmountBelowMin)
assert.ErrorIs(t, err, limits.ErrAmountBelowMin)
_, err = e.C2CDepositFunds(t.Context(), &C2CLendingFundsParams{Coin: currency.BTC, Quantity: 1232})
if err != nil {
@@ -2827,7 +2816,7 @@ func TestC2CRedeemFunds(t *testing.T) {
assert.ErrorIs(t, err, currency.ErrCurrencyCodeEmpty)
_, err = e.C2CRedeemFunds(t.Context(), &C2CLendingFundsParams{Coin: currency.BTC})
assert.ErrorIs(t, err, order.ErrAmountBelowMin)
assert.ErrorIs(t, err, limits.ErrAmountBelowMin)
_, err = e.C2CRedeemFunds(t.Context(), &C2CLendingFundsParams{Coin: currency.BTC, Quantity: 1232})
if err != nil {

View File

@@ -13,6 +13,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/common/key"
"github.com/thrasher-corp/gocryptotrader/config"
"github.com/thrasher-corp/gocryptotrader/currency"
"github.com/thrasher-corp/gocryptotrader/exchange/order/limits"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket"
"github.com/thrasher-corp/gocryptotrader/exchange/websocket/buffer"
exchange "github.com/thrasher-corp/gocryptotrader/exchanges"
@@ -434,7 +435,7 @@ func (e *Exchange) FetchTradablePairs(ctx context.Context, a asset.Item) (curren
}
}
default:
return nil, fmt.Errorf("%w %v", asset.ErrNotSupported, a)
return nil, fmt.Errorf("%w %q", asset.ErrNotSupported, a)
}
pairs = make(currency.Pairs, 0, len(allPairs))
var filterSymbol string
@@ -1675,7 +1676,7 @@ func (e *Exchange) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item)
default:
return fmt.Errorf("%s %w", a, asset.ErrNotSupported)
}
limits := make([]order.MinMaxLevel, 0, len(allInstrumentsInfo.List))
l := make([]limits.MinMaxLevel, 0, len(allInstrumentsInfo.List))
for x := range allInstrumentsInfo.List {
if allInstrumentsInfo.List[x].Status != "Trading" {
continue
@@ -1686,9 +1687,8 @@ func (e *Exchange) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item)
log.Warnf(log.ExchangeSys, "%s unable to load limits for %s %v, pair data missing", e.Name, a, symbol)
continue
}
limits = append(limits, order.MinMaxLevel{
Asset: a,
Pair: pair,
l = append(l, limits.MinMaxLevel{
Key: key.NewExchangeAssetPair(e.Name, a, pair),
MinimumBaseAmount: allInstrumentsInfo.List[x].LotSizeFilter.MinOrderQty.Float64(),
MaximumBaseAmount: allInstrumentsInfo.List[x].LotSizeFilter.MaxOrderQty.Float64(),
MinPrice: allInstrumentsInfo.List[x].PriceFilter.MinPrice.Float64(),
@@ -1700,7 +1700,7 @@ func (e *Exchange) UpdateOrderExecutionLimits(ctx context.Context, a asset.Item)
MaximumQuoteAmount: allInstrumentsInfo.List[x].LotSizeFilter.MaxOrderQty.Float64() * allInstrumentsInfo.List[x].PriceFilter.MaxPrice.Float64(),
})
}
return e.LoadLimits(limits)
return limits.Load(l)
}
// SetLeverage sets the account's initial leverage for the asset type and pair
@@ -2105,12 +2105,7 @@ func (e *Exchange) GetOpenInterest(ctx context.Context, k ...key.PairAsset) ([]f
continue
}
return []futures.OpenInterest{{
Key: key.ExchangePairAsset{
Exchange: e.Name,
Asset: k[0].Asset,
Base: k[0].Base,
Quote: k[0].Quote,
},
Key: key.NewExchangeAssetPair(e.Name, k[0].Asset, k[0].Pair()),
OpenInterest: ticks.List[i].OpenInterest.Float64(),
}}, nil
}
@@ -2141,12 +2136,7 @@ func (e *Exchange) GetOpenInterest(ctx context.Context, k ...key.PairAsset) ([]f
continue
}
resp = append(resp, futures.OpenInterest{
Key: key.ExchangePairAsset{
Exchange: e.Name,
Base: pair.Base.Item,
Quote: pair.Quote.Item,
Asset: assets[i],
},
Key: key.NewExchangeAssetPair(e.Name, assets[i], pair),
OpenInterest: ticks.List[i].OpenInterest.Float64(),
})
}
@@ -2205,6 +2195,6 @@ func (e *Exchange) GetCurrencyTradeURL(ctx context.Context, a asset.Item, cp cur
case asset.Options:
return tradeBaseURL + "trade/option/usdc/" + cp.Base.Upper().String(), nil
default:
return "", fmt.Errorf("%w %v", asset.ErrNotSupported, a)
return "", fmt.Errorf("%w %q", asset.ErrNotSupported, a)
}
}