mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 01:01:56 +00:00
common: pull out ProfitFixerBundle
This commit is contained in:
parent
d982524824
commit
c217aadc1b
|
@ -2,12 +2,14 @@ package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
|
|
||||||
|
"github.com/c9s/bbgo/pkg/bbgo"
|
||||||
"github.com/c9s/bbgo/pkg/exchange/batch"
|
"github.com/c9s/bbgo/pkg/exchange/batch"
|
||||||
"github.com/c9s/bbgo/pkg/types"
|
"github.com/c9s/bbgo/pkg/types"
|
||||||
)
|
)
|
||||||
|
@ -101,3 +103,38 @@ func (f *ProfitFixer) FixFromTrades(allTrades []types.Trade, stats *types.Profit
|
||||||
log.Infof("profitFixer fix finished: profitStats and position are updated from %d trades", len(allTrades))
|
log.Infof("profitFixer fix finished: profitStats and position are updated from %d trades", len(allTrades))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type ProfitFixerBundle struct {
|
||||||
|
ProfitFixerConfig *ProfitFixerConfig `json:"profitFixer,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *ProfitFixerBundle) Fix(
|
||||||
|
ctx context.Context,
|
||||||
|
symbol string,
|
||||||
|
position *types.Position,
|
||||||
|
profitStats *types.ProfitStats,
|
||||||
|
sessions ...*bbgo.ExchangeSession,
|
||||||
|
) error {
|
||||||
|
bbgo.Notify("Fixing %s profitStats and position...", symbol)
|
||||||
|
|
||||||
|
log.Infof("profitFixer is enabled, checking checkpoint: %+v", f.ProfitFixerConfig.TradesSince)
|
||||||
|
|
||||||
|
if f.ProfitFixerConfig.TradesSince.Time().IsZero() {
|
||||||
|
return fmt.Errorf("tradesSince time can not be zero")
|
||||||
|
}
|
||||||
|
|
||||||
|
fixer := NewProfitFixer()
|
||||||
|
for _, session := range sessions {
|
||||||
|
if ss, ok := session.Exchange.(types.ExchangeTradeHistoryService); ok {
|
||||||
|
log.Infof("adding makerSession %s to profitFixer", session.Name)
|
||||||
|
fixer.AddExchange(session.Name, ss)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return fixer.Fix(ctx,
|
||||||
|
symbol,
|
||||||
|
f.ProfitFixerConfig.TradesSince.Time(),
|
||||||
|
time.Now(),
|
||||||
|
profitStats,
|
||||||
|
position)
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
@ -28,41 +27,6 @@ func init() {
|
||||||
bbgo.RegisterStrategy(ID, &Strategy{})
|
bbgo.RegisterStrategy(ID, &Strategy{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type ProfitFixer struct {
|
|
||||||
ProfitFixerConfig *common.ProfitFixerConfig `json:"profitFixer,omitempty"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *ProfitFixer) Fix(
|
|
||||||
ctx context.Context,
|
|
||||||
symbol string,
|
|
||||||
position *types.Position,
|
|
||||||
profitStats *types.ProfitStats,
|
|
||||||
sessions ...*bbgo.ExchangeSession,
|
|
||||||
) error {
|
|
||||||
bbgo.Notify("Fixing %s profitStats and position...", symbol)
|
|
||||||
|
|
||||||
log.Infof("profitFixer is enabled, checking checkpoint: %+v", f.ProfitFixerConfig.TradesSince)
|
|
||||||
|
|
||||||
if f.ProfitFixerConfig.TradesSince.Time().IsZero() {
|
|
||||||
return fmt.Errorf("tradesSince time can not be zero")
|
|
||||||
}
|
|
||||||
|
|
||||||
fixer := common.NewProfitFixer()
|
|
||||||
for _, session := range sessions {
|
|
||||||
if ss, ok := session.Exchange.(types.ExchangeTradeHistoryService); ok {
|
|
||||||
log.Infof("adding makerSession %s to profitFixer", session.Name)
|
|
||||||
fixer.AddExchange(session.Name, ss)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return fixer.Fix(ctx,
|
|
||||||
symbol,
|
|
||||||
f.ProfitFixerConfig.TradesSince.Time(),
|
|
||||||
time.Now(),
|
|
||||||
profitStats,
|
|
||||||
position)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strategy is the strategy struct of LiquidityMaker
|
// Strategy is the strategy struct of LiquidityMaker
|
||||||
// liquidity maker does not care about the current price, it tries to place liquidity orders (limit maker orders)
|
// liquidity maker does not care about the current price, it tries to place liquidity orders (limit maker orders)
|
||||||
// around the current mid price
|
// around the current mid price
|
||||||
|
@ -99,7 +63,7 @@ type Strategy struct {
|
||||||
|
|
||||||
MinProfit fixedpoint.Value `json:"minProfit"`
|
MinProfit fixedpoint.Value `json:"minProfit"`
|
||||||
|
|
||||||
ProfitFixer
|
common.ProfitFixerBundle
|
||||||
|
|
||||||
liquidityOrderBook, adjustmentOrderBook *bbgo.ActiveOrderBook
|
liquidityOrderBook, adjustmentOrderBook *bbgo.ActiveOrderBook
|
||||||
|
|
||||||
|
@ -129,12 +93,12 @@ func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
|
||||||
if s.ProfitFixer.ProfitFixerConfig != nil {
|
if s.ProfitFixerBundle.ProfitFixerConfig != nil {
|
||||||
market, _ := session.Market(s.Symbol)
|
market, _ := session.Market(s.Symbol)
|
||||||
s.Position = types.NewPositionFromMarket(market)
|
s.Position = types.NewPositionFromMarket(market)
|
||||||
s.ProfitStats = types.NewProfitStats(market)
|
s.ProfitStats = types.NewProfitStats(market)
|
||||||
|
|
||||||
if err := s.ProfitFixer.Fix(ctx, s.Symbol, s.Position, s.ProfitStats, session); err != nil {
|
if err := s.ProfitFixerBundle.Fix(ctx, s.Symbol, s.Position, s.ProfitStats, session); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user