@@ -8,11 +8,14 @@ import (
8
8
"sync"
9
9
"time"
10
10
11
+ "github.com/pkg/errors"
11
12
"github.com/prometheus/client_golang/prometheus"
12
13
"github.com/sirupsen/logrus"
13
14
"go.uber.org/multierr"
14
15
15
16
"github.com/c9s/bbgo/pkg/bbgo"
17
+ "github.com/c9s/bbgo/pkg/exchange"
18
+ maxapi "github.com/c9s/bbgo/pkg/exchange/max/maxapi"
16
19
"github.com/c9s/bbgo/pkg/exchange/retry"
17
20
"github.com/c9s/bbgo/pkg/fixedpoint"
18
21
"github.com/c9s/bbgo/pkg/strategy/common"
@@ -370,7 +373,9 @@ func (s *Strategy) CleanUp(ctx context.Context) error {
370
373
return werr
371
374
}
372
375
373
- func (s * Strategy ) CalculateAndEmitProfit (ctx context.Context ) error {
376
+ func (s * Strategy ) CalculateAndEmitProfitUntilSuccessful (ctx context.Context ) error {
377
+ fromOrderID := s .ProfitStats .FromOrderID
378
+
374
379
historyService , ok := s .ExchangeSession .Exchange .(types.ExchangeTradeHistoryService )
375
380
if ! ok {
376
381
return fmt .Errorf ("exchange %s doesn't support ExchangeTradeHistoryService" , s .ExchangeSession .Exchange .Name ())
@@ -381,6 +386,22 @@ func (s *Strategy) CalculateAndEmitProfit(ctx context.Context) error {
381
386
return fmt .Errorf ("exchange %s doesn't support ExchangeOrderQueryService" , s .ExchangeSession .Exchange .Name ())
382
387
}
383
388
389
+ var op = func () error {
390
+ if err := s .CalculateAndEmitProfit (ctx , historyService , queryService ); err != nil {
391
+ return errors .Wrapf (err , "failed to calculate and emit profit, please check it" )
392
+ }
393
+
394
+ if s .ProfitStats .FromOrderID == fromOrderID {
395
+ return fmt .Errorf ("FromOrderID (%d) is not updated, retry it" , s .ProfitStats .FromOrderID )
396
+ }
397
+
398
+ return nil
399
+ }
400
+
401
+ return retry .GeneralLiteBackoff (ctx , op )
402
+ }
403
+
404
+ func (s * Strategy ) CalculateAndEmitProfit (ctx context.Context , historyService types.ExchangeTradeHistoryService , queryService types.ExchangeOrderQueryService ) error {
384
405
// TODO: pagination for it
385
406
// query the orders
386
407
s .logger .Infof ("query %s closed orders from order id #%d" , s .Symbol , s .ProfitStats .FromOrderID )
@@ -390,6 +411,8 @@ func (s *Strategy) CalculateAndEmitProfit(ctx context.Context) error {
390
411
}
391
412
s .logger .Infof ("there are %d closed orders from order id #%d" , len (orders ), s .ProfitStats .FromOrderID )
392
413
414
+ isMax := exchange .IsMaxExchange (s .ExchangeSession .Exchange )
415
+
393
416
var rounds []Round
394
417
var round Round
395
418
for _ , order := range orders {
@@ -402,9 +425,18 @@ func (s *Strategy) CalculateAndEmitProfit(ctx context.Context) error {
402
425
case types .SideTypeBuy :
403
426
round .OpenPositionOrders = append (round .OpenPositionOrders , order )
404
427
case types .SideTypeSell :
405
- if order .Status != types .OrderStatusFilled {
406
- continue
428
+ if ! isMax {
429
+ if order .Status != types .OrderStatusFilled {
430
+ s .logger .Infof ("take-profit order is %s not filled, so this round is not finished. Skip it" , order .Status )
431
+ continue
432
+ }
433
+ } else {
434
+ if ! maxapi .IsFilledOrderState (maxapi .OrderState (order .OriginalStatus )) {
435
+ s .logger .Infof ("isMax and take-profit order is %s not done or finalizing, so this round is not finished. Skip it" , order .OriginalStatus )
436
+ continue
437
+ }
407
438
}
439
+
408
440
round .TakeProfitOrder = order
409
441
rounds = append (rounds , round )
410
442
round = Round {}
@@ -415,7 +447,7 @@ func (s *Strategy) CalculateAndEmitProfit(ctx context.Context) error {
415
447
416
448
s .logger .Infof ("there are %d rounds from order id #%d" , len (rounds ), s .ProfitStats .FromOrderID )
417
449
for _ , round := range rounds {
418
- debugRoundOrders (s .logger , "calculate" , round )
450
+ debugRoundOrders (s .logger , strconv . FormatInt ( s . ProfitStats . Round , 10 ) , round )
419
451
var roundOrders []types.Order = round .OpenPositionOrders
420
452
roundOrders = append (roundOrders , round .TakeProfitOrder )
421
453
for _ , order := range roundOrders {
0 commit comments