-
-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1644 from c9s/narumi/fee-budget
REFACTOR: Extract and move FeeBudget from xgap
- Loading branch information
Showing
4 changed files
with
195 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package common | ||
|
||
import ( | ||
"sync" | ||
"time" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type FeeBudget struct { | ||
DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,omitempty"` | ||
State *State `persistence:"state"` | ||
|
||
mu sync.Mutex | ||
} | ||
|
||
func (f *FeeBudget) Initialize() { | ||
if f.State == nil { | ||
f.State = &State{} | ||
f.State.Reset() | ||
} | ||
|
||
if f.State.IsOver24Hours() { | ||
log.Warn("[FeeBudget] state is over 24 hours, resetting to zero") | ||
f.State.Reset() | ||
} | ||
} | ||
|
||
func (f *FeeBudget) IsBudgetAllowed() bool { | ||
if f.DailyFeeBudgets == nil { | ||
return true | ||
} | ||
|
||
if f.State.AccumulatedFees == nil { | ||
return true | ||
} | ||
|
||
if f.State.IsOver24Hours() { | ||
f.State.Reset() | ||
return true | ||
} | ||
|
||
for asset, budget := range f.DailyFeeBudgets { | ||
if fee, ok := f.State.AccumulatedFees[asset]; ok { | ||
if fee.Compare(budget) >= 0 { | ||
log.Warnf("[FeeBudget] accumulative fee %s exceeded the fee budget %s, skipping...", fee.String(), budget.String()) | ||
return false | ||
} | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
func (f *FeeBudget) HandleTradeUpdate(trade types.Trade) { | ||
log.Infof("[FeeBudget] received trade %s", trade.String()) | ||
|
||
if f.State.IsOver24Hours() { | ||
f.State.Reset() | ||
} | ||
|
||
// safe check | ||
if f.State.AccumulatedFees == nil { | ||
f.mu.Lock() | ||
f.State.AccumulatedFees = make(map[string]fixedpoint.Value) | ||
f.mu.Unlock() | ||
} | ||
|
||
f.State.AccumulatedFees[trade.FeeCurrency] = f.State.AccumulatedFees[trade.FeeCurrency].Add(trade.Fee) | ||
log.Infof("[FeeBudget] accumulated fee: %s %s", f.State.AccumulatedFees[trade.FeeCurrency].String(), trade.FeeCurrency) | ||
} | ||
|
||
type State struct { | ||
AccumulatedFeeStartedAt time.Time `json:"accumulatedFeeStartedAt,omitempty"` | ||
AccumulatedFees map[string]fixedpoint.Value `json:"accumulatedFees,omitempty"` | ||
} | ||
|
||
func (s *State) IsOver24Hours() bool { | ||
return time.Since(s.AccumulatedFeeStartedAt) >= 24*time.Hour | ||
} | ||
|
||
func (s *State) Reset() { | ||
t := time.Now() | ||
dateTime := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location()) | ||
|
||
log.Infof("[State] resetting accumulated started time to: %s", dateTime) | ||
|
||
s.AccumulatedFeeStartedAt = dateTime | ||
s.AccumulatedFees = make(map[string]fixedpoint.Value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package common | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/c9s/bbgo/pkg/fixedpoint" | ||
"github.com/c9s/bbgo/pkg/types" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFeeBudget(t *testing.T) { | ||
cases := []struct { | ||
budgets map[string]fixedpoint.Value | ||
trades []types.Trade | ||
expected bool | ||
}{ | ||
{ | ||
budgets: map[string]fixedpoint.Value{ | ||
"MAX": fixedpoint.NewFromFloat(0.5), | ||
}, | ||
trades: []types.Trade{ | ||
{FeeCurrency: "MAX", Fee: fixedpoint.NewFromFloat(0.1)}, | ||
{FeeCurrency: "USDT", Fee: fixedpoint.NewFromFloat(10.0)}, | ||
}, | ||
expected: true, | ||
}, | ||
{ | ||
budgets: map[string]fixedpoint.Value{ | ||
"MAX": fixedpoint.NewFromFloat(0.5), | ||
}, | ||
trades: []types.Trade{ | ||
{FeeCurrency: "MAX", Fee: fixedpoint.NewFromFloat(0.1)}, | ||
{FeeCurrency: "MAX", Fee: fixedpoint.NewFromFloat(0.5)}, | ||
{FeeCurrency: "USDT", Fee: fixedpoint.NewFromFloat(10.0)}, | ||
}, | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
feeBudget := FeeBudget{ | ||
DailyFeeBudgets: c.budgets, | ||
} | ||
feeBudget.Initialize() | ||
|
||
for _, trade := range c.trades { | ||
feeBudget.HandleTradeUpdate(trade) | ||
} | ||
assert.Equal(t, c.expected, feeBudget.IsBudgetAllowed()) | ||
|
||
// test reset | ||
feeBudget.State.AccumulatedFeeStartedAt = feeBudget.State.AccumulatedFeeStartedAt.Add(-24 * time.Hour) | ||
assert.True(t, feeBudget.IsBudgetAllowed()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters