Skip to content

Commit 8fc7c38

Browse files
authored
Merge pull request #1622 from c9s/kbearXD/dca2/emit-position-after-recovery
FEATURE: [dca2] emit position after recovery and refactor
2 parents 845bc7a + 27ff44b commit 8fc7c38

File tree

4 files changed

+25
-45
lines changed

4 files changed

+25
-45
lines changed

pkg/strategy/dca2/background_runner.go

-35
This file was deleted.

pkg/strategy/dca2/strategy.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,9 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
326326
s.logger.Infof("profit stats %s", s.ProfitStats.String())
327327
s.logger.Infof("startTimeOfNextRound %s", s.startTimeOfNextRound)
328328

329+
// emit position after recovery
330+
s.OrderExecutor.TradeCollector().EmitPositionUpdate(s.Position)
331+
329332
s.updateTakeProfitPrice()
330333

331334
// store persistence
@@ -340,7 +343,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
340343
})
341344
})
342345

343-
go s.runBackgroundTask(ctx)
346+
go s.syncPeriodically(ctx)
344347

345348
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
346349
defer wg.Done()

pkg/strategy/dca2/active_order_recover.go pkg/strategy/dca2/sync.go

+17-9
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,38 @@ import (
44
"context"
55
"time"
66

7+
"github.com/c9s/bbgo/pkg/bbgo"
78
"github.com/c9s/bbgo/pkg/exchange/retry"
89
"github.com/c9s/bbgo/pkg/strategy/common"
910
"github.com/c9s/bbgo/pkg/util"
1011
)
1112

12-
func (s *Strategy) recoverPeriodically(ctx context.Context) {
13-
s.logger.Info("monitor and recover periodically")
14-
interval := util.MillisecondsJitter(10*time.Minute, 5*60*1000)
15-
ticker := time.NewTicker(interval)
16-
defer ticker.Stop()
13+
func (s *Strategy) syncPeriodically(ctx context.Context) {
14+
s.logger.Info("sync periodically")
15+
16+
// sync persistence
17+
syncPersistenceTicker := time.NewTicker(1 * time.Hour)
18+
defer syncPersistenceTicker.Stop()
19+
20+
// sync active orders
21+
syncActiveOrdersTicker := time.NewTicker(util.MillisecondsJitter(10*time.Minute, 5*60*1000))
22+
defer syncActiveOrdersTicker.Stop()
1723

1824
for {
1925
select {
2026
case <-ctx.Done():
2127
return
22-
case <-ticker.C:
23-
if err := s.recoverActiveOrders(ctx); err != nil {
24-
s.logger.WithError(err).Warn(err, "failed to recover active orders")
28+
case <-syncPersistenceTicker.C:
29+
bbgo.Sync(ctx, s)
30+
case <-syncActiveOrdersTicker.C:
31+
if err := s.syncActiveOrders(ctx); err != nil {
32+
s.logger.WithError(err).Warn(err, "failed to sync active orders")
2533
}
2634
}
2735
}
2836
}
2937

30-
func (s *Strategy) recoverActiveOrders(ctx context.Context) error {
38+
func (s *Strategy) syncActiveOrders(ctx context.Context) error {
3139
s.logger.Info("recover active orders...")
3240
openOrders, err := retry.QueryOpenOrdersUntilSuccessfulLite(ctx, s.ExchangeSession.Exchange, s.Symbol)
3341
if err != nil {

pkg/strategy/dca2/take_profit.go

+4
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ import (
1212
func (s *Strategy) placeTakeProfitOrders(ctx context.Context) error {
1313
s.logger.Info("start placing take profit orders")
1414
currentRound, err := s.collector.CollectCurrentRound(ctx)
15+
if err != nil {
16+
return errors.Wrap(err, "failed to place the take-profit order when collecting current round")
17+
}
18+
1519
if currentRound.TakeProfitOrder.OrderID != 0 {
1620
return fmt.Errorf("there is a take-profit order before placing the take-profit order, please check it")
1721
}

0 commit comments

Comments
 (0)