Skip to content

Commit

Permalink
dca2: add ttl for persistence and nextRoundPaused flag
Browse files Browse the repository at this point in the history
  • Loading branch information
kbearXD committed Mar 18, 2024
1 parent a23c476 commit a11cddb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
7 changes: 6 additions & 1 deletion pkg/strategy/dca2/state.go
Expand Up @@ -137,8 +137,13 @@ func (s *Strategy) triggerNextState() {
}

func (s *Strategy) runWaitToOpenPositionState(ctx context.Context, next State) {
s.logger.Info("[State] WaitToOpenPosition - check startTimeOfNextRound")
if s.nextRoundPaused {
s.logger.Info("[State] WaitToOpenPosition - nextRoundPaused is set")
return
}

if time.Now().Before(s.startTimeOfNextRound) {
s.logger.Infof("[State] WaitToOpenPosition - before the startTimeOfNextRound %s", s.startTimeOfNextRound.String())
return
}

Expand Down
40 changes: 30 additions & 10 deletions pkg/strategy/dca2/strategy.go
Expand Up @@ -44,8 +44,9 @@ type advancedOrderCancelApi interface {

//go:generate callbackgen -type Strateg
type Strategy struct {
Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
PersistenceTTL types.Duration `json:"persistenceTTL"`

Environment *bbgo.Environment
ExchangeSession *bbgo.ExchangeSession
Expand Down Expand Up @@ -87,11 +88,12 @@ type Strategy struct {

// private field
mu sync.Mutex
takeProfitPrice fixedpoint.Value
startTimeOfNextRound time.Time
nextStateC chan State
state State
roundCollector *RoundCollector
takeProfitPrice fixedpoint.Value
startTimeOfNextRound time.Time
nextRoundPaused bool

// callbacks
common.StatusCallbacks
Expand Down Expand Up @@ -164,6 +166,8 @@ func (s *Strategy) newPrometheusLabels() prometheus.Labels {
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
instanceID := s.InstanceID()
s.ExchangeSession = session

s.logger.Infof("persistence ttl: %s", s.PersistenceTTL.Duration())
if s.ProfitStats == nil {
s.ProfitStats = newProfitStats(s.Market, s.QuoteInvestment)
}
Expand All @@ -172,6 +176,16 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
s.Position = types.NewPositionFromMarket(s.Market)
}

// if dev mode is on and it's not a new strategy
if s.DevMode != nil && s.DevMode.Enabled && !s.DevMode.IsNewAccount {
s.ProfitStats = newProfitStats(s.Market, s.QuoteInvestment)
s.Position = types.NewPositionFromMarket(s.Market)
}

// set ttl for persistence
s.Position.SetTTL(s.PersistenceTTL.Duration())
s.ProfitStats.SetTTL(s.PersistenceTTL.Duration())

// round collector
s.roundCollector = NewRoundCollector(s.logger, s.Symbol, s.OrderGroupID, s.ExchangeSession.Exchange)
if s.roundCollector == nil {
Expand All @@ -187,12 +201,6 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
// prometheus labels
baseLabels = s.newPrometheusLabels()

// if dev mode is on and it's not a new strategy
if s.DevMode != nil && s.DevMode.Enabled && !s.DevMode.IsNewAccount {
s.ProfitStats = newProfitStats(s.Market, s.QuoteInvestment)
s.Position = types.NewPositionFromMarket(s.Market)
}

s.Position.Strategy = ID
s.Position.StrategyInstanceID = instanceID

Expand Down Expand Up @@ -380,6 +388,15 @@ func (s *Strategy) CleanUp(ctx context.Context) error {
return werr
}

// PauseNextRound will stop openning open-position orders at the next round
func (s *Strategy) PauseNextRound() {
s.nextRoundPaused = true
}

func (s *Strategy) ContinueNextRound() {
s.nextRoundPaused = false
}

func (s *Strategy) UpdateProfitStatsUntilSuccessful(ctx context.Context) error {
var op = func() error {
if updated, err := s.UpdateProfitStats(ctx); err != nil {
Expand Down Expand Up @@ -456,4 +473,7 @@ func (s *Strategy) updateNumOfOrdersMetrics(ctx context.Context) {

// update active orders metrics
metricsNumOfActiveOrders.With(baseLabels).Set(float64(s.OrderExecutor.ActiveMakerOrders().NumOfOrders()))

// set persistence
bbgo.Sync(ctx, s)
}

0 comments on commit a11cddb

Please sign in to comment.