@@ -366,6 +366,7 @@ func (s *Strategy) Close(ctx context.Context) error {
366
366
367
367
var err error
368
368
if s .UseCancelAllOrdersApiWhenClose {
369
+ s .logger .Info ("UseCancelAllOrdersApiWhenClose is set, will cancel all orders by cancel all orders API" )
369
370
err = tradingutil .UniversalCancelAllOrders (ctx , s .ExchangeSession .Exchange , s .Symbol , nil )
370
371
} else {
371
372
err = s .OrderExecutor .GracefulCancel (ctx )
@@ -429,7 +430,10 @@ func (s *Strategy) ContinueNextRound() {
429
430
s .nextRoundPaused = false
430
431
}
431
432
432
- func (s * Strategy ) UpdateProfitStatsUntilSuccessful (ctx context.Context ) error {
433
+ // UpdateProfitStatsUntilSuccessful will update the profit stats until duration (duration == 0 means no stop)
434
+ // it will retry for 30 minutes with exponential backoff
435
+ // if it is not successful, it will return an error and the ProfitStats will not be updated into persistence
436
+ func (s * Strategy ) UpdateProfitStatsUntilDuration (ctx context.Context , duration time.Duration ) error {
433
437
var op = func () error {
434
438
if updated , err := s .UpdateProfitStats (ctx ); err != nil {
435
439
return errors .Wrapf (err , "failed to update profit stats, please check it" )
@@ -443,8 +447,8 @@ func (s *Strategy) UpdateProfitStatsUntilSuccessful(ctx context.Context) error {
443
447
// exponential increased interval retry until success
444
448
bo := backoff .NewExponentialBackOff ()
445
449
bo .InitialInterval = 5 * time .Second
446
- bo .MaxInterval = 20 * time .Minute
447
- bo .MaxElapsedTime = 0
450
+ bo .MaxInterval = 5 * time .Minute
451
+ bo .MaxElapsedTime = duration
448
452
449
453
return backoff .Retry (op , backoff .WithContext (bo , ctx ))
450
454
}
@@ -460,6 +464,8 @@ func (s *Strategy) UpdateProfitStats(ctx context.Context) (bool, error) {
460
464
return false , errors .Wrapf (err , "failed to collect finish rounds from #%d" , s .ProfitStats .FromOrderID )
461
465
}
462
466
467
+ s .logger .Infof ("there are %d finished rounds from #%d" , len (rounds ), s .ProfitStats .FromOrderID )
468
+
463
469
var updated bool = false
464
470
for _ , round := range rounds {
465
471
trades , err := s .collector .CollectRoundTrades (ctx , round )
@@ -472,6 +478,11 @@ func (s *Strategy) UpdateProfitStats(ctx context.Context) (bool, error) {
472
478
s .ProfitStats .AddTrade (trade )
473
479
}
474
480
481
+ s .logger .Infof ("profit stats:\n %s" , s .ProfitStats .String ())
482
+
483
+ // emit profit
484
+ s .EmitProfit (s .ProfitStats )
485
+
475
486
// update profit stats FromOrderID to make sure we will not collect duplicated rounds
476
487
for _ , order := range round .TakeProfitOrders {
477
488
if order .OrderID >= s .ProfitStats .FromOrderID {
@@ -486,11 +497,6 @@ func (s *Strategy) UpdateProfitStats(ctx context.Context) (bool, error) {
486
497
bbgo .Sync (ctx , s )
487
498
updated = true
488
499
489
- s .logger .Infof ("profit stats:\n %s" , s .ProfitStats .String ())
490
-
491
- // emit profit
492
- s .EmitProfit (s .ProfitStats )
493
-
494
500
// make profit stats forward to new round
495
501
s .ProfitStats .NewRound ()
496
502
}
0 commit comments