From 17368b9585793aff75e05889762e8d80119e537e Mon Sep 17 00:00:00 2001 From: Newtoniano <81423796+Newtoniano@users.noreply.github.com> Date: Wed, 20 Mar 2024 18:48:08 +0100 Subject: [PATCH] add short position close logic --- pkg/bbgo/order_executor_general.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/pkg/bbgo/order_executor_general.go b/pkg/bbgo/order_executor_general.go index 3ebec372f0..8de07c9e3e 100644 --- a/pkg/bbgo/order_executor_general.go +++ b/pkg/bbgo/order_executor_general.go @@ -517,13 +517,17 @@ func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fix return fmt.Errorf("insufficient base balance, can not sell: %+v", submitOrder) } } else if e.position.IsShort() { - // TODO: check quote balance here, we also need the current price to validate, need to design. - /* - if quoteBalance, ok := e.session.Account.Balance(e.position.Market.QuoteCurrency); ok { - // AdjustQuantityByMaxAmount(submitOrder.Quantity, quoteBalance.Available) - // submitOrder.Quantity = fixedpoint.Min(submitOrder.Quantity,) + if quoteBalance, ok := e.session.Account.Balance(e.position.Market.QuoteCurrency); ok { + ticker, err := e.session.Exchange.QueryTicker(ctx, e.position.Symbol) + if err != nil { + return err } - */ + currentPrice := ticker.Sell + submitOrder.Quantity = AdjustQuantityByMaxAmount(submitOrder.Quantity, currentPrice, quoteBalance.Available) + if submitOrder.Quantity.IsZero() { + return fmt.Errorf("insufficient quote balance, can not buy: %+v", submitOrder) + } + } } }