mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-25 08:15:15 +00:00
bollmaker: refactor ClosePosition method
This commit is contained in:
parent
b3160815ff
commit
dbc6d4fb44
|
@ -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.
|
||||
|
|
Loading…
Reference in New Issue
Block a user