From f30aca1b5a0079a6d776f7b04ea7808a3af69b39 Mon Sep 17 00:00:00 2001 From: c9s Date: Mon, 2 Sep 2024 15:51:07 +0800 Subject: [PATCH] xmaker: update position metrics when restored --- pkg/strategy/xmaker/strategy.go | 4 ++++ pkg/types/position.go | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index caa81bc0b..d34894791 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -1255,8 +1255,12 @@ func (s *Strategy) CrossRun( s.Position = types.NewPositionFromMarket(s.makerMarket) s.Position.Strategy = ID s.Position.StrategyInstanceID = instanceID + } else { + s.Position.Strategy = ID + s.Position.StrategyInstanceID = instanceID } + s.Position.UpdateMetrics() bbgo.Notify("xmaker: %s position is restored", s.Symbol, s.Position) if s.ProfitStats == nil { diff --git a/pkg/types/position.go b/pkg/types/position.go index e232cbcbe..26dc8bb36 100644 --- a/pkg/types/position.go +++ b/pkg/types/position.go @@ -656,6 +656,12 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp return fixedpoint.Zero, fixedpoint.Zero, false } +func (p *Position) UpdateMetrics() { + p.Lock() + p.updateMetrics() + p.Unlock() +} + func (p *Position) updateMetrics() { // update the position metrics only if the position defines the strategy ID if p.StrategyInstanceID == "" || p.Strategy == "" {