mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
feature: use LocalActiveOrderBook for cancelling open orders for strategy controller in support strategy
This commit is contained in:
parent
26a5114182
commit
7b3e369766
|
@ -170,8 +170,9 @@ type Strategy struct {
|
||||||
|
|
||||||
tradeCollector *bbgo.TradeCollector
|
tradeCollector *bbgo.TradeCollector
|
||||||
|
|
||||||
orderStore *bbgo.OrderStore
|
orderStore *bbgo.OrderStore
|
||||||
state *State
|
activeOrders *bbgo.LocalActiveOrderBook
|
||||||
|
state *State
|
||||||
|
|
||||||
triggerEMA *indicator.EWMA
|
triggerEMA *indicator.EWMA
|
||||||
longTermEMA *indicator.EWMA
|
longTermEMA *indicator.EWMA
|
||||||
|
@ -249,6 +250,7 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
|
||||||
}
|
}
|
||||||
|
|
||||||
s.orderStore.Add(createdOrders...)
|
s.orderStore.Add(createdOrders...)
|
||||||
|
s.activeOrders.Add(createdOrders...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -303,6 +305,7 @@ func (s *Strategy) submitOrders(ctx context.Context, orderExecutor bbgo.OrderExe
|
||||||
}
|
}
|
||||||
|
|
||||||
s.orderStore.Add(createdOrders...)
|
s.orderStore.Add(createdOrders...)
|
||||||
|
s.activeOrders.Add(createdOrders...)
|
||||||
s.tradeCollector.Emit()
|
s.tradeCollector.Emit()
|
||||||
return createdOrders, nil
|
return createdOrders, nil
|
||||||
}
|
}
|
||||||
|
@ -393,13 +396,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.Status = types.StrategyStatusRunning
|
s.Status = types.StrategyStatusRunning
|
||||||
|
|
||||||
s.OnSuspend(func() {
|
s.OnSuspend(func() {
|
||||||
var err error = nil
|
|
||||||
// Cancel all order
|
// Cancel all order
|
||||||
for _, order := range s.orderStore.Orders() {
|
if err := s.activeOrders.GracefulCancel(ctx, session.Exchange); err != nil {
|
||||||
err = s.cancelOrder(order.OrderID, ctx, s.orderExecutor)
|
errMsg := "Not all {s.Symbol} orders are cancelled! Please check again."
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
errMsg := "Not all orders are cancelled! Please check again."
|
|
||||||
log.WithError(err).Errorf(errMsg)
|
log.WithError(err).Errorf(errMsg)
|
||||||
s.Notify(errMsg)
|
s.Notify(errMsg)
|
||||||
} else {
|
} else {
|
||||||
|
@ -407,7 +406,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save state
|
// Save state
|
||||||
if err = s.SaveState(); err != nil {
|
if err := s.SaveState(); err != nil {
|
||||||
log.WithError(err).Errorf("can not save state: %+v", s.state)
|
log.WithError(err).Errorf("can not save state: %+v", s.state)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s state is saved.", s.Symbol)
|
log.Infof("%s state is saved.", s.Symbol)
|
||||||
|
@ -476,6 +475,9 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
s.orderStore = bbgo.NewOrderStore(s.Symbol)
|
s.orderStore = bbgo.NewOrderStore(s.Symbol)
|
||||||
s.orderStore.BindStream(session.UserDataStream)
|
s.orderStore.BindStream(session.UserDataStream)
|
||||||
|
|
||||||
|
s.activeOrders = bbgo.NewLocalActiveOrderBook(s.Symbol)
|
||||||
|
s.activeOrders.BindStream(session.UserDataStream)
|
||||||
|
|
||||||
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
|
if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() {
|
||||||
s.trailingStopControl = &TrailingStopControl{
|
s.trailingStopControl = &TrailingStopControl{
|
||||||
symbol: s.Symbol,
|
symbol: s.Symbol,
|
||||||
|
@ -746,9 +748,15 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
|
||||||
|
|
||||||
// Cancel trailing stop order
|
// Cancel trailing stop order
|
||||||
if s.TrailingStopTarget.TrailingStopCallbackRatio.Sign() > 0 {
|
if s.TrailingStopTarget.TrailingStopCallbackRatio.Sign() > 0 {
|
||||||
if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, orderExecutor); err != nil {
|
// Cancel all orders
|
||||||
log.WithError(err).Errorf("Can not cancel the trailing stop order!")
|
if err := s.activeOrders.GracefulCancel(ctx, session.Exchange); err != nil {
|
||||||
|
errMsg := "Not all {s.Symbol} orders are cancelled! Please check again."
|
||||||
|
log.WithError(err).Errorf(errMsg)
|
||||||
|
s.Notify(errMsg)
|
||||||
|
} else {
|
||||||
|
s.Notify("All {s.Symbol} orders are cancelled.")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.trailingStopControl.OrderID = 0
|
s.trailingStopControl.OrderID = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user