diff --git a/pkg/strategy/grid2/profit_stats.go b/pkg/strategy/grid2/profit_stats.go index 40001369e..df88af1a7 100644 --- a/pkg/strategy/grid2/profit_stats.go +++ b/pkg/strategy/grid2/profit_stats.go @@ -24,6 +24,7 @@ type GridProfitStats struct { Market types.Market `json:"market,omitempty"` ProfitEntries []*GridProfit `json:"profitEntries,omitempty"` Since *time.Time `json:"since,omitempty"` + InitialOrderID uint64 `json:"initialOrderID"` } func newGridProfitStats(market types.Market) *GridProfitStats { diff --git a/pkg/strategy/grid2/strategy.go b/pkg/strategy/grid2/strategy.go index c60137076..a1a276397 100644 --- a/pkg/strategy/grid2/strategy.go +++ b/pkg/strategy/grid2/strategy.go @@ -3,6 +3,7 @@ package grid2 import ( "context" "fmt" + "sort" "strconv" "sync" "time" @@ -819,10 +820,23 @@ func (s *Strategy) openGrid(ctx context.Context, session *bbgo.ExchangeSession) return err } + var orderIds []uint64 + for _, order := range createdOrders { + orderIds = append(orderIds, order.OrderID) + s.logger.Info(order.String()) } + sort.Slice(orderIds, func(i, j int) bool { + return orderIds[i] < orderIds[j] + }) + + if len(orderIds) > 0 { + s.GridProfitStats.InitialOrderID = orderIds[0] + bbgo.Sync(ctx, s) + } + s.logger.Infof("ALL GRID ORDERS SUBMITTED") return nil } @@ -997,8 +1011,12 @@ func (s *Strategy) recoverGrid(ctx context.Context, historyService types.Exchang _ = lastOrderTime // for MAX exchange we need the order ID to query the closed order history - if oid, ok := findEarliestOrderID(openOrders); ok { - lastOrderID = oid + if s.GridProfitStats != nil && s.GridProfitStats.InitialOrderID > 0 { + lastOrderID = s.GridProfitStats.InitialOrderID + } else { + if oid, ok := findEarliestOrderID(openOrders); ok { + lastOrderID = oid + } } activeOrderBook := s.orderExecutor.ActiveMakerOrders() @@ -1332,14 +1350,14 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. session.MarketDataStream.OnKLineClosed(s.newTakeProfitHandler(ctx, session)) } - session.UserDataStream.OnStart(func() { - if s.TriggerPrice.IsZero() { + // if TriggerPrice is zero, that means we need to open the grid when start up + if s.TriggerPrice.IsZero() { + session.UserDataStream.OnStart(func() { if err := s.openGrid(ctx, session); err != nil { s.logger.WithError(err).Errorf("failed to setup grid orders") } - return - } - }) + }) + } return nil }