Skip to content

Commit

Permalink
xmaker: pull out cancelSpreadMakerOrderAndReturnCoveredPos
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Jan 24, 2025
1 parent ee6da3e commit 071182c
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1447,6 +1447,31 @@ func (s *Strategy) canDelayHedge(hedgeSide types.SideType, pos fixedpoint.Value)
return false
}

func (s *Strategy) cancelSpreadMakerOrderAndReturnCoveredPos(ctx context.Context) {
s.logger.Infof("canceling current spread maker order...")

finalOrder, err := s.SpreadMaker.cancelAndQueryOrder(ctx)
if err != nil {
s.logger.WithError(err).Errorf("spread maker: cancel order error")
}

if finalOrder != nil {
spreadMakerVolumeMetrics.With(s.metricsLabels).Add(finalOrder.ExecutedQuantity.Float64())
spreadMakerQuoteVolumeMetrics.With(s.metricsLabels).Add(finalOrder.ExecutedQuantity.Mul(finalOrder.Price).Float64())

remainingQuantity := finalOrder.GetRemainingQuantity()

s.logger.Infof("returning remaining quantity %f to the covered position", remainingQuantity.Float64())
switch finalOrder.Side {
case types.SideTypeSell:
s.coveredPosition.Sub(remainingQuantity)
case types.SideTypeBuy:
s.coveredPosition.Add(remainingQuantity)

}
}
}

func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
if pos.IsZero() {
return
Expand Down Expand Up @@ -1476,27 +1501,7 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
keptOrder = s.SpreadMaker.shouldKeepOrder(curOrder, now)
if !keptOrder {
s.logger.Infof("canceling current spread maker order...")

finalOrder, err := s.SpreadMaker.cancelAndQueryOrder(ctx)
if err != nil {
s.logger.WithError(err).Errorf("spread maker: cancel order error")
}

if finalOrder != nil {
spreadMakerVolumeMetrics.With(s.metricsLabels).Add(finalOrder.ExecutedQuantity.Float64())
spreadMakerQuoteVolumeMetrics.With(s.metricsLabels).Add(finalOrder.ExecutedQuantity.Mul(finalOrder.Price).Float64())

remainingQuantity := finalOrder.GetRemainingQuantity()

s.logger.Infof("returning remaining quantity %f to the covered position", remainingQuantity.Float64())
switch finalOrder.Side {
case types.SideTypeSell:
s.coveredPosition.Sub(remainingQuantity)
case types.SideTypeBuy:
s.coveredPosition.Add(remainingQuantity)

}
}
s.cancelSpreadMakerOrderAndReturnCoveredPos(ctx)
}
}

Expand All @@ -1520,6 +1525,10 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
}
}
}
} else {
if _, hasOrder := s.SpreadMaker.getOrder(); hasOrder {
s.cancelSpreadMakerOrderAndReturnCoveredPos(ctx)
}
}
}
}
Expand Down

0 comments on commit 071182c

Please sign in to comment.