bbgo_origin/pkg/strategy/dca2/strategy.go

474 lines
13 KiB
Go
Raw Normal View History

2023-11-13 08:20:25 +00:00
package dca2
import (
"context"
"fmt"
2023-11-23 08:45:28 +00:00
"math"
"strconv"
2023-11-23 08:45:28 +00:00
"sync"
"time"
2023-11-13 08:20:25 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/sirupsen/logrus"
"go.uber.org/multierr"
2023-11-13 08:20:25 +00:00
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/exchange/retry"
2023-11-13 08:20:25 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
2024-01-10 06:37:07 +00:00
"github.com/c9s/bbgo/pkg/strategy/common"
2023-11-13 08:20:25 +00:00
"github.com/c9s/bbgo/pkg/types"
2023-11-23 08:45:28 +00:00
"github.com/c9s/bbgo/pkg/util"
"github.com/c9s/bbgo/pkg/util/tradingutil"
2023-11-13 08:20:25 +00:00
)
const (
ID = "dca2"
orderTag = "dca2"
)
2023-11-13 08:20:25 +00:00
var (
log = logrus.WithField("strategy", ID)
baseLabels prometheus.Labels
)
2023-11-13 08:20:25 +00:00
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type advancedOrderCancelApi interface {
CancelAllOrders(ctx context.Context) ([]types.Order, error)
CancelOrdersBySymbol(ctx context.Context, symbol string) ([]types.Order, error)
CancelOrdersByGroupID(ctx context.Context, groupID uint32) ([]types.Order, error)
}
2024-01-02 06:09:38 +00:00
//go:generate callbackgen -type Strateg
2023-11-13 08:20:25 +00:00
type Strategy struct {
Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
2023-11-23 08:45:28 +00:00
Environment *bbgo.Environment
ExchangeSession *bbgo.ExchangeSession
OrderExecutor *bbgo.GeneralOrderExecutor
Market types.Market
2023-11-13 08:20:25 +00:00
Symbol string `json:"symbol"`
// setting
QuoteInvestment fixedpoint.Value `json:"quoteInvestment"`
MaxOrderCount int64 `json:"maxOrderCount"`
2023-11-23 08:45:28 +00:00
PriceDeviation fixedpoint.Value `json:"priceDeviation"`
TakeProfitRatio fixedpoint.Value `json:"takeProfitRatio"`
CoolDownInterval types.Duration `json:"coolDownInterval"`
2023-11-13 08:20:25 +00:00
// OrderGroupID is the group ID used for the strategy instance for canceling orders
OrderGroupID uint32 `json:"orderGroupID"`
// RecoverWhenStart option is used for recovering dca states
RecoverWhenStart bool `json:"recoverWhenStart"`
// KeepOrdersWhenShutdown option is used for keeping the grid orders when shutting down bbgo
KeepOrdersWhenShutdown bool `json:"keepOrdersWhenShutdown"`
// UseCancelAllOrdersApiWhenClose uses a different API to cancel all the orders on the market when closing a grid
UseCancelAllOrdersApiWhenClose bool `json:"useCancelAllOrdersApiWhenClose"`
// dev mode
DevMode *DevMode `json:"devMode"`
2023-11-13 08:20:25 +00:00
// log
logger *logrus.Entry
LogFields logrus.Fields `json:"logFields"`
// PrometheusLabels will be used as the base prometheus labels
PrometheusLabels prometheus.Labels `json:"prometheusLabels"`
2023-11-13 08:20:25 +00:00
// private field
2023-11-23 08:45:28 +00:00
mu sync.Mutex
takeProfitPrice fixedpoint.Value
startTimeOfNextRound time.Time
nextStateC chan State
state State
// callbacks
2024-01-10 06:37:07 +00:00
common.StatusCallbacks
positionCallbacks []func(*types.Position)
profitCallbacks []func(*ProfitStats)
2023-11-13 08:20:25 +00:00
}
func (s *Strategy) ID() string {
return ID
}
func (s *Strategy) Validate() error {
if s.MaxOrderCount < 1 {
return fmt.Errorf("maxOrderCount can not be < 1")
2023-11-13 08:20:25 +00:00
}
2023-11-23 08:45:28 +00:00
if s.TakeProfitRatio.Sign() <= 0 {
2023-11-13 08:20:25 +00:00
return fmt.Errorf("takeProfitSpread can not be <= 0")
}
2023-11-23 08:45:28 +00:00
if s.PriceDeviation.Sign() <= 0 {
2023-11-13 08:20:25 +00:00
return fmt.Errorf("margin can not be <= 0")
}
// TODO: validate balance is enough
return nil
}
func (s *Strategy) Defaults() error {
if s.LogFields == nil {
s.LogFields = logrus.Fields{}
}
s.LogFields["symbol"] = s.Symbol
s.LogFields["strategy"] = ID
2023-11-13 08:20:25 +00:00
return nil
}
func (s *Strategy) Initialize() error {
s.logger = log.WithFields(s.LogFields)
return nil
}
func (s *Strategy) InstanceID() string {
return fmt.Sprintf("%s-%s", ID, s.Symbol)
}
func (s *Strategy) Subscribe(session *bbgo.ExchangeSession) {
2023-11-23 08:45:28 +00:00
session.Subscribe(types.KLineChannel, s.Symbol, types.SubscribeOptions{Interval: types.Interval1m})
2023-11-13 08:20:25 +00:00
}
func (s *Strategy) newPrometheusLabels() prometheus.Labels {
labels := prometheus.Labels{
"exchange": "default",
"symbol": s.Symbol,
}
if s.ExchangeSession != nil {
labels["exchange"] = s.ExchangeSession.Name
}
if s.PrometheusLabels == nil {
return labels
}
return mergeLabels(s.PrometheusLabels, labels)
}
2023-11-13 08:20:25 +00:00
func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
2023-11-23 08:45:28 +00:00
instanceID := s.InstanceID()
s.ExchangeSession = session
2024-01-23 07:40:03 +00:00
if s.ProfitStats == nil {
s.ProfitStats = newProfitStats(s.Market, s.QuoteInvestment)
}
2024-01-23 07:40:03 +00:00
if s.Position == nil {
s.Position = types.NewPositionFromMarket(s.Market)
}
// prometheus
if s.PrometheusLabels != nil {
initMetrics(labelKeys(s.PrometheusLabels))
}
registerMetrics()
// prometheus labels
baseLabels = s.newPrometheusLabels()
// if dev mode is on and it's not a new strategy
if s.DevMode != nil && s.DevMode.Enabled && !s.DevMode.IsNewAccount {
s.ProfitStats = newProfitStats(s.Market, s.QuoteInvestment)
s.Position = types.NewPositionFromMarket(s.Market)
}
s.Position.Strategy = ID
s.Position.StrategyInstanceID = instanceID
if session.MakerFeeRate.Sign() > 0 || session.TakerFeeRate.Sign() > 0 {
s.Position.SetExchangeFeeRate(session.ExchangeName, types.ExchangeFee{
MakerFeeRate: session.MakerFeeRate,
TakerFeeRate: session.TakerFeeRate,
})
}
s.OrderExecutor = bbgo.NewGeneralOrderExecutor(session, s.Symbol, ID, instanceID, s.Position)
s.OrderExecutor.BindEnvironment(s.Environment)
s.OrderExecutor.Bind()
2023-11-23 08:45:28 +00:00
if s.OrderGroupID == 0 {
s.OrderGroupID = util.FNV32(instanceID) % math.MaxInt32
2023-11-13 08:20:25 +00:00
}
2023-11-23 08:45:28 +00:00
// order executor
s.OrderExecutor.TradeCollector().OnPositionUpdate(func(position *types.Position) {
2024-03-12 06:29:36 +00:00
s.logger.Infof("POSITION UPDATE: %s", s.Position.String())
2023-11-13 08:20:25 +00:00
bbgo.Sync(ctx, s)
2023-11-23 08:45:28 +00:00
// update take profit price here
s.updateTakeProfitPrice()
})
s.OrderExecutor.ActiveMakerOrders().OnFilled(func(o types.Order) {
2024-03-12 06:29:36 +00:00
s.logger.Infof("FILLED ORDER: %s", o.String())
openPositionSide := types.SideTypeBuy
takeProfitSide := types.SideTypeSell
switch o.Side {
case openPositionSide:
s.emitNextState(OpenPositionOrderFilled)
case takeProfitSide:
s.emitNextState(WaitToOpenPosition)
default:
2024-03-12 06:29:36 +00:00
s.logger.Infof("unsupported side (%s) of order: %s", o.Side, o)
}
// update metrics when filled
s.updateNumOfOrdersMetrics(ctx)
2023-11-23 08:45:28 +00:00
})
2023-11-13 08:20:25 +00:00
2023-11-23 08:45:28 +00:00
session.MarketDataStream.OnKLine(func(kline types.KLine) {
// check price here
if s.state != OpenPositionOrderFilled {
return
}
compRes := kline.Close.Compare(s.takeProfitPrice)
// price doesn't hit the take profit price
2023-12-22 07:50:48 +00:00
if compRes < 0 {
return
}
s.emitNextState(OpenPositionOrdersCancelling)
2023-11-13 08:20:25 +00:00
})
2023-11-23 08:45:28 +00:00
session.UserDataStream.OnAuth(func() {
2024-03-12 06:29:36 +00:00
s.logger.Info("user data stream authenticated")
time.AfterFunc(3*time.Second, func() {
if isInitialize := s.initializeNextStateC(); !isInitialize {
// no need to recover when two situation
// 1. recoverWhenStart is false
// 2. dev mode is on and it's not new strategy
if !s.RecoverWhenStart || (s.DevMode != nil && s.DevMode.Enabled && !s.DevMode.IsNewAccount) {
s.updateState(WaitToOpenPosition)
} else {
// recover
2024-03-12 06:29:36 +00:00
maxTry := 3
for try := 1; try <= maxTry; try++ {
s.logger.Infof("try #%d recover", try)
err := s.recover(ctx)
if err == nil {
s.logger.Infof("recover successfully at #%d", try)
break
}
s.logger.WithError(err).Warnf("failed to recover at #%d", try)
if try == 3 {
s.logger.Errorf("failed to recover after %d trying, please check it", maxTry)
return
}
}
}
2024-03-12 06:29:36 +00:00
s.logger.Infof("state: %d", s.state)
s.logger.Infof("position %s", s.Position.String())
s.logger.Infof("profit stats %s", s.ProfitStats.String())
s.logger.Infof("startTimeOfNextRound %s", s.startTimeOfNextRound)
s.updateTakeProfitPrice()
// store persistence
bbgo.Sync(ctx, s)
2024-01-02 06:09:38 +00:00
// ready
s.EmitReady()
// start running state machine
s.runState(ctx)
}
})
2023-11-23 08:45:28 +00:00
})
2023-11-13 08:20:25 +00:00
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
if s.KeepOrdersWhenShutdown {
s.logger.Infof("keepOrdersWhenShutdown is set, will keep the orders on the exchange")
return
}
if err := s.Close(ctx); err != nil {
s.logger.WithError(err).Errorf("dca2 graceful order cancel error")
}
})
2023-11-23 08:45:28 +00:00
return nil
2023-11-13 08:20:25 +00:00
}
func (s *Strategy) updateTakeProfitPrice() {
takeProfitRatio := s.TakeProfitRatio
s.takeProfitPrice = s.Market.TruncatePrice(s.Position.AverageCost.Mul(fixedpoint.One.Add(takeProfitRatio)))
2024-03-12 06:29:36 +00:00
s.logger.Infof("cost: %s, ratio: %s, price: %s", s.Position.AverageCost.String(), takeProfitRatio.String(), s.takeProfitPrice.String())
}
func (s *Strategy) Close(ctx context.Context) error {
2024-03-12 06:29:36 +00:00
s.logger.Infof("closing %s dca2", s.Symbol)
defer s.EmitClosed()
2024-01-03 09:02:03 +00:00
err := s.OrderExecutor.GracefulCancel(ctx)
if err != nil {
2024-03-12 06:29:36 +00:00
s.logger.WithError(err).Errorf("there are errors when cancelling orders at close")
}
bbgo.Sync(ctx, s)
return err
}
func (s *Strategy) CleanUp(ctx context.Context) error {
_ = s.Initialize()
defer s.EmitClosed()
session := s.ExchangeSession
if session == nil {
return fmt.Errorf("Session is nil, please check it")
}
// ignore the first cancel error, this skips one open-orders query request
if err := tradingutil.UniversalCancelAllOrders(ctx, session.Exchange, nil); err == nil {
return nil
}
// if cancel all orders returns error, get the open orders and retry the cancel in each round
var werr error
for {
s.logger.Infof("checking %s open orders...", s.Symbol)
openOrders, err := retry.QueryOpenOrdersUntilSuccessful(ctx, session.Exchange, s.Symbol)
if err != nil {
s.logger.WithError(err).Errorf("unable to query open orders")
continue
}
// all clean up
if len(openOrders) == 0 {
break
}
if err := tradingutil.UniversalCancelAllOrders(ctx, session.Exchange, openOrders); err != nil {
s.logger.WithError(err).Errorf("unable to cancel all orders")
werr = multierr.Append(werr, err)
}
time.Sleep(1 * time.Second)
}
return werr
}
func (s *Strategy) CalculateAndEmitProfit(ctx context.Context) error {
historyService, ok := s.ExchangeSession.Exchange.(types.ExchangeTradeHistoryService)
if !ok {
return fmt.Errorf("exchange %s doesn't support ExchangeTradeHistoryService", s.ExchangeSession.Exchange.Name())
}
queryService, ok := s.ExchangeSession.Exchange.(types.ExchangeOrderQueryService)
if !ok {
return fmt.Errorf("exchange %s doesn't support ExchangeOrderQueryService", s.ExchangeSession.Exchange.Name())
}
// TODO: pagination for it
// query the orders
2024-03-12 06:29:36 +00:00
s.logger.Infof("query %s closed orders from order id #%d", s.Symbol, s.ProfitStats.FromOrderID)
orders, err := retry.QueryClosedOrdersUntilSuccessfulLite(ctx, historyService, s.Symbol, time.Time{}, time.Time{}, s.ProfitStats.FromOrderID)
if err != nil {
return err
}
2024-03-12 06:29:36 +00:00
s.logger.Infof("there are %d closed orders from order id #%d", len(orders), s.ProfitStats.FromOrderID)
var rounds []Round
var round Round
for _, order := range orders {
2024-01-10 06:37:07 +00:00
// skip not this strategy order
if order.GroupID != s.OrderGroupID {
continue
}
switch order.Side {
case types.SideTypeBuy:
round.OpenPositionOrders = append(round.OpenPositionOrders, order)
case types.SideTypeSell:
if order.Status != types.OrderStatusFilled {
continue
}
round.TakeProfitOrder = order
rounds = append(rounds, round)
round = Round{}
default:
s.logger.Errorf("there is order with unsupported side")
}
}
2024-03-12 06:29:36 +00:00
s.logger.Infof("there are %d rounds from order id #%d", len(rounds), s.ProfitStats.FromOrderID)
for _, round := range rounds {
2024-03-12 06:29:36 +00:00
debugRoundOrders(s.logger, "calculate", round)
var roundOrders []types.Order = round.OpenPositionOrders
roundOrders = append(roundOrders, round.TakeProfitOrder)
for _, order := range roundOrders {
2024-03-12 06:29:36 +00:00
s.logger.Infof("calculate profit stats from order: %s", order.String())
2024-01-10 06:37:07 +00:00
// skip no trade orders
if order.ExecutedQuantity.Sign() == 0 {
continue
}
2024-03-12 06:29:36 +00:00
trades, err := retry.QueryOrderTradesUntilSuccessfulLite(ctx, queryService, types.OrderQuery{
Symbol: order.Symbol,
OrderID: strconv.FormatUint(order.OrderID, 10),
})
if err != nil {
return err
}
for _, trade := range trades {
2024-03-12 06:29:36 +00:00
s.logger.Infof("calculate profit stats from trade: %s", trade.String())
s.ProfitStats.AddTrade(trade)
}
}
s.ProfitStats.FromOrderID = round.TakeProfitOrder.OrderID + 1
s.ProfitStats.QuoteInvestment = s.ProfitStats.QuoteInvestment.Add(s.ProfitStats.CurrentRoundProfit)
// store into persistence
bbgo.Sync(ctx, s)
2024-03-12 06:29:36 +00:00
s.logger.Infof("profit stats:\n%s", s.ProfitStats.String())
// emit profit
s.EmitProfit(s.ProfitStats)
updateProfitMetrics(s.ProfitStats.Round, s.ProfitStats.CurrentRoundProfit.Float64())
s.ProfitStats.NewRound()
}
return nil
}
func (s *Strategy) updateNumOfOrdersMetrics(ctx context.Context) {
// update open orders metrics
openOrders, err := s.ExchangeSession.Exchange.QueryOpenOrders(ctx, s.Symbol)
if err != nil {
s.logger.WithError(err).Warn("failed to query open orders to update num of the orders metrics")
} else {
metricsNumOfOpenOrders.With(baseLabels).Set(float64(len(openOrders)))
}
// update active orders metrics
metricsNumOfActiveOrders.With(baseLabels).Set(float64(s.OrderExecutor.ActiveMakerOrders().NumOfOrders()))
}