bbgo_origin/pkg/strategy/common/fee_budget.go

74 lines
1.7 KiB
Go
Raw Normal View History

package common
import (
"sync"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
2024-06-18 10:40:50 +00:00
"github.com/c9s/bbgo/pkg/util"
log "github.com/sirupsen/logrus"
)
type FeeBudget struct {
DailyFeeBudgets map[string]fixedpoint.Value `json:"dailyFeeBudgets,omitempty"`
2024-06-18 10:40:50 +00:00
DailyFeeTracker *util.DailyDataTracker `persistence:"dailyFeeTracker"`
mu sync.Mutex
}
func (f *FeeBudget) Initialize() {
2024-06-18 10:40:50 +00:00
if f.DailyFeeTracker == nil {
f.DailyFeeTracker = &util.DailyDataTracker{}
f.DailyFeeTracker.Reset()
}
2024-06-18 10:40:50 +00:00
if f.DailyFeeTracker.IsOver24Hours() {
log.Warn("[FeeBudget] state is over 24 hours, resetting to zero")
2024-06-18 10:40:50 +00:00
f.DailyFeeTracker.Reset()
}
}
func (f *FeeBudget) IsBudgetAllowed() bool {
if f.DailyFeeBudgets == nil {
return true
}
2024-06-18 10:40:50 +00:00
if f.DailyFeeTracker.Data == nil {
return true
}
2024-06-18 10:40:50 +00:00
if f.DailyFeeTracker.IsOver24Hours() {
f.DailyFeeTracker.Reset()
return true
}
for asset, budget := range f.DailyFeeBudgets {
2024-06-18 10:40:50 +00:00
if fee, ok := f.DailyFeeTracker.Data[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())
2024-06-18 10:40:50 +00:00
if f.DailyFeeTracker.IsOver24Hours() {
f.DailyFeeTracker.Reset()
}
// safe check
2024-06-18 10:40:50 +00:00
if f.DailyFeeTracker.Data == nil {
f.mu.Lock()
2024-06-18 10:40:50 +00:00
f.DailyFeeTracker.Data = make(map[string]fixedpoint.Value)
f.mu.Unlock()
}
2024-06-18 10:40:50 +00:00
f.DailyFeeTracker.Data[trade.FeeCurrency] = f.DailyFeeTracker.Data[trade.FeeCurrency].Add(trade.Fee)
log.Infof("[FeeBudget] accumulated fee: %s %s", f.DailyFeeTracker.Data[trade.FeeCurrency].String(), trade.FeeCurrency)
}