From 671ce872c474023071d56989da8b4c9b6bb0fe4b Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 18 Dec 2023 22:01:11 +0800 Subject: [PATCH] bbgo: fix and improve session UpdatePrice method --- pkg/bbgo/session.go | 38 ++++++++++++++++++++++++++++++++++---- pkg/types/currencies.go | 7 +++++++ pkg/types/market.go | 9 ++++++++- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index 6778ce35b..1c414f6bd 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -654,7 +654,7 @@ func (session *ExchangeSession) Market(symbol string) (market types.Market, ok b return market, ok } -func (session *ExchangeSession) Markets() map[string]types.Market { +func (session *ExchangeSession) Markets() types.MarketMap { return session.markets } @@ -703,10 +703,30 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context, currencies []s // return nil // } + markets := session.Markets() + var symbols []string for _, c := range currencies { - symbols = append(symbols, c+fiat) // BTC/USDT - symbols = append(symbols, fiat+c) // USDT/TWD + 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 + } + } + } + + if len(symbols) == 0 { + return nil } tickers, err := session.Exchange.QueryTickers(ctx, symbols...) @@ -717,7 +737,17 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context, currencies []s var lastTime time.Time for k, v := range tickers { // for {Crypto}/USDT markets - session.lastPrices[k] = v.Last + // map things like BTCUSDT = {price} + if market, ok := markets[k]; ok { + if types.IsFiatCurrency(market.BaseCurrency) { + session.lastPrices[k] = v.Last.Div(fixedpoint.One) + } else { + session.lastPrices[k] = v.Last + } + } else { + session.lastPrices[k] = v.Last + } + if v.Time.After(lastTime) { lastTime = v.Time } diff --git a/pkg/types/currencies.go b/pkg/types/currencies.go index 4e24c3a83..3c5b539e2 100644 --- a/pkg/types/currencies.go +++ b/pkg/types/currencies.go @@ -24,8 +24,15 @@ var USD = wrapper{accounting.Accounting{Symbol: "$ ", Precision: 2}} var BTC = wrapper{accounting.Accounting{Symbol: "BTC ", Precision: 8}} var BNB = wrapper{accounting.Accounting{Symbol: "BNB ", Precision: 4}} +const ( + USDT = "USDT" + USDC = "USDC" + BUSD = "BUSD" +) + var FiatCurrencies = []string{"USDC", "USDT", "USD", "TWD", "EUR", "GBP", "BUSD"} +// USDFiatCurrencies lists the USD stable coins var USDFiatCurrencies = []string{"USDT", "USDC", "USD", "BUSD"} func IsUSDFiatCurrency(currency string) bool { diff --git a/pkg/types/market.go b/pkg/types/market.go index 8829fed73..5ea42d6d7 100644 --- a/pkg/types/market.go +++ b/pkg/types/market.go @@ -90,7 +90,9 @@ func (m Market) TruncateQuoteQuantity(quantity fixedpoint.Value) fixedpoint.Valu // when side = buy, then available = quote balance // The balance will be truncated first in order to calculate the minimal notional and minimal quantity // The adjusted (truncated) order quantity will be returned -func (m Market) GreaterThanMinimalOrderQuantity(side SideType, price, available fixedpoint.Value) (fixedpoint.Value, bool) { +func (m Market) GreaterThanMinimalOrderQuantity( + side SideType, price, available fixedpoint.Value, +) (fixedpoint.Value, bool) { switch side { case SideTypeSell: available = m.TruncateQuantity(available) @@ -236,3 +238,8 @@ type MarketMap map[string]Market func (m MarketMap) Add(market Market) { m[market.Symbol] = market } + +func (m MarketMap) Has(symbol string) bool { + _, ok := m[symbol] + return ok +}