xmaker: pull out delay hedge logics

This commit is contained in:
c9s 2024-10-15 23:00:09 +08:00
parent 334c868117
commit 0b1773b959
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -1166,6 +1166,27 @@ func AdjustHedgeQuantityWithAvailableBalance(
return market.TruncateQuantity(quantity)
}
func (s *Strategy) canDelayHedge(side types.SideType, pos fixedpoint.Value) bool {
if !s.EnableDelayHedge {
return false
}
signal := s.lastAggregatedSignal.Get()
// if the signal is strong enough, we can delay the hedge and wait for the next tick
if math.Abs(signal) > s.DelayHedgeSignalThreshold {
period, ok := s.getPositionHoldingPeriod(time.Now())
if ok && (signal > 0 && side == types.SideTypeSell) || (signal < 0 && side == types.SideTypeBuy) {
if period < s.MaxDelayHedgeDuration.Duration() {
s.logger.Infof("delay hedge enabled, signal %f is strong enough, waiting for the next tick to hedge %s quantity (max period %s)", signal, pos, s.MaxDelayHedgeDuration.Duration().String())
return true
}
}
}
return false
}
func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
side := types.SideTypeBuy
if pos.IsZero() {
@ -1178,16 +1199,9 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
side = types.SideTypeSell
}
signal := s.lastAggregatedSignal.Get()
// if the signal is strong enough, we can delay the hedge and wait for the next tick
if math.Abs(signal) > s.DelayHedgeSignalThreshold {
if period, ok := s.getPositionHoldingPeriod(time.Now()); ok {
if period < s.MaxDelayHedgeDuration.Duration() {
if s.canDelayHedge(side, pos) {
return
}
}
}
lastPrice := s.lastPrice.Get()
sourceBook := s.sourceBook.CopyDepth(1)