mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-21 22:43:52 +00:00
xdepthmaker: simplify hedge
This commit is contained in:
parent
b02580f9f6
commit
61ea41d999
|
@ -435,13 +435,13 @@ func (s *Stream) handleOrderTradeEvent(m OrderTradeEvent) {
|
|||
return
|
||||
}
|
||||
|
||||
debugf("received %s (%s) OrderTradeEvent: %+v", m.instId, m.actionType, m)
|
||||
debugf("received OrderTradeEvent %s (%s): %+v", m.instId, m.actionType, m)
|
||||
|
||||
for _, order := range m.Orders {
|
||||
if order.TradeId == 0 {
|
||||
debugf("%s order update #%d: %+v", m.instId, order.OrderId, order)
|
||||
debugf("received %s order update #%d: %+v", m.instId, order.OrderId, order)
|
||||
} else {
|
||||
debugf("%s order update #%d and trade update #%d: %+v", m.instId, order.OrderId, order, order.TradeId)
|
||||
debugf("received %s order update #%d and trade update #%d: %+v", m.instId, order.OrderId, order.TradeId, order)
|
||||
}
|
||||
|
||||
globalOrder, err := order.toGlobalOrder()
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"github.com/c9s/bbgo/pkg/exchange/retry"
|
||||
"github.com/c9s/bbgo/pkg/fixedpoint"
|
||||
"github.com/c9s/bbgo/pkg/strategy/common"
|
||||
"github.com/c9s/bbgo/pkg/strategy/xmaker"
|
||||
"github.com/c9s/bbgo/pkg/types"
|
||||
"github.com/c9s/bbgo/pkg/util"
|
||||
"github.com/c9s/bbgo/pkg/util/tradingutil"
|
||||
|
@ -570,37 +571,15 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
|
|||
|
||||
// adjust quantity according to the balances
|
||||
account := s.hedgeSession.GetAccount()
|
||||
switch side {
|
||||
|
||||
case types.SideTypeBuy:
|
||||
// check quote quantity
|
||||
if quote, ok := account.Balance(s.hedgeMarket.QuoteCurrency); ok {
|
||||
if quote.Available.Compare(notional) < 0 {
|
||||
// adjust price to higher 0.1%, so that we can ensure that the order can be executed
|
||||
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, lastPrice.Mul(lastPriceModifier), quote.Available)
|
||||
quantity = s.hedgeMarket.TruncateQuantity(quantity)
|
||||
}
|
||||
}
|
||||
|
||||
case types.SideTypeSell:
|
||||
// check quote quantity
|
||||
if base, ok := account.Balance(s.hedgeMarket.BaseCurrency); ok {
|
||||
if base.Available.Compare(quantity) < 0 {
|
||||
quantity = base.Available
|
||||
}
|
||||
}
|
||||
}
|
||||
quantity = xmaker.AdjustHedgeQuantityWithAvailableBalance(account,
|
||||
s.hedgeMarket, side, quantity, lastPrice)
|
||||
|
||||
// truncate quantity for the supported precision
|
||||
quantity = s.hedgeMarket.TruncateQuantity(quantity)
|
||||
|
||||
if notional.Compare(s.hedgeMarket.MinNotional.Mul(minGap)) <= 0 {
|
||||
log.Warnf("the adjusted amount %v is less than minimal notional %v, skipping hedge", notional, s.hedgeMarket.MinNotional)
|
||||
return
|
||||
}
|
||||
|
||||
if quantity.Compare(s.hedgeMarket.MinQuantity.Mul(minGap)) <= 0 {
|
||||
log.Warnf("the adjusted quantity %v is less than minimal quantity %v, skipping hedge", quantity, s.hedgeMarket.MinQuantity)
|
||||
if s.hedgeMarket.IsDustQuantity(quantity, lastPrice) {
|
||||
log.Warnf("skip dust quantity: %s @ price %f", quantity.String(), lastPrice.Float64())
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -613,7 +592,6 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
|
|||
s.hedgeErrorRateReservation = nil
|
||||
}
|
||||
|
||||
log.Infof("submitting %s hedge order %s %v", s.HedgeSymbol, side.String(), quantity)
|
||||
bbgo.Notify("Submitting %s hedge order %s %v", s.HedgeSymbol, side.String(), quantity)
|
||||
|
||||
_, err := s.HedgeOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||
|
|
|
@ -1030,17 +1030,19 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances
|
|||
return true, nil
|
||||
}
|
||||
|
||||
func (s *Strategy) adjustHedgeQuantityWithAvailableBalance(
|
||||
account *types.Account, side types.SideType, quantity, lastPrice fixedpoint.Value,
|
||||
func AdjustHedgeQuantityWithAvailableBalance(
|
||||
account *types.Account,
|
||||
market types.Market,
|
||||
side types.SideType, quantity, lastPrice fixedpoint.Value,
|
||||
) fixedpoint.Value {
|
||||
switch side {
|
||||
|
||||
case types.SideTypeBuy:
|
||||
// check quote quantity
|
||||
if quote, ok := account.Balance(s.sourceMarket.QuoteCurrency); ok {
|
||||
if quote.Available.Compare(s.sourceMarket.MinNotional) < 0 {
|
||||
if quote, ok := account.Balance(market.QuoteCurrency); ok {
|
||||
if quote.Available.Compare(market.MinNotional) < 0 {
|
||||
// adjust price to higher 0.1%, so that we can ensure that the order can be executed
|
||||
availableQuote := s.sourceMarket.TruncateQuoteQuantity(quote.Available)
|
||||
availableQuote := market.TruncateQuoteQuantity(quote.Available)
|
||||
quantity = bbgo.AdjustQuantityByMaxAmount(quantity, lastPrice, availableQuote)
|
||||
|
||||
}
|
||||
|
@ -1048,7 +1050,7 @@ func (s *Strategy) adjustHedgeQuantityWithAvailableBalance(
|
|||
|
||||
case types.SideTypeSell:
|
||||
// check quote quantity
|
||||
if base, ok := account.Balance(s.sourceMarket.BaseCurrency); ok {
|
||||
if base, ok := account.Balance(market.BaseCurrency); ok {
|
||||
if base.Available.Compare(quantity) < 0 {
|
||||
quantity = base.Available
|
||||
}
|
||||
|
@ -1056,7 +1058,7 @@ func (s *Strategy) adjustHedgeQuantityWithAvailableBalance(
|
|||
}
|
||||
|
||||
// truncate the quantity to the supported precision
|
||||
return s.sourceMarket.TruncateQuantity(quantity)
|
||||
return market.TruncateQuantity(quantity)
|
||||
}
|
||||
|
||||
func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
|
||||
|
@ -1094,7 +1096,8 @@ func (s *Strategy) Hedge(ctx context.Context, pos fixedpoint.Value) {
|
|||
return
|
||||
}
|
||||
} else {
|
||||
quantity = s.adjustHedgeQuantityWithAvailableBalance(account, side, quantity, lastPrice)
|
||||
quantity = AdjustHedgeQuantityWithAvailableBalance(
|
||||
account, s.sourceMarket, side, quantity, lastPrice)
|
||||
}
|
||||
|
||||
// truncate quantity for the supported precision
|
||||
|
|
Loading…
Reference in New Issue
Block a user