From 468b73abb67d59ebd5ac5bc3fe50d8cb8ada084b Mon Sep 17 00:00:00 2001 From: "chiahung.lin" Date: Wed, 3 Jan 2024 13:54:38 +0800 Subject: [PATCH] bbgo.Sync profit stats --- config/dca2.yaml | 11 +++++------ pkg/strategy/dca2/state.go | 7 ++++++- pkg/strategy/dca2/strategy.go | 3 +++ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/config/dca2.yaml b/config/dca2.yaml index 6cb8b6ca9..894afc634 100644 --- a/config/dca2.yaml +++ b/config/dca2.yaml @@ -23,9 +23,8 @@ exchangeStrategies: dca2: symbol: ETHUSDT short: false - budget: 200 - maxOrderNum: 5 - priceDeviation: 1% - takeProfitRatio: 0.2% - coolDownInterval: 3m - circuitBreakLossThreshold: -0.9 + quoteInvestment: "200" + maxOrderCount: 5 + priceDeviation: "0.01" + takeProfitRatio: "0.002" + coolDownInterval: 180 diff --git a/pkg/strategy/dca2/state.go b/pkg/strategy/dca2/state.go index bd8756976..24aa15cef 100644 --- a/pkg/strategy/dca2/state.go +++ b/pkg/strategy/dca2/state.go @@ -3,6 +3,8 @@ package dca2 import ( "context" "time" + + "github.com/c9s/bbgo/pkg/bbgo" ) type State int64 @@ -177,12 +179,13 @@ func (s *Strategy) runOpenPositionOrdersCancelled(ctx context.Context, next Stat s.logger.Info("[State] OpenPositionOrdersCancelled -> TakeProfitReady") } -func (s *Strategy) runTakeProfitReady(_ context.Context, next State) { +func (s *Strategy) runTakeProfitReady(ctx context.Context, next State) { // wait 3 seconds to avoid position not update time.Sleep(3 * time.Second) s.logger.Info("[State] TakeProfitReady - start reseting position and calculate quote investment for next round") s.QuoteInvestment = s.QuoteInvestment.Add(s.Position.Quote) + s.ProfitStats.QuoteInvestment = s.QuoteInvestment // reset position s.Position.Reset() @@ -191,6 +194,8 @@ func (s *Strategy) runTakeProfitReady(_ context.Context, next State) { s.EmitProfit(s.ProfitStats) s.ProfitStats.FinishRound() + bbgo.Sync(ctx, s) + // set the start time of the next round s.startTimeOfNextRound = time.Now().Add(s.CoolDownInterval.Duration()) s.state = WaitToOpenPosition diff --git a/pkg/strategy/dca2/strategy.go b/pkg/strategy/dca2/strategy.go index 5128a5235..f68077ec2 100644 --- a/pkg/strategy/dca2/strategy.go +++ b/pkg/strategy/dca2/strategy.go @@ -159,6 +159,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. s.OrderExecutor.TradeCollector().OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { s.ProfitStats.AddTrade(trade) + bbgo.Sync(ctx, s) }) s.OrderExecutor.ActiveMakerOrders().OnFilled(func(o types.Order) { @@ -206,6 +207,8 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. s.logger.Infof("[DCA] recovered position %s", s.Position.String()) s.logger.Infof("[DCA] recovered quote investment %s", s.QuoteInvestment) s.logger.Infof("[DCA] recovered startTimeOfNextRound %s", s.startTimeOfNextRound) + + bbgo.Sync(ctx, s) } else { s.state = WaitToOpenPosition }