Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: [xmaker] fix covered position field #1781

Merged
merged 1 commit into from
Oct 16, 2024
Merged
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
15 changes: 8 additions & 7 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,9 +202,10 @@ 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.MutexValue `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

sourceBook, makerBook *types.StreamOrderBook
activeMakerOrders *bbgo.ActiveOrderBook
Expand Down Expand Up @@ -1284,9 +1285,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.Add(quantity)
s.coveredPosition.Add(quantity)
} else {
s.CoveredPosition.Add(quantity.Neg())
s.coveredPosition.Add(quantity.Neg())
}
}

Expand Down Expand Up @@ -1499,7 +1500,7 @@ func (s *Strategy) hedgeWorker(ctx context.Context) {
s.setPositionStartTime(tt)
}

coveredPosition := s.CoveredPosition.Get()
coveredPosition := s.coveredPosition.Get()
uncoverPosition := position.Sub(coveredPosition)
absPos := uncoverPosition.Abs()

Expand Down Expand Up @@ -1744,7 +1745,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.Add(c)
s.coveredPosition.Add(c)
}

s.ProfitStats.AddTrade(trade)
Expand Down
Loading