xmaker: update position metrics when restored

This commit is contained in:
c9s 2024-09-02 15:51:07 +08:00
parent f9b9832fff
commit f30aca1b5a
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 10 additions and 0 deletions

View File

@ -1255,8 +1255,12 @@ func (s *Strategy) CrossRun(
s.Position = types.NewPositionFromMarket(s.makerMarket) s.Position = types.NewPositionFromMarket(s.makerMarket)
s.Position.Strategy = ID s.Position.Strategy = ID
s.Position.StrategyInstanceID = instanceID 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) bbgo.Notify("xmaker: %s position is restored", s.Symbol, s.Position)
if s.ProfitStats == nil { if s.ProfitStats == nil {

View File

@ -656,6 +656,12 @@ func (p *Position) AddTrade(td Trade) (profit fixedpoint.Value, netProfit fixedp
return fixedpoint.Zero, fixedpoint.Zero, false return fixedpoint.Zero, fixedpoint.Zero, false
} }
func (p *Position) UpdateMetrics() {
p.Lock()
p.updateMetrics()
p.Unlock()
}
func (p *Position) updateMetrics() { func (p *Position) updateMetrics() {
// update the position metrics only if the position defines the strategy ID // update the position metrics only if the position defines the strategy ID
if p.StrategyInstanceID == "" || p.Strategy == "" { if p.StrategyInstanceID == "" || p.Strategy == "" {