Skip to content

Commit

Permalink
Merge pull request #1899 from c9s/c9s/xmaker/improve-reverse-signal-chk
Browse files Browse the repository at this point in the history
FIX: improve order store prune chk
  • Loading branch information
c9s authored Feb 3, 2025
2 parents 87ae919 + 2390e07 commit 562ca53
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
16 changes: 15 additions & 1 deletion pkg/core/orderstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,23 @@ func (s *OrderStore) BindStream(stream types.Stream) {
})
}

func (s *OrderStore) Size() (l int) {
s.mu.Lock()
l = len(s.orders)
s.mu.Unlock()
return l
}

func (s *OrderStore) Prune(expiryDuration time.Duration) {
size := s.Size()

// skip prune if the size is less than 100
if size < 100 {
return
}

cutOffTime := time.Now().Add(-expiryDuration)
orders := make(map[uint64]types.Order, len(s.orders))
orders := make(map[uint64]types.Order, size)

s.mu.Lock()
defer s.mu.Unlock()
Expand Down
7 changes: 5 additions & 2 deletions pkg/strategy/xmaker/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -1555,6 +1555,7 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
s.logger.WithError(err).Errorf("unable to place spread maker order")
} else if retOrder != nil {
s.orderStore.Add(*retOrder)
s.orderStore.Prune(8 * time.Hour)

s.logger.Infof("spread maker order placed: #%d %f@%f (%s)", retOrder.OrderID, retOrder.Quantity.Float64(), retOrder.Price.Float64(), retOrder.Status)

Expand All @@ -1570,8 +1571,10 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
}
}
} else if s.SpreadMaker.ReverseSignalOrderCancel {
if _, hasOrder := s.SpreadMaker.getOrder(); hasOrder {
s.cancelSpreadMakerOrderAndReturnCoveredPos(ctx, &s.coveredPosition)
if !isSignalSidePosition(signal, s.Position.Side()) {
if _, hasOrder := s.SpreadMaker.getOrder(); hasOrder {
s.cancelSpreadMakerOrderAndReturnCoveredPos(ctx, &s.coveredPosition)
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,10 @@ func (p *Position) Type() PositionType {
}

func (p *Position) Side() SideType {
if p.Base.Sign() > 0 {
b := p.GetBase()
if b.Sign() > 0 {
return SideTypeBuy
} else if p.Base.Sign() < 0 {
} else if b.Sign() < 0 {
return SideTypeSell
}

Expand Down

0 comments on commit 562ca53

Please sign in to comment.