mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
fix
This commit is contained in:
parent
243b90aaf9
commit
ccb7308263
|
@ -27,23 +27,29 @@ func (s *Strategy) initializeRecoverCh() bool {
|
|||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
alreadyInitialize := false
|
||||
isInitialize := false
|
||||
|
||||
if s.activeOrdersRecoverCh == nil {
|
||||
s.logger.Info("initialize recover channel")
|
||||
s.activeOrdersRecoverCh = make(chan struct{}, 1)
|
||||
if s.activeOrdersRecoverC == nil {
|
||||
s.logger.Info("initializing recover channel")
|
||||
s.activeOrdersRecoverC = make(chan struct{}, 1)
|
||||
} else {
|
||||
s.logger.Info("already initialize recover channel, trigger active orders recover")
|
||||
alreadyInitialize = true
|
||||
s.activeOrdersRecoverCh <- struct{}{}
|
||||
s.logger.Info("recover channel is already initialized, trigger active orders recover")
|
||||
isInitialize = true
|
||||
|
||||
select {
|
||||
case s.activeOrdersRecoverC <- struct{}{}:
|
||||
s.logger.Info("trigger active orders recover")
|
||||
default:
|
||||
s.logger.Info("activeOrdersRecoverC is full")
|
||||
}
|
||||
}
|
||||
|
||||
return alreadyInitialize
|
||||
return isInitialize
|
||||
}
|
||||
|
||||
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
||||
// every time we activeOrdersRecoverCh receive signal, do active orders recover
|
||||
if alreadyInitialize := s.initializeRecoverCh(); alreadyInitialize {
|
||||
// every time we activeOrdersRecoverC receive signal, do active orders recover
|
||||
if isInitialize := s.initializeRecoverCh(); isInitialize {
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -72,7 +78,7 @@ func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
|||
log.WithError(err).Errorf("unable to sync active orders")
|
||||
}
|
||||
|
||||
case <-s.activeOrdersRecoverCh:
|
||||
case <-s.activeOrdersRecoverC:
|
||||
if err := syncActiveOrders(ctx, opts); err != nil {
|
||||
log.WithError(err).Errorf("unable to sync active orders")
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package grid2
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"math"
|
||||
"sort"
|
||||
|
@ -205,7 +204,7 @@ type Strategy struct {
|
|||
tradingCtx, writeCtx context.Context
|
||||
cancelWrite context.CancelFunc
|
||||
|
||||
activeOrdersRecoverCh chan struct{}
|
||||
activeOrdersRecoverC chan struct{}
|
||||
|
||||
// this ensures that bbgo.Sync to lock the object
|
||||
sync.Mutex
|
||||
|
@ -899,7 +898,6 @@ func (s *Strategy) newOrderUpdateHandler(ctx context.Context, session *bbgo.Exch
|
|||
s.handleOrderFilled(o)
|
||||
|
||||
// sync the profits to redis
|
||||
s.debugGridProfitStats("OrderUpdate")
|
||||
bbgo.Sync(ctx, s)
|
||||
|
||||
s.updateGridNumOfOrdersMetricsWithLock()
|
||||
|
@ -1018,7 +1016,6 @@ func (s *Strategy) CloseGrid(ctx context.Context) error {
|
|||
|
||||
defer s.EmitGridClosed()
|
||||
|
||||
s.debugGridProfitStats("CloseGrid")
|
||||
bbgo.Sync(ctx, s)
|
||||
|
||||
// now we can cancel the open orders
|
||||
|
@ -1171,7 +1168,6 @@ func (s *Strategy) openGrid(ctx context.Context, session *bbgo.ExchangeSession)
|
|||
|
||||
if len(orderIds) > 0 {
|
||||
s.GridProfitStats.InitialOrderID = orderIds[0]
|
||||
s.debugGridProfitStats("openGrid")
|
||||
bbgo.Sync(ctx, s)
|
||||
}
|
||||
|
||||
|
@ -1272,23 +1268,6 @@ func (s *Strategy) debugOrders(desc string, orders []types.Order) {
|
|||
s.logger.Infof(sb.String())
|
||||
}
|
||||
|
||||
func (s *Strategy) debugGridProfitStats(trigger 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", trigger)
|
||||
return
|
||||
}
|
||||
|
||||
s.logger.Infof("trigger %s => grid profit stats : %s", trigger, string(b))
|
||||
}
|
||||
|
||||
func (s *Strategy) debugLog(format string, args ...interface{}) {
|
||||
if !s.Debug {
|
||||
return
|
||||
|
@ -1883,7 +1862,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
|||
s.GridProfitStats.AddTrade(trade)
|
||||
})
|
||||
orderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
|
||||
s.debugGridProfitStats("OnPositionUpdate")
|
||||
bbgo.Sync(ctx, s)
|
||||
})
|
||||
orderExecutor.ActiveMakerOrders().OnFilled(s.newOrderUpdateHandler(ctx, session))
|
||||
|
@ -1987,7 +1965,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
|||
}
|
||||
|
||||
func (s *Strategy) startProcess(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||
s.debugGridProfitStats("startProcess")
|
||||
if s.RecoverOrdersWhenStart {
|
||||
// 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...")
|
||||
|
|
Loading…
Reference in New Issue
Block a user