From b137707723ecd0e5fe7a56948ea7419f9d172f4e Mon Sep 17 00:00:00 2001 From: c9s Date: Tue, 15 Oct 2024 16:24:35 +0800 Subject: [PATCH] xmaker: use mutex protected fixedpoint for covered position --- pkg/strategy/xmaker/strategy.go | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index e5c2aa37c..a8ecaa0c6 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -196,9 +196,9 @@ type Strategy struct { CircuitBreaker *circuitbreaker.BasicCircuitBreaker `json:"circuitBreaker"` // persistence fields - Position *types.Position `json:"position,omitempty" persistence:"position"` - ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` - CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"` + Position *types.Position `json:"position,omitempty" persistence:"position"` + ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` + CoveredPosition fixedpoint.MutexValue `json:"coveredPosition,omitempty" persistence:"covered_position"` sourceBook, makerBook *types.StreamOrderBook activeMakerOrders *bbgo.ActiveOrderBook @@ -1221,9 +1221,9 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) { // if it's selling, then we should add a positive position if side == types.SideTypeSell { - s.CoveredPosition = s.CoveredPosition.Add(quantity) + s.CoveredPosition.Add(quantity) } else { - s.CoveredPosition = s.CoveredPosition.Add(quantity.Neg()) + s.CoveredPosition.Add(quantity.Neg()) } } @@ -1420,13 +1420,14 @@ func (s *Strategy) hedgeWorker(ctx context.Context) { position := s.Position.GetBase() - uncoverPosition := position.Sub(s.CoveredPosition) + coveredPosition := s.CoveredPosition.Get() + uncoverPosition := position.Sub(coveredPosition) absPos := uncoverPosition.Abs() if !s.DisableHedge && absPos.Compare(s.sourceMarket.MinQuantity) > 0 { log.Infof("%s base position %v coveredPosition: %v uncoverPosition: %v", s.Symbol, position, - s.CoveredPosition, + coveredPosition, uncoverPosition, ) @@ -1529,12 +1530,6 @@ func (s *Strategy) CrossRun( } } - if s.CoveredPosition.IsZero() { - if s.state != nil && !s.CoveredPosition.IsZero() { - s.CoveredPosition = s.state.CoveredPosition - } - } - s.priceSolver = pricesolver.NewSimplePriceResolver(sourceMarkets) s.priceSolver.BindStream(s.sourceSession.MarketDataStream) s.sourceSession.UserDataStream.OnTradeUpdate(s.priceSolver.UpdateFromTrade) @@ -1663,7 +1658,7 @@ func (s *Strategy) CrossRun( s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { c := trade.PositionChange() if trade.Exchange == s.sourceSession.ExchangeName { - s.CoveredPosition = s.CoveredPosition.Add(c) + s.CoveredPosition.Add(c) } s.ProfitStats.AddTrade(trade)