bbgo: fix and improve session UpdatePrice method

This commit is contained in:
c9s 2023-12-18 22:01:11 +08:00
parent 3ba46f9b7e
commit 671ce872c4
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 49 additions and 5 deletions

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}