bbgo: pass price time into the asset conversion function

This commit is contained in:
c9s 2022-05-04 14:23:09 +08:00
parent 3b25db31df
commit 95f7d85183
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
3 changed files with 11 additions and 17 deletions

View File

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

View File

@ -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)
}
if v.Time.After(lastTime) {
lastTime = v.Time
}

View File

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