rsmaker: refactor ClosePosition method

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

View File

@ -163,23 +163,6 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{ session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
Interval: s.Interval, Interval: s.Interval,
}) })
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
// Interval: types.Interval12h.String(),
// })
// if s.DefaultBollinger != nil && s.DefaultBollinger.Interval != "" {
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
// Interval: string(s.DefaultBollinger.Interval),
// })
// }
//
// if s.NeutralBollinger != nil && s.NeutralBollinger.Interval != "" {
// session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{
// Interval: string(s.NeutralBollinger.Interval),
// })
// }
// s.SmartStops.Subscribe(session) // s.SmartStops.Subscribe(session)
} }
@ -196,41 +179,10 @@ func (s *Strategy) CurrentPosition() *types.Position {
} }
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,
}
s.Notify("Submitting %s %s order to close position by %v", s.Symbol, side.String(), percentage, submitOrder)
_, err := s.orderExecutor.SubmitOrders(ctx, submitOrder)
if err != nil {
log.WithError(err).Errorf("can not place position close order")
}
return err
} }
// StrategyController // StrategyController
func (s *Strategy) GetStatus() types.StrategyStatus { func (s *Strategy) GetStatus() types.StrategyStatus {
return s.status return s.status
} }