pivotshort: clean up ClosePosition method

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

View File

@ -162,35 +162,16 @@ func (s *Strategy) placeMarketSell(ctx context.Context, quantity fixedpoint.Valu
}) })
} }
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
}
func (s *Strategy) CurrentPosition() *types.Position { func (s *Strategy) CurrentPosition() *types.Position {
return s.Position return s.Position
} }
func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error { func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Value) error {
// Cancel active orders return s.orderExecutor.ClosePosition(ctx, percentage)
_ = s.orderExecutor.GracefulCancel(ctx)
submitOrder := s.Position.NewMarketCloseOrder(percentage) // types.SubmitOrder{
if submitOrder == nil {
return nil
}
if s.session.Margin {
submitOrder.MarginSideEffect = s.Exit.MarginSideEffect
}
bbgo.Notify("Closing %s position by %f", s.Symbol, percentage.Float64())
log.Infof("Closing %s position by %f", s.Symbol, percentage.Float64())
_, err := s.orderExecutor.SubmitOrders(ctx, *submitOrder)
if err != nil {
bbgo.Notify("close %s position error", s.Symbol)
log.WithError(err).Errorf("close %s position error", s.Symbol)
}
return err
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s:%s", ID, s.Symbol)
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {