Skip to content

Commit

Permalink
add fee budget support to random strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
narumiruna committed Jun 18, 2024
1 parent 11d2a49 commit 892c607
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
8 changes: 5 additions & 3 deletions pkg/strategy/common/fee_budget.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func (f *FeeBudget) IsBudgetAllowed() bool {
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 {
Expand All @@ -59,14 +64,12 @@ func (f *FeeBudget) HandleTradeUpdate(trade types.Trade) {
}

f.State.AccumulatedFees[trade.FeeCurrency] = f.State.AccumulatedFees[trade.FeeCurrency].Add(trade.Fee)
f.State.AccumulatedVolume = f.State.AccumulatedVolume.Add(trade.Quantity)
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"`
AccumulatedVolume fixedpoint.Value `json:"accumulatedVolume,omitempty"`
}

func (s *State) IsOver24Hours() bool {
Expand All @@ -81,5 +84,4 @@ func (s *State) Reset() {

s.AccumulatedFeeStartedAt = dateTime
s.AccumulatedFees = make(map[string]fixedpoint.Value)
s.AccumulatedVolume = fixedpoint.Zero
}
6 changes: 5 additions & 1 deletion pkg/strategy/common/fee_budget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"testing"
"time"

"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
Expand Down Expand Up @@ -46,7 +47,10 @@ func TestFeeBudget(t *testing.T) {
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())
}
}
35 changes: 29 additions & 6 deletions pkg/strategy/random/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func init() {

type Strategy struct {
*common.Strategy
*common.FeeBudget

Environment *bbgo.Environment
Market types.Market
Expand All @@ -45,6 +46,10 @@ func (s *Strategy) Initialize() error {
if s.Strategy == nil {
s.Strategy = &common.Strategy{}
}

if s.FeeBudget == nil {
s.FeeBudget = &common.FeeBudget{}
}
return nil
}

Expand All @@ -71,11 +76,25 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {}

func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, s.ID(), s.InstanceID())
s.FeeBudget.Initialize()

session.UserDataStream.OnStart(func() {
if s.OnStart {
s.placeOrder()
if !s.OnStart {
return
}

if !s.FeeBudget.IsBudgetAllowed() {
return
}

s.placeOrder(ctx)
})

session.UserDataStream.OnTradeUpdate(func(trade types.Trade) {
if trade.Symbol != s.Symbol {
return
}
s.FeeBudget.HandleTradeUpdate(trade)
})

// the shutdown handler, you can cancel all orders
Expand All @@ -86,15 +105,19 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
})

s.cron = cron.New()
s.cron.AddFunc(s.Schedule, s.placeOrder)
s.cron.AddFunc(s.Schedule, func() {
if !s.FeeBudget.IsBudgetAllowed() {
return
}

s.placeOrder(ctx)
})
s.cron.Start()

return nil
}

func (s *Strategy) placeOrder() {
ctx := context.Background()

func (s *Strategy) placeOrder(ctx context.Context) {
baseBalance, ok := s.Session.GetAccount().Balance(s.Market.BaseCurrency)
if !ok {
log.Errorf("base balance not found")
Expand Down

0 comments on commit 892c607

Please sign in to comment.