bbgo: update balances metrics and trade metrics

This commit is contained in:
c9s 2021-12-27 17:16:30 +08:00
parent 0f24eec715
commit 7b629c9d30
2 changed files with 80 additions and 7 deletions

View File

@ -16,17 +16,42 @@ var (
},
)
metricsBalances = prometheus.NewGaugeVec(
metricsLockedBalances = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances",
Help: "bbgo exchange session balance",
Name: "bbgo_balances_locked",
Help: "bbgo exchange locked balances",
},
[]string{
"exchange", // exchange name
"status", // 1 -> ON, 0 -> OFF
"margin", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection.
"currency",
},
)
metricsAvailableBalances = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_available",
Help: "bbgo exchange available balances",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection.
"currency",
},
)
metricsTotalBalances = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_balances_total",
Help: "bbgo exchange session total balances",
},
[]string{
"exchange", // exchange name
"margin", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection.
"currency",
"margin", // margin of connection. 1 or 0
"symbol", // margin symbol of the connection.
},
)
@ -64,7 +89,9 @@ var (
func init() {
prometheus.MustRegister(
metricsConnectionStatus,
metricsBalances,
metricsTotalBalances,
metricsLockedBalances,
metricsAvailableBalances,
metricsTradesTotal,
metricsTradingVolume,
)

View File

@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
log "github.com/sirupsen/logrus"
"github.com/c9s/bbgo/pkg/cmd/cmdutil"
@ -321,6 +322,8 @@ func (session *ExchangeSession) Init(ctx context.Context, environ *Environment)
session.UserDataStream.OnTradeUpdate(session.OrderExecutor.EmitTradeUpdate)
session.UserDataStream.OnOrderUpdate(session.OrderExecutor.EmitOrderUpdate)
session.Account.BindStream(session.UserDataStream)
session.bindUserDataStreamMetrics(session.UserDataStream)
}
// TODO: move this logic to Environment struct
@ -771,3 +774,46 @@ func (session *ExchangeSession) InitExchange(name string, exchange types.Exchang
session.logger = log.WithField("session", name)
return nil
}
func (session *ExchangeSession) MarginType() string {
margin := "none"
if session.Margin {
margin = "margin"
if session.IsolatedMargin {
margin = "isolated"
}
}
return margin
}
func (session *ExchangeSession) metricsBalancesUpdater(balances types.BalanceMap) {
for currency, balance := range balances {
labels := prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"symbol": session.IsolatedMarginSymbol,
"currency": currency,
}
metricsTotalBalances.With(labels).Set(balance.Total().Float64())
metricsLockedBalances.With(labels).Set(balance.Locked.Float64())
metricsAvailableBalances.With(labels).Set(balance.Available.Float64())
}
}
func (session *ExchangeSession) metricsTradeUpdater(trade types.Trade) {
labels := prometheus.Labels{
"exchange": session.ExchangeName.String(),
"margin": session.MarginType(),
"side": trade.Side.String(),
"symbol": trade.Symbol,
"liquidity": trade.Liquidity(),
}
metricsTradingVolume.With(labels).Add(trade.Quantity)
metricsTradesTotal.With(labels).Inc()
}
func (session *ExchangeSession) bindUserDataStreamMetrics(stream types.Stream) {
stream.OnBalanceUpdate(session.metricsBalancesUpdater)
stream.OnTradeUpdate(session.metricsTradeUpdater)
}