Skip to content

Commit

Permalink
Merge pull request #1859 from c9s/c9s/account-margin-level-metrics
Browse files Browse the repository at this point in the history
FEATURE: add account margin level metrics
  • Loading branch information
c9s authored Dec 11, 2024
2 parents 46db54e + 7626879 commit c288b9e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
20 changes: 20 additions & 0 deletions pkg/bbgo/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/c9s/bbgo/pkg/cache"
"github.com/c9s/bbgo/pkg/envvar"
"github.com/c9s/bbgo/pkg/exchange/retry"
"github.com/c9s/bbgo/pkg/metrics"
"github.com/c9s/bbgo/pkg/util/templateutil"

exchange2 "github.com/c9s/bbgo/pkg/exchange"
Expand Down Expand Up @@ -232,13 +233,32 @@ func (session *ExchangeSession) GetAccount() (a *types.Account) {
return a
}

func (session *ExchangeSession) GetIsolatedSymbol() string {
isolatedSymbol := ""
if session.IsolatedMarginSymbol != "" {
isolatedSymbol = session.IsolatedMarginSymbol
} else if session.IsolatedFuturesSymbol != "" {
isolatedSymbol = session.IsolatedFuturesSymbol
}

return isolatedSymbol
}

// UpdateAccount locks the account mutex and update the account object
func (session *ExchangeSession) UpdateAccount(ctx context.Context) (*types.Account, error) {
account, err := session.Exchange.QueryAccount(ctx)
if err != nil {
return nil, err
}

isolatedSymbol := session.GetIsolatedSymbol()

metrics.AccountMarginLevelMetrics.With(prometheus.Labels{
"exchange": session.ExchangeName.String(),
"account_type": string(account.AccountType),
"isolated_symbol": isolatedSymbol,
}).Set(account.MarginLevel.Float64())

session.setAccount(account)
return account, nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/exchange/max/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ func (s *Stream) handleTradeEvent(e max.TradeUpdateEvent) {
}
}

// handleBookEvent returns a callback that will be registered to the websocket stream
// this callback will be called when the websocket stream receives a book event
func (s *Stream) handleBookEvent(ex *Exchange) func(e max.BookEvent) {
return func(e max.BookEvent) {
symbol := toGlobalSymbol(e.Market)
Expand Down
13 changes: 13 additions & 0 deletions pkg/metrics/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package metrics

import "github.com/prometheus/client_golang/prometheus"

var AccountMarginLevelMetrics = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_account_margin_level",
Help: "account margin level metrics",
}, []string{"exchange", "account_type", "isolated_symbol"})

func init() {
prometheus.MustRegister(AccountMarginLevelMetrics)
}

0 comments on commit c288b9e

Please sign in to comment.