mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
dca: use order executor to close position
This commit is contained in:
parent
929ffc3e5e
commit
b3160815ff
|
@ -85,35 +85,7 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
|
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
|
||||||
base := s.Position.GetBase()
|
return s.orderExecutor.ClosePosition(ctx, percentage)
|
||||||
if base.IsZero() {
|
|
||||||
return fmt.Errorf("no opened %s position", s.Position.Symbol)
|
|
||||||
}
|
|
||||||
|
|
||||||
// make it negative
|
|
||||||
quantity := base.Mul(percentage).Abs()
|
|
||||||
side := types.SideTypeBuy
|
|
||||||
if base.Sign() > 0 {
|
|
||||||
side = types.SideTypeSell
|
|
||||||
}
|
|
||||||
|
|
||||||
if quantity.Compare(s.Market.MinQuantity) < 0 {
|
|
||||||
return fmt.Errorf("order quantity %v is too small, less than %v", quantity, s.Market.MinQuantity)
|
|
||||||
}
|
|
||||||
|
|
||||||
submitOrder := types.SubmitOrder{
|
|
||||||
Symbol: s.Symbol,
|
|
||||||
Side: side,
|
|
||||||
Type: types.OrderTypeMarket,
|
|
||||||
Quantity: quantity,
|
|
||||||
Market: s.Market,
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
|
|
||||||
if err != nil {
|
|
||||||
log.WithError(err).Errorf("can not place position close order")
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) InstanceID() string {
|
func (s *Strategy) InstanceID() string {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user