gracefully cancel order before closing position

This commit is contained in:
c9s 2022-06-09 11:46:14 +08:00
parent 1d8cd2d604
commit 1bfc125a52
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -227,18 +227,23 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
if kline.Symbol != s.Symbol || kline.Interval != types.Interval1m {
return
}
// TODO: handle stop loss here, faster than closed kline
if canClosePosition(s.Position, kline.Close) {
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
log.WithError(err).Errorf("graceful cancel order error")
}
// calculate return rate
R := kline.Close.Sub(s.Position.AverageCost).Div(s.Position.AverageCost)
if R.Compare(s.Exit.StopLossPercentage) > 0 {
// SL
s.Notify("%s SL triggered", s.Symbol)
s.ClosePosition(ctx, fixedpoint.One)
s.ClosePosition(ctx, fixedpoint.One.Div(fixedpoint.NewFromFloat(0.99)))
} else if R.Compare(s.Exit.TakeProfitPercentage.Neg()) < 0 && kline.GetLowerShadowRatio().Compare(s.Exit.LowerShadowRatio) > 0 {
// TP
s.Notify("%s TP triggered", s.Symbol)
s.ClosePosition(ctx, fixedpoint.One)
s.ClosePosition(ctx, fixedpoint.One.Div(fixedpoint.NewFromFloat(0.99)))
}
}