xdepthmaker: add HedgeStrategyBboQueue1 hedge method

This commit is contained in:
c9s 2024-09-24 17:10:50 +08:00
parent 59191cf6bf
commit 60d5126b61
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -594,8 +594,10 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) error {
return s.executeHedgeMarket(ctx, side, quantity)
case HedgeStrategyBboCounterParty1:
return s.executeHedgeBboCounterParty1(ctx, side, quantity)
case HedgeStrategyBboQueue1:
return s.executeHedgeBboQueue1(ctx, side, quantity)
default:
return fmt.Errorf("unsupported hedge strategy %s, please check your configuration", s.HedgeStrategy)
return fmt.Errorf("unsupported or invalid hedge strategy setup %q, please check your configuration", s.HedgeStrategy)
}
}
@ -643,6 +645,50 @@ func (s *Strategy) executeHedgeBboCounterParty1(
})
}
func (s *Strategy) executeHedgeBboQueue1(
ctx context.Context,
side types.SideType,
quantity fixedpoint.Value,
) error {
price := s.lastSourcePrice.Get()
if sourcePrice := s.getSourceBboPrice(side); sourcePrice.Sign() > 0 {
price = sourcePrice
}
if price.IsZero() {
return ErrZeroPrice
}
// adjust quantity according to the balances
account := s.hedgeSession.GetAccount()
quantity = xmaker.AdjustHedgeQuantityWithAvailableBalance(account,
s.hedgeMarket,
side,
quantity,
price)
// truncate quantity for the supported precision
quantity = s.hedgeMarket.TruncateQuantity(quantity)
if quantity.IsZero() {
return ErrZeroQuantity
}
if s.hedgeMarket.IsDustQuantity(quantity, price) {
return ErrDustQuantity
}
// submit order as limit taker
return s.executeHedgeOrder(ctx, types.SubmitOrder{
Market: s.hedgeMarket,
Symbol: s.hedgeMarket.Symbol,
Type: types.OrderTypeLimit,
Price: price,
Side: side,
Quantity: quantity,
})
}
func (s *Strategy) executeHedgeMarket(
ctx context.Context,
side types.SideType,