package rebalance import ( "context" "fmt" "sync" "github.com/sirupsen/logrus" "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/types" ) const ID = "rebalance" var log = logrus.WithField("strategy", ID) func init() { bbgo.RegisterStrategy(ID, &Strategy{}) } func instanceID(symbol string) string { return fmt.Sprintf("%s:%s", ID, symbol) } type Strategy struct { Environment *bbgo.Environment Interval types.Interval `json:"interval"` QuoteCurrency string `json:"quoteCurrency"` TargetWeights types.ValueMap `json:"targetWeights"` Threshold fixedpoint.Value `json:"threshold"` MaxAmount fixedpoint.Value `json:"maxAmount"` // max amount to buy or sell per order OrderType types.OrderType `json:"orderType"` DryRun bool `json:"dryRun"` PositionMap PositionMap `persistence:"positionMap"` ProfitStatsMap ProfitStatsMap `persistence:"profitStatsMap"` session *bbgo.ExchangeSession orderExecutorMap GeneralOrderExecutorMap activeOrderBook *bbgo.ActiveOrderBook } func (s *Strategy) Defaults() error { if s.OrderType == "" { s.OrderType = types.OrderTypeLimitMaker } return nil } func (s *Strategy) Initialize() error { return nil } func (s *Strategy) ID() string { return ID } func (s *Strategy) Validate() error { if len(s.TargetWeights) == 0 { return fmt.Errorf("targetWeights should not be empty") } if !s.TargetWeights.Sum().Eq(fixedpoint.One) { return fmt.Errorf("the sum of targetWeights should be 1") } for currency, weight := range s.TargetWeights { if weight.Float64() < 0 { return fmt.Errorf("%s weight: %f should not less than 0", currency, weight.Float64()) } } if s.Threshold.Sign() < 0 { return fmt.Errorf("threshold should not less than 0") } if s.MaxAmount.Sign() < 0 { return fmt.Errorf("maxAmount shoud not less than 0") } return nil } func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) { for _, symbol := range s.symbols() { session.Subscribe(types.KLineChannel, symbol, types.SubscribeOptions{Interval: s.Interval}) } } func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { s.session = session markets, err := s.markets() if err != nil { return err } if s.PositionMap == nil { s.PositionMap = NewPositionMap(markets) } if s.ProfitStatsMap == nil { s.ProfitStatsMap = NewProfitStatsMap(markets) } s.orderExecutorMap = NewGeneralOrderExecutorMap(session, s.PositionMap) s.orderExecutorMap.BindEnvironment(s.Environment) s.orderExecutorMap.BindProfitStats(s.ProfitStatsMap) s.orderExecutorMap.Bind() s.orderExecutorMap.Sync(ctx, s) s.activeOrderBook = bbgo.NewActiveOrderBook("") s.activeOrderBook.BindStream(s.session.UserDataStream) s.session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { s.rebalance(ctx) }) // the shutdown handler, you can cancel all orders bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) { defer wg.Done() _ = s.orderExecutorMap.GracefulCancel(ctx) }) return nil } func (s *Strategy) rebalance(ctx context.Context) { // cancel active orders before rebalance if err := s.session.Exchange.CancelOrders(ctx, s.activeOrderBook.Orders()...); err != nil { log.WithError(err).Errorf("failed to cancel orders") } submitOrders := s.generateSubmitOrders(ctx) for _, order := range submitOrders { log.Infof("generated submit order: %s", order.String()) } if s.DryRun { return } createdOrders, err := s.orderExecutorMap.SubmitOrders(ctx, submitOrders...) if err != nil { log.WithError(err).Error("failed to submit orders") return } s.activeOrderBook.Add(createdOrders...) } func (s *Strategy) prices(ctx context.Context) types.ValueMap { m := make(types.ValueMap) for currency := range s.TargetWeights { if currency == s.QuoteCurrency { m[s.QuoteCurrency] = fixedpoint.One continue } ticker, err := s.session.Exchange.QueryTicker(ctx, currency+s.QuoteCurrency) if err != nil { log.WithError(err).Error("failed to query tickers") return nil } m[currency] = ticker.Last } return m } func (s *Strategy) quantities() types.ValueMap { m := make(types.ValueMap) balances := s.session.GetAccount().Balances() for currency := range s.TargetWeights { m[currency] = balances[currency].Total() } return m } func (s *Strategy) generateSubmitOrders(ctx context.Context) (submitOrders []types.SubmitOrder) { prices := s.prices(ctx) marketValues := prices.Mul(s.quantities()) currentWeights := marketValues.Normalize() for currency, targetWeight := range s.TargetWeights { if currency == s.QuoteCurrency { continue } symbol := currency + s.QuoteCurrency currentWeight := currentWeights[currency] currentPrice := prices[currency] log.Infof("%s price: %v, current weight: %v, target weight: %v", symbol, currentPrice, currentWeight, targetWeight) // calculate the difference between current weight and target weight // if the difference is less than threshold, then we will not create the order weightDifference := targetWeight.Sub(currentWeight) if weightDifference.Abs().Compare(s.Threshold) < 0 { log.Infof("%s weight distance |%v - %v| = |%v| less than the threshold: %v", symbol, currentWeight, targetWeight, weightDifference, s.Threshold) continue } quantity := weightDifference.Mul(marketValues.Sum()).Div(currentPrice) side := types.SideTypeBuy if quantity.Sign() < 0 { side = types.SideTypeSell quantity = quantity.Abs() } if s.MaxAmount.Sign() > 0 { quantity = bbgo.AdjustQuantityByMaxAmount(quantity, currentPrice, s.MaxAmount) log.Infof("adjust the quantity %v (%s %s @ %v) by max amount %v", quantity, symbol, side.String(), currentPrice, s.MaxAmount) } log.Debugf("symbol: %v, quantity: %v", symbol, quantity) order := types.SubmitOrder{ Symbol: symbol, Side: side, Type: s.OrderType, Quantity: quantity, Price: currentPrice, } submitOrders = append(submitOrders, order) } return submitOrders } func (s *Strategy) symbols() (symbols []string) { for currency := range s.TargetWeights { if currency == s.QuoteCurrency { continue } symbols = append(symbols, currency+s.QuoteCurrency) } return symbols } func (s *Strategy) markets() ([]types.Market, error) { markets := []types.Market{} for _, symbol := range s.symbols() { market, ok := s.session.Market(symbol) if !ok { return nil, fmt.Errorf("market %s not found", symbol) } markets = append(markets, market) } return markets, nil }