From 0616c73a887dcc43f0d4096c9971833a28373a56 Mon Sep 17 00:00:00 2001 From: kbearXD Date: Tue, 26 Mar 2024 16:42:29 +0800 Subject: [PATCH 1/2] FEATURE: emit position when position updated and reset --- pkg/strategy/dca2/state.go | 3 +++ pkg/strategy/dca2/strategy.go | 11 +++++++++++ 2 files changed, 14 insertions(+) diff --git a/pkg/strategy/dca2/state.go b/pkg/strategy/dca2/state.go index 79594d1e5..c91fcfa0c 100644 --- a/pkg/strategy/dca2/state.go +++ b/pkg/strategy/dca2/state.go @@ -215,6 +215,9 @@ func (s *Strategy) runTakeProfitReady(ctx context.Context, next State) { // reset position and open new round for profit stats before position opening s.Position.Reset() + // emit position + s.EmitPosition(s.Position) + // store into redis bbgo.Sync(ctx, s) diff --git a/pkg/strategy/dca2/strategy.go b/pkg/strategy/dca2/strategy.go index 348cddb6c..35d377e7f 100644 --- a/pkg/strategy/dca2/strategy.go +++ b/pkg/strategy/dca2/strategy.go @@ -237,6 +237,9 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. // update take profit price here s.updateTakeProfitPrice() + + // emit position update + s.EmitPosition(position) }) s.OrderExecutor.ActiveMakerOrders().OnFilled(func(o types.Order) { @@ -427,6 +430,14 @@ func (s *Strategy) ContinueNextRound() { s.nextRoundPaused = false } +func (s *Strategy) GetTakeProfitPrice() fixedpoint.Value { + if s.Position.Base == 0 { + return fixedpoint.Zero + } + + return s.takeProfitPrice +} + func (s *Strategy) UpdateProfitStatsUntilSuccessful(ctx context.Context) error { var op = func() error { if updated, err := s.UpdateProfitStats(ctx); err != nil { From 63d13d5f7ba3c4add3c7001804ad7042386e8dce Mon Sep 17 00:00:00 2001 From: kbearXD Date: Thu, 11 Apr 2024 16:03:59 +0800 Subject: [PATCH 2/2] use existing TradeCollector's EmitPositionUpdate --- pkg/strategy/dca2/state.go | 2 +- pkg/strategy/dca2/strategy.go | 19 ++++++------------- pkg/strategy/dca2/strategy_callbacks.go | 14 -------------- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/pkg/strategy/dca2/state.go b/pkg/strategy/dca2/state.go index c91fcfa0c..9a7550cfc 100644 --- a/pkg/strategy/dca2/state.go +++ b/pkg/strategy/dca2/state.go @@ -216,7 +216,7 @@ func (s *Strategy) runTakeProfitReady(ctx context.Context, next State) { s.Position.Reset() // emit position - s.EmitPosition(s.Position) + s.OrderExecutor.TradeCollector().EmitPositionUpdate(s.Position) // store into redis bbgo.Sync(ctx, s) diff --git a/pkg/strategy/dca2/strategy.go b/pkg/strategy/dca2/strategy.go index 35d377e7f..ec67d0aad 100644 --- a/pkg/strategy/dca2/strategy.go +++ b/pkg/strategy/dca2/strategy.go @@ -101,8 +101,7 @@ type Strategy struct { // callbacks common.StatusCallbacks - positionCallbacks []func(*types.Position) - profitCallbacks []func(*ProfitStats) + profitCallbacks []func(*ProfitStats) } func (s *Strategy) ID() string { @@ -237,9 +236,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. // update take profit price here s.updateTakeProfitPrice() - - // emit position update - s.EmitPosition(position) }) s.OrderExecutor.ActiveMakerOrders().OnFilled(func(o types.Order) { @@ -283,6 +279,11 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. return } + if s.takeProfitPrice.IsZero() { + s.logger.Warn("take profit price should not be 0 when there is at least one open-position order filled, please check it") + return + } + compRes := kline.Close.Compare(s.takeProfitPrice) // price doesn't hit the take profit price if compRes < 0 { @@ -430,14 +431,6 @@ func (s *Strategy) ContinueNextRound() { s.nextRoundPaused = false } -func (s *Strategy) GetTakeProfitPrice() fixedpoint.Value { - if s.Position.Base == 0 { - return fixedpoint.Zero - } - - return s.takeProfitPrice -} - func (s *Strategy) UpdateProfitStatsUntilSuccessful(ctx context.Context) error { var op = func() error { if updated, err := s.UpdateProfitStats(ctx); err != nil { diff --git a/pkg/strategy/dca2/strategy_callbacks.go b/pkg/strategy/dca2/strategy_callbacks.go index febebd52e..bc460f664 100644 --- a/pkg/strategy/dca2/strategy_callbacks.go +++ b/pkg/strategy/dca2/strategy_callbacks.go @@ -2,20 +2,6 @@ package dca2 -import ( - "github.com/c9s/bbgo/pkg/types" -) - -func (s *Strategy) OnPosition(cb func(*types.Position)) { - s.positionCallbacks = append(s.positionCallbacks, cb) -} - -func (s *Strategy) EmitPosition(position *types.Position) { - for _, cb := range s.positionCallbacks { - cb(position) - } -} - func (s *Strategy) OnProfit(cb func(*ProfitStats)) { s.profitCallbacks = append(s.profitCallbacks, cb) }