bbgo: pull out findPossibleMarketSymbols and add tests

This commit is contained in:
c9s 2023-12-18 22:09:04 +08:00
parent 671ce872c4
commit 882c1273b3
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 69 additions and 19 deletions

View File

@ -1020,7 +1020,7 @@ func (session *ExchangeSession) getSessionSymbols(defaultSymbols ...string) ([]s
return defaultSymbols, nil
}
return session.FindPossibleSymbols()
return session.FindPossibleAssetSymbols()
}
func defaultSyncSinceTime() time.Time {

View File

@ -704,25 +704,10 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context, currencies []s
// }
markets := session.Markets()
var symbols []string
for _, c := range currencies {
var tries []string
// expand USD stable coin currencies
if types.IsUSDFiatCurrency(fiat) {
for _, usdFiat := range types.USDFiatCurrencies {
tries = append(tries, c+usdFiat, usdFiat+c)
}
} else {
tries = []string{c + fiat, fiat + c}
}
for _, try := range tries {
if markets.Has(try) {
symbols = append(symbols, try)
break
}
}
possibleSymbols := findPossibleMarketSymbols(markets, c, fiat)
symbols = append(symbols, possibleSymbols...)
}
if len(symbols) == 0 {
@ -757,7 +742,7 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context, currencies []s
return err
}
func (session *ExchangeSession) FindPossibleSymbols() (symbols []string, err error) {
func (session *ExchangeSession) FindPossibleAssetSymbols() (symbols []string, err error) {
// If the session is an isolated margin session, there will be only the isolated margin symbol
if session.Margin && session.IsolatedMargin {
return []string{
@ -1041,3 +1026,24 @@ func (session *ExchangeSession) FormatOrders(orders []types.SubmitOrder) (format
return formattedOrders, err
}
func findPossibleMarketSymbols(markets types.MarketMap, c, fiat string) (symbols []string) {
var tries []string
// expand USD stable coin currencies
if types.IsUSDFiatCurrency(fiat) {
for _, usdFiat := range types.USDFiatCurrencies {
tries = append(tries, c+usdFiat, usdFiat+c)
}
} else {
tries = []string{c + fiat, fiat + c}
}
for _, try := range tries {
if markets.Has(try) {
symbols = append(symbols, try)
break
}
}
return symbols
}

44
pkg/bbgo/session_test.go Normal file
View File

@ -0,0 +1,44 @@
package bbgo
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/c9s/bbgo/pkg/types"
)
func Test_findPossibleMarketSymbols(t *testing.T) {
t.Run("btcusdt", func(t *testing.T) {
markets := types.MarketMap{
"BTCUSDT": types.Market{},
"BTCUSDC": types.Market{},
"BTCUSD": types.Market{},
"BTCBUSD": types.Market{},
}
symbols := findPossibleMarketSymbols(markets, "BTC", "USDT")
if assert.Len(t, symbols, 1) {
assert.Equal(t, "BTCUSDT", symbols[0])
}
})
t.Run("btcusd only", func(t *testing.T) {
markets := types.MarketMap{
"BTCUSD": types.Market{},
}
symbols := findPossibleMarketSymbols(markets, "BTC", "USDT")
if assert.Len(t, symbols, 1) {
assert.Equal(t, "BTCUSD", symbols[0])
}
})
t.Run("usd to stable coin", func(t *testing.T) {
markets := types.MarketMap{
"BTCUSDT": types.Market{},
}
symbols := findPossibleMarketSymbols(markets, "BTC", "USD")
if assert.Len(t, symbols, 1) {
assert.Equal(t, "BTCUSDT", symbols[0])
}
})
}