mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
fix/order-executor: ClosePosition() works on futures position
This commit is contained in:
parent
34866ce7cc
commit
7a80b90dac
|
@ -407,22 +407,32 @@ func (e *GeneralOrderExecutor) ClosePosition(ctx context.Context, percentage fix
|
||||||
atomic.AddInt64(&e.closing, 1)
|
atomic.AddInt64(&e.closing, 1)
|
||||||
defer atomic.StoreInt64(&e.closing, 0)
|
defer atomic.StoreInt64(&e.closing, 0)
|
||||||
|
|
||||||
// check base balance and adjust the close position order
|
if e.session.Futures { // Futures: Use base qty in e.position
|
||||||
if e.position.IsLong() {
|
submitOrder.Quantity = e.position.GetBase().Abs()
|
||||||
if baseBalance, ok := e.session.Account.Balance(e.position.Market.BaseCurrency); ok {
|
submitOrder.ReduceOnly = true
|
||||||
submitOrder.Quantity = fixedpoint.Min(submitOrder.Quantity, baseBalance.Available)
|
if e.position.IsLong() {
|
||||||
|
submitOrder.Side = types.SideTypeSell
|
||||||
|
} else {
|
||||||
|
submitOrder.Side = types.SideTypeBuy
|
||||||
}
|
}
|
||||||
if submitOrder.Quantity.IsZero() {
|
} else { // Spot and spot margin
|
||||||
return fmt.Errorf("insufficient base balance, can not sell: %+v", submitOrder)
|
// check base balance and adjust the close position order
|
||||||
}
|
if e.position.IsLong() {
|
||||||
} else if e.position.IsShort() {
|
if baseBalance, ok := e.session.Account.Balance(e.position.Market.BaseCurrency); ok {
|
||||||
// TODO: check quote balance here, we also need the current price to validate, need to design.
|
submitOrder.Quantity = fixedpoint.Min(submitOrder.Quantity, baseBalance.Available)
|
||||||
/*
|
|
||||||
if quoteBalance, ok := e.session.Account.Balance(e.position.Market.QuoteCurrency); ok {
|
|
||||||
// AdjustQuantityByMaxAmount(submitOrder.Quantity, quoteBalance.Available)
|
|
||||||
// submitOrder.Quantity = fixedpoint.Min(submitOrder.Quantity,)
|
|
||||||
}
|
}
|
||||||
*/
|
if submitOrder.Quantity.IsZero() {
|
||||||
|
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,)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tagStr := strings.Join(tags, ",")
|
tagStr := strings.Join(tags, ",")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user