xmaker: refactor and clean up tryArbitrage
This commit is contained in:
parent
3c4e3c0979
commit
a6f364f6ae
|
@ -774,7 +774,7 @@ func (s *Strategy) updateQuote(ctx context.Context) error {
|
||||||
askMarginMetrics.With(s.metricsLabels).Set(quote.AskMargin.Float64())
|
askMarginMetrics.With(s.metricsLabels).Set(quote.AskMargin.Float64())
|
||||||
|
|
||||||
if s.EnableArbitrage {
|
if s.EnableArbitrage {
|
||||||
done, err := s.tryArbitrage(ctx, quote, makerBalances)
|
done, err := s.tryArbitrage(ctx, quote, makerBalances, hedgeBalances)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.WithError(err).Errorf("unable to arbitrage")
|
s.logger.WithError(err).Errorf("unable to arbitrage")
|
||||||
} else if done {
|
} else if done {
|
||||||
|
@ -935,19 +935,33 @@ func aggregatePriceVolumeSliceWithPriceFilter(pvs types.PriceVolumeSlice, filter
|
||||||
}
|
}
|
||||||
|
|
||||||
// tryArbitrage tries to arbitrage between the source and maker exchange
|
// tryArbitrage tries to arbitrage between the source and maker exchange
|
||||||
func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, balances types.BalanceMap) (bool, error) {
|
func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, makerBalances, hedgeBalances types.BalanceMap) (bool, error) {
|
||||||
marginBidPrice := quote.BestBidPrice.Mul(fixedpoint.One.Sub(quote.BidMargin))
|
marginBidPrice := quote.BestBidPrice.Mul(fixedpoint.One.Sub(quote.BidMargin))
|
||||||
marginAskPrice := quote.BestAskPrice.Mul(fixedpoint.One.Add(quote.AskMargin))
|
marginAskPrice := quote.BestAskPrice.Mul(fixedpoint.One.Add(quote.AskMargin))
|
||||||
|
|
||||||
quoteBalance, hasQuote := balances[s.makerMarket.QuoteCurrency]
|
makerBid, makerAsk, ok := s.makerBook.BestBidAndAsk()
|
||||||
baseBalance, hasBase := balances[s.makerMarket.BaseCurrency]
|
if !ok {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
var iocOrders []types.SubmitOrder
|
var iocOrders []types.SubmitOrder
|
||||||
if makerBid, makerAsk, ok := s.makerBook.BestBidAndAsk(); ok {
|
if makerAsk.Price.Compare(marginBidPrice) <= 0 {
|
||||||
if hasQuote && makerAsk.Price.Compare(marginBidPrice) <= 0 {
|
quoteBalance, hasQuote := makerBalances[s.makerMarket.QuoteCurrency]
|
||||||
|
if !hasQuote {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
askPvs := s.makerBook.SideBook(types.SideTypeSell)
|
askPvs := s.makerBook.SideBook(types.SideTypeSell)
|
||||||
sumPv := aggregatePriceVolumeSliceWithPriceFilter(askPvs, marginBidPrice)
|
sumPv := aggregatePriceVolumeSliceWithPriceFilter(askPvs, marginBidPrice)
|
||||||
qty := fixedpoint.Min(quoteBalance.Available.Div(sumPv.Price), sumPv.Volume)
|
qty := fixedpoint.Min(quoteBalance.Available.Div(sumPv.Price), sumPv.Volume)
|
||||||
|
|
||||||
|
if sourceBase, ok := hedgeBalances[s.sourceMarket.BaseCurrency]; ok {
|
||||||
|
qty = fixedpoint.Min(qty, sourceBase.Available)
|
||||||
|
} else {
|
||||||
|
// insufficient hedge base balance for arbitrage
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
iocOrders = append(iocOrders, types.SubmitOrder{
|
iocOrders = append(iocOrders, types.SubmitOrder{
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
Type: types.OrderTypeLimit,
|
Type: types.OrderTypeLimit,
|
||||||
|
@ -957,11 +971,23 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, balances type
|
||||||
TimeInForce: types.TimeInForceIOC,
|
TimeInForce: types.TimeInForceIOC,
|
||||||
})
|
})
|
||||||
|
|
||||||
} else if hasBase && makerBid.Price.Compare(marginAskPrice) >= 0 {
|
} else if makerBid.Price.Compare(marginAskPrice) >= 0 {
|
||||||
|
baseBalance, hasBase := makerBalances[s.makerMarket.BaseCurrency]
|
||||||
|
if !hasBase {
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
bidPvs := s.makerBook.SideBook(types.SideTypeBuy)
|
bidPvs := s.makerBook.SideBook(types.SideTypeBuy)
|
||||||
sumPv := aggregatePriceVolumeSliceWithPriceFilter(bidPvs, marginBidPrice)
|
sumPv := aggregatePriceVolumeSliceWithPriceFilter(bidPvs, marginAskPrice)
|
||||||
qty := fixedpoint.Min(baseBalance.Available, sumPv.Volume)
|
qty := fixedpoint.Min(baseBalance.Available, sumPv.Volume)
|
||||||
|
|
||||||
|
if sourceQuote, ok := hedgeBalances[s.sourceMarket.QuoteCurrency]; ok {
|
||||||
|
qty = fixedpoint.Min(qty, quote.BestAskPrice.Div(sourceQuote.Available))
|
||||||
|
} else {
|
||||||
|
// insufficient hedge quote balance for arbitrage
|
||||||
|
return false, nil
|
||||||
|
}
|
||||||
|
|
||||||
// send ioc order for arbitrage
|
// send ioc order for arbitrage
|
||||||
iocOrders = append(iocOrders, types.SubmitOrder{
|
iocOrders = append(iocOrders, types.SubmitOrder{
|
||||||
Symbol: s.Symbol,
|
Symbol: s.Symbol,
|
||||||
|
@ -985,16 +1011,18 @@ func (s *Strategy) tryArbitrage(ctx context.Context, quote *Quote, balances type
|
||||||
|
|
||||||
defer s.tradeCollector.Process()
|
defer s.tradeCollector.Process()
|
||||||
|
|
||||||
createdOrders, _, err := bbgo.BatchPlaceOrder(ctx, s.makerSession.Exchange, s.makerOrderCreateCallback, formattedOrders...)
|
createdOrders, _, err := bbgo.BatchPlaceOrder(
|
||||||
|
ctx,
|
||||||
|
s.makerSession.Exchange,
|
||||||
|
s.makerOrderCreateCallback,
|
||||||
|
formattedOrders...)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return len(createdOrders) > 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger.Infof("sent arbitrage orders: %+v", createdOrders)
|
s.logger.Infof("sent arbitrage IOC order: %+v", createdOrders)
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
|
||||||
|
|
||||||
return false, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) adjustHedgeQuantityWithAvailableBalance(
|
func (s *Strategy) adjustHedgeQuantityWithAvailableBalance(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user