Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/strategy/liquiditymaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
return err
}

s.Position.UpdateMetrics()
s.Position.UpdateMetrics(nil)

session.MarketDataStream.OnKLineClosed(func(k types.KLine) {
if k.Interval == s.AdjustmentUpdateInterval {
Expand Down
2 changes: 1 addition & 1 deletion pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2660,7 +2660,7 @@ func (s *Strategy) CrossRun(
s.logger.WithError(err).Warnf("unable to cancel all orders: %v", err)
}

s.Position.UpdateMetrics()
s.Position.UpdateMetrics(nil)
bbgo.Notify("xmaker: %s position is restored", s.Symbol, s.Position)

// restore position into the position exposure
Expand Down
13 changes: 9 additions & 4 deletions pkg/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
p.Lock()
defer p.Unlock()

defer p.updateMetrics()
defer p.updateMetrics(&td.Price)

// update changedAt field before we unlock in the defer func
defer func() {
Expand Down Expand Up @@ -740,13 +740,13 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
return fixedpoint.Zero, fixedpoint.Zero, false
}

func (p *Position) UpdateMetrics() {
func (p *Position) UpdateMetrics(price *fixedpoint.Value) {
p.Lock()
p.updateMetrics()
p.updateMetrics(price)
p.Unlock()
}

func (p *Position) updateMetrics() {
func (p *Position) updateMetrics(price *fixedpoint.Value) {
// update the position metrics only if the position defines the strategy ID
if p.StrategyInstanceID == "" || p.Strategy == "" {
return
Expand All @@ -760,4 +760,9 @@ func (p *Position) updateMetrics() {
positionAverageCostMetrics.With(labels).Set(p.AverageCost.Float64())
positionBaseQuantityMetrics.With(labels).Set(p.Base.Float64())
positionQuoteQuantityMetrics.With(labels).Set(p.Quote.Float64())

if price != nil {
unrealizedProfit := p.UnrealizedProfit(*price)
positionUnrealizedProfitMetrics.With(labels).Set(unrealizedProfit.Float64())
}
}
23 changes: 12 additions & 11 deletions pkg/types/position_metrics.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package types

import "github.com/prometheus/client_golang/prometheus"
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var positionAverageCostMetrics = prometheus.NewGaugeVec(
var positionAverageCostMetrics = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_avg_cost",
Help: "bbgo position average cost metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})

var positionBaseQuantityMetrics = prometheus.NewGaugeVec(
var positionBaseQuantityMetrics = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_base_qty",
Help: "bbgo position base quantity metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})

var positionQuoteQuantityMetrics = prometheus.NewGaugeVec(
var positionQuoteQuantityMetrics = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_quote_qty",
Help: "bbgo position quote quantity metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})

func init() {
prometheus.MustRegister(
positionAverageCostMetrics,
positionBaseQuantityMetrics,
positionQuoteQuantityMetrics,
)
}
var positionUnrealizedProfitMetrics = promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "bbgo_position_unrealized_profit",
Help: "bbgo position unrealized profit metrics",
}, []string{"strategy_id", "strategy_type", "symbol"})
Loading