Skip to content

Commit

Permalink
bbgo: fix and improve session UpdatePrice method
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Dec 18, 2023
1 parent 3ba46f9 commit 671ce87
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
38 changes: 34 additions & 4 deletions pkg/bbgo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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...)
Expand All @@ -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
}
Expand Down
7 changes: 7 additions & 0 deletions pkg/types/currencies.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 8 additions & 1 deletion pkg/types/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

0 comments on commit 671ce87

Please sign in to comment.