Skip to content

Commit d41de32

Browse files
authored
Merge pull request #1666 from c9s/c9s/liqmaker-profit-fixer
FEATURE: [liqmaker] add profit fixer support
2 parents e293ec5 + c217aad commit d41de32

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

pkg/strategy/common/profit_fixer.go

+37
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ package common
22

33
import (
44
"context"
5+
"fmt"
56
"sync"
67
"time"
78

89
log "github.com/sirupsen/logrus"
910
"golang.org/x/sync/errgroup"
1011

12+
"github.com/c9s/bbgo/pkg/bbgo"
1113
"github.com/c9s/bbgo/pkg/exchange/batch"
1214
"github.com/c9s/bbgo/pkg/types"
1315
)
@@ -101,3 +103,38 @@ func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.Profit
101103
log.Infof("profitFixer fix finished: profitStats and position are updated from %d trades", len(allTrades))
102104
return nil
103105
}
106+
107+
type ProfitFixerBundle struct {
108+
ProfitFixerConfig *ProfitFixerConfig `json:"profitFixer,omitempty"`
109+
}
110+
111+
func (f *ProfitFixerBundle) Fix(
112+
ctx context.Context,
113+
symbol string,
114+
position *types.Position,
115+
profitStats *types.ProfitStats,
116+
sessions ...*bbgo.ExchangeSession,
117+
) error {
118+
bbgo.Notify("Fixing %s profitStats and position...", symbol)
119+
120+
log.Infof("profitFixer is enabled, checking checkpoint: %+v", f.ProfitFixerConfig.TradesSince)
121+
122+
if f.ProfitFixerConfig.TradesSince.Time().IsZero() {
123+
return fmt.Errorf("tradesSince time can not be zero")
124+
}
125+
126+
fixer := NewProfitFixer()
127+
for _, session := range sessions {
128+
if ss, ok := session.Exchange.(types.ExchangeTradeHistoryService); ok {
129+
log.Infof("adding makerSession %s to profitFixer", session.Name)
130+
fixer.AddExchange(session.Name, ss)
131+
}
132+
}
133+
134+
return fixer.Fix(ctx,
135+
symbol,
136+
f.ProfitFixerConfig.TradesSince.Time(),
137+
time.Now(),
138+
profitStats,
139+
position)
140+
}

pkg/strategy/liquiditymaker/strategy.go

+15
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ type Strategy struct {
6363

6464
MinProfit fixedpoint.Value `json:"minProfit"`
6565

66+
common.ProfitFixerBundle
67+
6668
liquidityOrderBook, adjustmentOrderBook *bbgo.ActiveOrderBook
6769

6870
liquidityScale bbgo.Scale
@@ -91,6 +93,19 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
9193
}
9294

9395
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
96+
if s.ProfitFixerBundle.ProfitFixerConfig != nil {
97+
market, _ := session.Market(s.Symbol)
98+
s.Position = types.NewPositionFromMarket(market)
99+
s.ProfitStats = types.NewProfitStats(market)
100+
101+
if err := s.ProfitFixerBundle.Fix(ctx, s.Symbol, s.Position, s.ProfitStats, session); err != nil {
102+
return err
103+
}
104+
105+
bbgo.Notify("Fixed %s position", s.Symbol, s.Position)
106+
bbgo.Notify("Fixed %s profitStats", s.Symbol, s.ProfitStats)
107+
}
108+
94109
s.Strategy.Initialize(ctx, s.Environment, session, s.Market, ID, s.InstanceID())
95110

96111
s.orderGenerator = &LiquidityOrderGenerator{

pkg/strategy/xdepthmaker/strategy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ type Strategy struct {
196196
// Pips is the pips of the layer prices
197197
Pips fixedpoint.Value `json:"pips"`
198198

199-
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer"`
199+
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer,omitempty"`
200200

201201
// --------------------------------
202202
// private fields

0 commit comments

Comments
 (0)