bollmaker: refactor ClosePosition method

This commit is contained in:
c9s 2022-06-22 13:44:46 +08:00
parent b3160815ff
commit dbc6d4fb44
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -215,34 +215,7 @@ func (s *Strategy) CurrentPosition() *types.Position {
}
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
base := s.Position.GetBase()
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,
}
bbgo.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
_, err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
return err
return s.orderExecutor.ClosePosition(ctx, percentage)
}
// Deprecated: LoadState method is migrated to the persistence struct tag.