xdepthmaker: support bbgo counter party 1 hedge method

This commit is contained in:
c9s 2024-09-24 16:33:53 +08:00
parent 566234d3ab
commit 59191cf6bf
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -593,10 +593,10 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) error {
case HedgeStrategyMarket:
return s.executeHedgeMarket(ctx, side, quantity)
case HedgeStrategyBboCounterParty1:
return s.executeHedgeBboCounterParty1(ctx, side, quantity)
default:
return fmt.Errorf("unsupported hedge strategy %s, please check your configuration", s.HedgeStrategy)
}
return nil
}
func (s *Strategy) executeHedgeBboCounterParty1(
@ -629,14 +629,15 @@ func (s *Strategy) executeHedgeBboCounterParty1(
}
if s.hedgeMarket.IsDustQuantity(quantity, price) {
s.logger.Warnf("skip dust quantity: %s @ price %f", quantity.String(), price.Float64())
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,
})
@ -672,7 +673,6 @@ func (s *Strategy) executeHedgeMarket(
}
if s.hedgeMarket.IsDustQuantity(quantity, price) {
s.logger.Warnf("skip dust quantity: %s @ price %f", quantity.String(), price.Float64())
return ErrDustQuantity
}
@ -687,23 +687,25 @@ func (s *Strategy) executeHedgeMarket(
// getSourceBboPrice returns the best bid offering price from the source order book
func (s *Strategy) getSourceBboPrice(side types.SideType) fixedpoint.Value {
bid, ask, ok := s.sourceBook.BestBidAndAsk()
if !ok {
return fixedpoint.Zero
}
switch side {
case types.SideTypeSell:
if bestAsk, ok := s.sourceBook.BestAsk(); ok {
return bestAsk.Price
}
return ask.Price
case types.SideTypeBuy:
if bestBid, ok := s.sourceBook.BestBid(); ok {
return bestBid.Price
return bid.Price
}
}
return fixedpoint.Zero
}
func (s *Strategy) executeHedgeOrder(ctx context.Context, submitOrder types.SubmitOrder) error {
if err := s.HedgeOrderExecutor.GracefulCancel(ctx); err != nil {
s.logger.WithError(err).Warnf("graceful cancel order error")
}
if s.hedgeErrorRateReservation != nil {
if !s.hedgeErrorRateReservation.OK() {
s.logger.Warnf("rate reservation hitted, skip executing hedge order")
@ -717,9 +719,12 @@ func (s *Strategy) executeHedgeOrder(ctx context.Context, submitOrder types.Subm
s.hedgeErrorRateReservation = nil
}
bbgo.Notify("Submitting %s hedge order on %s %s %s", s.HedgeSymbol, s.HedgeExchange,
bbgo.Notify("Submitting hedge %s order on %s %s %s %s @ %s",
submitOrder.Type, s.HedgeSymbol, s.HedgeExchange,
submitOrder.Side.String(),
submitOrder.Quantity.String())
submitOrder.Quantity.String(),
submitOrder.Price.String(),
)
_, err := s.HedgeOrderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil {