xmaker: use mutex protected fixedpoint for covered position

This commit is contained in:
c9s 2024-10-15 16:24:35 +08:00
parent d940cde945
commit b137707723
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -198,7 +198,7 @@ type Strategy struct {
// persistence fields // persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"` Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
CoveredPosition fixedpoint.Value `json:"coveredPosition,omitempty" persistence:"covered_position"` CoveredPosition fixedpoint.MutexValue `json:"coveredPosition,omitempty" persistence:"covered_position"`
sourceBook, makerBook *types.StreamOrderBook sourceBook, makerBook *types.StreamOrderBook
activeMakerOrders *bbgo.ActiveOrderBook 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 it's selling, then we should add a positive position
if side == types.SideTypeSell { if side == types.SideTypeSell {
s.CoveredPosition = s.CoveredPosition.Add(quantity) s.CoveredPosition.Add(quantity)
} else { } 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() position := s.Position.GetBase()
uncoverPosition := position.Sub(s.CoveredPosition) coveredPosition := s.CoveredPosition.Get()
uncoverPosition := position.Sub(coveredPosition)
absPos := uncoverPosition.Abs() absPos := uncoverPosition.Abs()
if !s.DisableHedge && absPos.Compare(s.sourceMarket.MinQuantity) > 0 { if !s.DisableHedge && absPos.Compare(s.sourceMarket.MinQuantity) > 0 {
log.Infof("%s base position %v coveredPosition: %v uncoverPosition: %v", log.Infof("%s base position %v coveredPosition: %v uncoverPosition: %v",
s.Symbol, s.Symbol,
position, position,
s.CoveredPosition, coveredPosition,
uncoverPosition, 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 = pricesolver.NewSimplePriceResolver(sourceMarkets)
s.priceSolver.BindStream(s.sourceSession.MarketDataStream) s.priceSolver.BindStream(s.sourceSession.MarketDataStream)
s.sourceSession.UserDataStream.OnTradeUpdate(s.priceSolver.UpdateFromTrade) 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) { s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) {
c := trade.PositionChange() c := trade.PositionChange()
if trade.Exchange == s.sourceSession.ExchangeName { if trade.Exchange == s.sourceSession.ExchangeName {
s.CoveredPosition = s.CoveredPosition.Add(c) s.CoveredPosition.Add(c)
} }
s.ProfitStats.AddTrade(trade) s.ProfitStats.AddTrade(trade)