grid2: use initial grid order id to query closed order history

This commit is contained in:
c9s 2022-12-26 01:35:37 +08:00
parent 6444fd5e03
commit 8af7d6f457
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 26 additions and 7 deletions

View File

@ -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 {

View File

@ -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,9 +1011,13 @@ 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 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 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
}