mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 08:45:16 +00:00
xfunding: implement reduceFuturesPosition
This commit is contained in:
parent
1b5126c9a1
commit
7ba7eb8be7
|
@ -408,7 +408,57 @@ func (s *Strategy) triggerPositionAction(ctx context.Context) {
|
|||
}
|
||||
}
|
||||
|
||||
func (s *Strategy) reduceFuturesPosition(ctx context.Context) {}
|
||||
func (s *Strategy) reduceFuturesPosition(ctx context.Context) {
|
||||
switch s.positionAction {
|
||||
case PositionOpening, PositionNoOp:
|
||||
return
|
||||
}
|
||||
|
||||
futuresBase := s.FuturesPosition.GetBase() // should be negative base quantity here
|
||||
|
||||
if futuresBase.Sign() > 0 {
|
||||
// unexpected error
|
||||
log.Errorf("unexpected futures position (got positive, expecting negative)")
|
||||
return
|
||||
}
|
||||
|
||||
_ = s.futuresOrderExecutor.GracefulCancel(ctx)
|
||||
|
||||
ticker, err := s.futuresSession.Exchange.QueryTicker(ctx, s.Symbol)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("can not query ticker")
|
||||
return
|
||||
}
|
||||
|
||||
if futuresBase.Compare(fixedpoint.Zero) < 0 {
|
||||
orderPrice := ticker.Sell
|
||||
|
||||
orderQuantity := futuresBase.Abs()
|
||||
orderQuantity = fixedpoint.Max(orderQuantity, s.futuresMarket.MinQuantity)
|
||||
orderQuantity = s.futuresMarket.AdjustQuantityByMinNotional(orderQuantity, orderPrice)
|
||||
if s.futuresMarket.IsDustQuantity(orderQuantity, orderPrice) {
|
||||
log.Infof("skip futures order with dust quantity %s, market = %+v", orderQuantity.String(), s.futuresMarket)
|
||||
return
|
||||
}
|
||||
|
||||
createdOrders, err := s.futuresOrderExecutor.SubmitOrders(ctx, types.SubmitOrder{
|
||||
Symbol: s.Symbol,
|
||||
Side: types.SideTypeBuy,
|
||||
Type: types.OrderTypeLimitMaker,
|
||||
Quantity: orderQuantity,
|
||||
Price: orderPrice,
|
||||
Market: s.futuresMarket,
|
||||
ReduceOnly: true,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("can not submit order")
|
||||
return
|
||||
}
|
||||
|
||||
log.Infof("created orders: %+v", createdOrders)
|
||||
}
|
||||
}
|
||||
|
||||
// syncFuturesPosition syncs the futures position with the given spot position
|
||||
func (s *Strategy) syncFuturesPosition(ctx context.Context) {
|
||||
|
@ -422,14 +472,6 @@ func (s *Strategy) syncFuturesPosition(ctx context.Context) {
|
|||
case PositionOpening, PositionNoOp:
|
||||
}
|
||||
|
||||
_ = s.futuresOrderExecutor.GracefulCancel(ctx)
|
||||
|
||||
ticker, err := s.futuresSession.Exchange.QueryTicker(ctx, s.Symbol)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("can not query ticker")
|
||||
return
|
||||
}
|
||||
|
||||
spotBase := s.SpotPosition.GetBase() // should be positive base quantity here
|
||||
futuresBase := s.FuturesPosition.GetBase() // should be negative base quantity here
|
||||
|
||||
|
@ -446,6 +488,14 @@ func (s *Strategy) syncFuturesPosition(ctx context.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
_ = s.futuresOrderExecutor.GracefulCancel(ctx)
|
||||
|
||||
ticker, err := s.futuresSession.Exchange.QueryTicker(ctx, s.Symbol)
|
||||
if err != nil {
|
||||
log.WithError(err).Errorf("can not query ticker")
|
||||
return
|
||||
}
|
||||
|
||||
// compare with the spot position and increase the position
|
||||
quoteValue, err := bbgo.CalculateQuoteQuantity(ctx, s.futuresSession, s.futuresMarket.QuoteCurrency, s.Leverage)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue
Block a user