mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
fix
This commit is contained in:
parent
243b90aaf9
commit
ccb7308263
|
@ -27,23 +27,29 @@ func (s *Strategy) initializeRecoverCh() bool {
|
||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
alreadyInitialize := false
|
isInitialize := false
|
||||||
|
|
||||||
if s.activeOrdersRecoverCh == nil {
|
if s.activeOrdersRecoverC == nil {
|
||||||
s.logger.Info("initialize recover channel")
|
s.logger.Info("initializing recover channel")
|
||||||
s.activeOrdersRecoverCh = make(chan struct{}, 1)
|
s.activeOrdersRecoverC = make(chan struct{}, 1)
|
||||||
} else {
|
} else {
|
||||||
s.logger.Info("already initialize recover channel, trigger active orders recover")
|
s.logger.Info("recover channel is already initialized, trigger active orders recover")
|
||||||
alreadyInitialize = true
|
isInitialize = true
|
||||||
s.activeOrdersRecoverCh <- struct{}{}
|
|
||||||
|
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) {
|
func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
||||||
// every time we activeOrdersRecoverCh receive signal, do active orders recover
|
// every time we activeOrdersRecoverC receive signal, do active orders recover
|
||||||
if alreadyInitialize := s.initializeRecoverCh(); alreadyInitialize {
|
if isInitialize := s.initializeRecoverCh(); isInitialize {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +78,7 @@ func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
|
||||||
log.WithError(err).Errorf("unable to sync active orders")
|
log.WithError(err).Errorf("unable to sync active orders")
|
||||||
}
|
}
|
||||||
|
|
||||||
case <-s.activeOrdersRecoverCh:
|
case <-s.activeOrdersRecoverC:
|
||||||
if err := syncActiveOrders(ctx, opts); err != nil {
|
if err := syncActiveOrders(ctx, opts); err != nil {
|
||||||
log.WithError(err).Errorf("unable to sync active orders")
|
log.WithError(err).Errorf("unable to sync active orders")
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package grid2
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"sort"
|
"sort"
|
||||||
|
@ -205,7 +204,7 @@ type Strategy struct {
|
||||||
tradingCtx, writeCtx context.Context
|
tradingCtx, writeCtx context.Context
|
||||||
cancelWrite context.CancelFunc
|
cancelWrite context.CancelFunc
|
||||||
|
|
||||||
activeOrdersRecoverCh chan struct{}
|
activeOrdersRecoverC chan struct{}
|
||||||
|
|
||||||
// this ensures that bbgo.Sync to lock the object
|
// this ensures that bbgo.Sync to lock the object
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
@ -899,7 +898,6 @@ 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()
|
||||||
|
@ -1018,7 +1016,6 @@ 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
|
||||||
|
@ -1171,7 +1168,6 @@ 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1272,23 +1268,6 @@ func (s *Strategy) debugOrders(desc string, orders []types.Order) {
|
||||||
s.logger.Infof(sb.String())
|
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{}) {
|
func (s *Strategy) debugLog(format string, args ...interface{}) {
|
||||||
if !s.Debug {
|
if !s.Debug {
|
||||||
return
|
return
|
||||||
|
@ -1883,7 +1862,6 @@ 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))
|
||||||
|
@ -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 {
|
func (s *Strategy) startProcess(ctx context.Context, session *bbgo.ExchangeSession) error {
|
||||||
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...")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user