mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 23:05:15 +00:00
FIX: recover even though inital order id is 0
This commit is contained in:
parent
f54fe28142
commit
00352b2a0d
|
@ -2,6 +2,7 @@ package grid2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -825,6 +826,7 @@ func (s *Strategy) newOrderUpdateHandler(ctx context.Context, session *bbgo.Exch
|
||||||
s.handleOrderFilled(o)
|
s.handleOrderFilled(o)
|
||||||
|
|
||||||
// sync the profits to redis
|
// sync the profits to redis
|
||||||
|
s.debugGridProfitStats("OrderUpdate")
|
||||||
bbgo.Sync(ctx, s)
|
bbgo.Sync(ctx, s)
|
||||||
|
|
||||||
s.updateGridNumOfOrdersMetricsWithLock()
|
s.updateGridNumOfOrdersMetricsWithLock()
|
||||||
|
@ -944,6 +946,7 @@ func (s *Strategy) CloseGrid(ctx context.Context) error {
|
||||||
|
|
||||||
defer s.EmitGridClosed()
|
defer s.EmitGridClosed()
|
||||||
|
|
||||||
|
s.debugGridProfitStats("CloseGrid")
|
||||||
bbgo.Sync(ctx, s)
|
bbgo.Sync(ctx, s)
|
||||||
|
|
||||||
// now we can cancel the open orders
|
// now we can cancel the open orders
|
||||||
|
@ -1082,6 +1085,7 @@ func (s *Strategy) openGrid(ctx context.Context, session *bbgo.ExchangeSession)
|
||||||
|
|
||||||
if len(orderIds) > 0 {
|
if len(orderIds) > 0 {
|
||||||
s.GridProfitStats.InitialOrderID = orderIds[0]
|
s.GridProfitStats.InitialOrderID = orderIds[0]
|
||||||
|
s.debugGridProfitStats("openGrid")
|
||||||
bbgo.Sync(ctx, s)
|
bbgo.Sync(ctx, s)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1212,6 +1216,23 @@ func (s *Strategy) debugOrders(desc string, orders []types.Order) {
|
||||||
s.logger.Infof(sb.String())
|
s.logger.Infof(sb.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Strategy) debugGridProfitStats(where string) {
|
||||||
|
if !s.Debug {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := *s.GridProfitStats
|
||||||
|
// ProfitEntries may have too many profits, make it nil to readable
|
||||||
|
stats.ProfitEntries = nil
|
||||||
|
b, err := json.Marshal(stats)
|
||||||
|
if err != nil {
|
||||||
|
s.logger.WithError(err).Errorf("[%s] failed to debug grid profit stats", where)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
s.logger.Infof("[%s] grid profit stats: %s", where, string(b))
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Strategy) debugLog(format string, args ...interface{}) {
|
func (s *Strategy) debugLog(format string, args ...interface{}) {
|
||||||
if !s.Debug {
|
if !s.Debug {
|
||||||
return
|
return
|
||||||
|
@ -1441,10 +1462,12 @@ func (s *Strategy) recoverGridWithOpenOrdersByScanningTrades(ctx context.Context
|
||||||
s.debugOrders("emit filled orders", filledOrders)
|
s.debugOrders("emit filled orders", filledOrders)
|
||||||
|
|
||||||
// 5. emit the filled orders
|
// 5. emit the filled orders
|
||||||
activeOrderBook := s.orderExecutor.ActiveMakerOrders()
|
/*
|
||||||
for _, filledOrder := range filledOrders {
|
activeOrderBook := s.orderExecutor.ActiveMakerOrders()
|
||||||
activeOrderBook.EmitFilled(filledOrder)
|
for _, filledOrder := range filledOrders {
|
||||||
}
|
activeOrderBook.EmitFilled(filledOrder)
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// 6. emit grid ready
|
// 6. emit grid ready
|
||||||
s.EmitGridReady()
|
s.EmitGridReady()
|
||||||
|
@ -2032,6 +2055,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
||||||
s.GridProfitStats.AddTrade(trade)
|
s.GridProfitStats.AddTrade(trade)
|
||||||
})
|
})
|
||||||
orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
||||||
|
s.debugGridProfitStats("OnPositionUpdate")
|
||||||
bbgo.Sync(ctx, s)
|
bbgo.Sync(ctx, s)
|
||||||
})
|
})
|
||||||
orderExecutor.ActiveMakerOrders().OnFilled(s.newOrderUpdateHandler(ctx, session))
|
orderExecutor.ActiveMakerOrders().OnFilled(s.newOrderUpdateHandler(ctx, session))
|
||||||
|
@ -2132,6 +2156,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) startProcess(ctx context.Context, session *bbgo.ExchangeSession) {
|
func (s *Strategy) startProcess(ctx context.Context, session *bbgo.ExchangeSession) {
|
||||||
|
s.debugGridProfitStats("startProcess")
|
||||||
if s.RecoverOrdersWhenStart {
|
if s.RecoverOrdersWhenStart {
|
||||||
// do recover only when triggerPrice is not set and not in the back-test mode
|
// do recover only when triggerPrice is not set and not in the back-test mode
|
||||||
s.logger.Infof("recoverWhenStart is set, trying to recover grid orders...")
|
s.logger.Infof("recoverWhenStart is set, trying to recover grid orders...")
|
||||||
|
@ -2187,19 +2212,12 @@ func (s *Strategy) recoverGridByScanningOrders(ctx context.Context, session *bbg
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) recoverGridByScanningTrades(ctx context.Context, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) recoverGridByScanningTrades(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||||
// no initial order id means we don't need to recover
|
|
||||||
if s.GridProfitStats.InitialOrderID == 0 {
|
|
||||||
s.logger.Debug("new strategy, no need to recover")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
|
openOrders, err := session.Exchange.QueryOpenOrders(ctx, s.Symbol)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger.Infof("found %d open orders left on the %s order book", len(openOrders), s.Symbol)
|
s.logger.Infof("found %d open orders left on the %s order book", len(openOrders), s.Symbol)
|
||||||
|
|
||||||
s.debugLog("recover grid with group id: %d", s.OrderGroupID)
|
s.debugLog("recover grid with group id: %d", s.OrderGroupID)
|
||||||
// filter out the order with the group id belongs to this grid
|
// filter out the order with the group id belongs to this grid
|
||||||
var openOrdersOnGrid []types.Order
|
var openOrdersOnGrid []types.Order
|
||||||
|
@ -2211,6 +2229,13 @@ func (s *Strategy) recoverGridByScanningTrades(ctx context.Context, session *bbg
|
||||||
|
|
||||||
s.logger.Infof("found %d open orders belong to this grid on the %s order book", len(openOrdersOnGrid), s.Symbol)
|
s.logger.Infof("found %d open orders belong to this grid on the %s order book", len(openOrdersOnGrid), s.Symbol)
|
||||||
|
|
||||||
|
// no initial order id means we don't need to recover
|
||||||
|
// 3/31 updated : Find there may be 0 initial order id when the strategy is not strategy, so we need to add more checking on it.
|
||||||
|
if s.GridProfitStats.InitialOrderID == 0 && len(openOrdersOnGrid) == 0 {
|
||||||
|
s.debugLog("new strategy, no need to recover")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
|
historyService, implemented := session.Exchange.(types.ExchangeTradeHistoryService)
|
||||||
if !implemented {
|
if !implemented {
|
||||||
s.logger.Warn("ExchangeTradeHistoryService is not implemented, can not recover grid")
|
s.logger.Warn("ExchangeTradeHistoryService is not implemented, can not recover grid")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user