From 95f7d8518365d7f9fe34995000f47eca528de82c Mon Sep 17 00:00:00 2001 From: c9s Date: Wed, 4 May 2022 14:23:09 +0800 Subject: [PATCH] bbgo: pass price time into the asset conversion function --- pkg/bbgo/environment.go | 4 +++- pkg/bbgo/session.go | 18 +++++------------- pkg/server/routes.go | 6 +++--- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/pkg/bbgo/environment.go b/pkg/bbgo/environment.go index 236f66446..be1cd0d3a 100644 --- a/pkg/bbgo/environment.go +++ b/pkg/bbgo/environment.go @@ -659,7 +659,9 @@ func (environ *Environment) RecordAsset(t time.Time, sessionName string, exchang return } - environ.AccountService.InsertAsset(t, sessionName, exchangeName, account, assets) + if err := environ.AccountService.InsertAsset(t, sessionName, exchangeName, account, assets); err != nil { + log.WithError(err).Errorf("can not insert asset record") + } } func (environ *Environment) RecordPosition(position *types.Position, trade types.Trade, profit *types.Profit) { diff --git a/pkg/bbgo/session.go b/pkg/bbgo/session.go index e2fba67ee..ee3e6254e 100644 --- a/pkg/bbgo/session.go +++ b/pkg/bbgo/session.go @@ -646,21 +646,18 @@ func (session *ExchangeSession) FormatOrder(order types.SubmitOrder) (types.Subm return order, nil } -func (session *ExchangeSession) UpdatePrices(ctx context.Context) (err error) { +func (session *ExchangeSession) UpdatePrices(ctx context.Context, currencies []string, fiat string) (err error) { if session.lastPriceUpdatedAt.After(time.Now().Add(-time.Hour)) { return nil } - balances := session.GetAccount().Balances() - var symbols []string - for _, b := range balances { - symbols = append(symbols, b.Currency+"USDT") - symbols = append(symbols, "USDT"+b.Currency) + for _, c := range currencies { + symbols = append(symbols, c+fiat) // BTC/USDT + symbols = append(symbols, fiat+c) // USDT/TWD } tickers, err := session.Exchange.QueryTickers(ctx, symbols...) - if err != nil || len(tickers) == 0 { return err } @@ -668,12 +665,7 @@ func (session *ExchangeSession) UpdatePrices(ctx context.Context) (err error) { var lastTime time.Time for k, v := range tickers { // for {Crypto}/USDT markets - if strings.HasSuffix(k, "USDT") { - session.lastPrices[k] = v.Last - } else if strings.HasPrefix(k, "USDT") { - session.lastPrices[k] = fixedpoint.One.Div(v.Last) - } - + session.lastPrices[k] = v.Last if v.Time.After(lastTime) { lastTime = v.Time } diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 68a67cdce..94e8f5fd1 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -442,7 +442,7 @@ func genFakeAssets() types.AssetMap { "DOTUSDT": fixedpoint.NewFromFloat(20.0), "SANDUSDT": fixedpoint.NewFromFloat(0.13), "MAXUSDT": fixedpoint.NewFromFloat(0.122), - }) + }, time.Now()) for currency, asset := range assets { totalAssets[currency] = asset } @@ -460,13 +460,13 @@ func (s *Server) listAssets(c *gin.Context) { for _, session := range s.Environ.Sessions() { balances := session.GetAccount().Balances() - if err := session.UpdatePrices(c); err != nil { + if err := session.UpdatePrices(c, balances.Currencies(), "USDT"); err != nil { logrus.WithError(err).Error("price update failed") c.Status(http.StatusInternalServerError) return } - assets := balances.Assets(session.LastPrices()) + assets := balances.Assets(session.LastPrices(), time.Now()) for currency, asset := range assets { totalAssets[currency] = asset