Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dca2: add ttl for persistence and nextRoundPaused flag #1589

Merged
merged 1 commit into from Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}