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

FEATURE: add ttl for position/grid2.profit_stats persistence #1396

Merged
merged 5 commits into from
Nov 8, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions pkg/strategy/grid2/active_order_recover.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,25 @@ func (s *Strategy) recoverActiveOrdersPeriodically(ctx context.Context) {
exchange: s.session.Exchange,
}

var lastRecoverTime time.Time

for {
select {

case <-ctx.Done():
return

case <-ticker.C:
if err := syncActiveOrders(ctx, opts); err != nil {
log.WithError(err).Errorf("unable to sync active orders")
s.recoverC <- struct{}{}
case <-s.recoverC:
if !time.Now().After(lastRecoverTime.Add(10 * time.Minute)) {
continue
}

case <-s.recoverC:
if err := syncActiveOrders(ctx, opts); err != nil {
log.WithError(err).Errorf("unable to sync active orders")
} else {
lastRecoverTime = time.Now()
}

}
}
}
Expand Down
14 changes: 14 additions & 0 deletions pkg/strategy/grid2/profit_stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ type GridProfitStats struct {
Market types.Market `json:"market,omitempty"`
Since *time.Time `json:"since,omitempty"`
InitialOrderID uint64 `json:"initialOrderID"`

// ttl is the ttl to keep in persistence
ttl time.Duration
}

func newGridProfitStats(market types.Market) *GridProfitStats {
Expand All @@ -40,6 +43,17 @@ func newGridProfitStats(market types.Market) *GridProfitStats {
}
}

func (s *GridProfitStats) SetTTL(ttl time.Duration) {
if ttl.Nanoseconds() <= 0 {
return
}
s.ttl = ttl
}

func (s *GridProfitStats) Expiration() time.Duration {
return s.ttl
}

func (s *GridProfitStats) AddTrade(trade types.Trade) {
if s.TotalFee == nil {
s.TotalFee = make(map[string]fixedpoint.Value)
Expand Down
5 changes: 5 additions & 0 deletions pkg/strategy/grid2/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ type Strategy struct {

GridProfitStats *GridProfitStats `persistence:"grid_profit_stats"`
Position *types.Position `persistence:"position"`
PersistenceTTL types.Duration `json:"persistenceTTL"`

// ExchangeSession is an injection field
ExchangeSession *bbgo.ExchangeSession
Expand Down Expand Up @@ -1835,13 +1836,17 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
s.ProfitSpread = s.Market.TruncatePrice(s.ProfitSpread)
}

s.logger.Infof("persistence ttl: %s", s.PersistenceTTL.Duration())

if s.GridProfitStats == nil {
s.GridProfitStats = newGridProfitStats(s.Market)
}
s.GridProfitStats.SetTTL(s.PersistenceTTL.Duration())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should only call SetTTL when .PersistenceTTL is set and > 0


if s.Position == nil {
s.Position = types.NewPositionFromMarket(s.Market)
}
s.Position.SetTTL(s.PersistenceTTL.Duration())

// initialize and register prometheus metrics
if s.PrometheusLabels != nil {
Expand Down
14 changes: 14 additions & 0 deletions pkg/types/position.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ type Position struct {

// Modify position callbacks
modifyCallbacks []func(baseQty fixedpoint.Value, quoteQty fixedpoint.Value, price fixedpoint.Value)

// ttl is the ttl to keep in persistence
ttl time.Duration
}

func (s *Position) SetTTL(ttl time.Duration) {
if ttl.Nanoseconds() <= 0 {
return
}
s.ttl = ttl
}

func (s *Position) Expiration() time.Duration {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we check the returned Expiration() in the caller? e.g., skip 0 expiration?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need to do it. Because if the value is 0, it means it will not expire anymore.

return s.ttl
}

func (p *Position) CsvHeader() []string {
Expand Down
Loading