Skip to content

Commit a57b663

Browse files
committed
add DailyDataTracker util
1 parent be27d32 commit a57b663

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

pkg/strategy/common/fee_budget.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package common
22

33
import (
44
"sync"
5-
"time"
65

76
"github.com/c9s/bbgo/pkg/fixedpoint"
87
"github.com/c9s/bbgo/pkg/types"
8+
"github.com/c9s/bbgo/pkg/util"
99
log "github.com/sirupsen/logrus"
1010
)
1111

@@ -73,20 +73,12 @@ func (f *FeeBudget) HandleTradeUpdate(trade types.Trade) {
7373
}
7474

7575
type State struct {
76-
AccumulatedFeeStartedAt time.Time `json:"accumulatedFeeStartedAt,omitempty"`
77-
AccumulatedFees map[string]fixedpoint.Value `json:"accumulatedFees,omitempty"`
78-
}
76+
util.DailyDataTracker
7977

80-
func (s *State) IsOver24Hours() bool {
81-
return time.Since(s.AccumulatedFeeStartedAt) >= 24*time.Hour
78+
AccumulatedFees map[string]fixedpoint.Value `json:"accumulatedFees,omitempty"`
8279
}
8380

8481
func (s *State) Reset() {
85-
t := time.Now()
86-
dateTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
87-
88-
log.Infof("[State] resetting accumulated started time to: %s", dateTime)
89-
90-
s.AccumulatedFeeStartedAt = dateTime
82+
s.DailyDataTracker.Reset()
9183
s.AccumulatedFees = make(map[string]fixedpoint.Value)
9284
}

pkg/strategy/common/fee_budget_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestFeeBudget(t *testing.T) {
5050
assert.Equal(t, c.expected, feeBudget.IsBudgetAllowed())
5151

5252
// test reset
53-
feeBudget.State.AccumulatedFeeStartedAt = feeBudget.State.AccumulatedFeeStartedAt.Add(-24 * time.Hour)
53+
feeBudget.State.StartedAt = feeBudget.State.StartedAt.Add(-24 * time.Hour)
5454
assert.True(t, feeBudget.IsBudgetAllowed())
5555
}
5656
}

pkg/util/countdown.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package util
2+
3+
import (
4+
"time"
5+
6+
log "github.com/sirupsen/logrus"
7+
)
8+
9+
type DailyDataTracker struct {
10+
StartedAt time.Time `json:"startedAt,omitempty"`
11+
}
12+
13+
func (c *DailyDataTracker) IsOver24Hours() bool {
14+
return time.Since(c.StartedAt) >= 24*time.Hour
15+
}
16+
17+
func (s *DailyDataTracker) Reset() {
18+
t := time.Now()
19+
dateTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
20+
21+
log.Infof("[Countdown] resetting accumulated started time to: %s", dateTime)
22+
23+
s.StartedAt = dateTime
24+
}

0 commit comments

Comments
 (0)