FEATURE: [dca2] new flag EnableQuoteInvestmentReallocate to decide if reallocate quote investment

This commit is contained in:
kbearXD 2024-04-01 10:17:37 +08:00
parent d55d1e9867
commit 8568e15e82
3 changed files with 14 additions and 2 deletions

View File

@ -21,7 +21,7 @@ func (s *Strategy) placeOpenPositionOrders(ctx context.Context) error {
return err
}
orders, err := generateOpenPositionOrders(s.Market, s.QuoteInvestment, s.ProfitStats.TotalProfit, price, s.PriceDeviation, s.MaxOrderCount, s.OrderGroupID)
orders, err := generateOpenPositionOrders(s.Market, s.EnableQuoteInvestmentReallocate, s.QuoteInvestment, s.ProfitStats.TotalProfit, price, s.PriceDeviation, s.MaxOrderCount, s.OrderGroupID)
if err != nil {
return err
}
@ -60,7 +60,7 @@ func getBestPriceUntilSuccess(ctx context.Context, ex types.Exchange, symbol str
return ticker.Sell, nil
}
func generateOpenPositionOrders(market types.Market, quoteInvestment, profit, price, priceDeviation fixedpoint.Value, maxOrderCount int64, orderGroupID uint32) ([]types.SubmitOrder, error) {
func generateOpenPositionOrders(market types.Market, enableQuoteInvestmentReallocate bool, quoteInvestment, profit, price, priceDeviation fixedpoint.Value, maxOrderCount int64, orderGroupID uint32) ([]types.SubmitOrder, error) {
factor := fixedpoint.One.Sub(priceDeviation)
profit = market.TruncatePrice(profit)
@ -83,6 +83,10 @@ func generateOpenPositionOrders(market types.Market, quoteInvestment, profit, pr
return nil, fmt.Errorf("failed to calculate notional and num of open position orders, price: %s, quote investment: %s", price, quoteInvestment)
}
if !enableQuoteInvestmentReallocate && orderNum != int(maxOrderCount) {
return nil, fmt.Errorf("failed to generate open-position orders due to the orders may be under min notional or quantity")
}
side := types.SideTypeBuy
var submitOrders []types.SubmitOrder

View File

@ -150,8 +150,13 @@ func (s *Strategy) runPositionOpening(ctx context.Context, next State) {
s.logger.Info("[State] PositionOpening - start placing open-position orders")
if err := s.placeOpenPositionOrders(ctx); err != nil {
s.logger.WithError(err).Error("failed to place dca orders, please check it.")
// try after 1 minute when failed to placing orders
s.startTimeOfNextRound = s.startTimeOfNextRound.Add(1 * time.Minute)
s.updateState(WaitToOpenPosition)
return
}
s.updateState(OpenPositionReady)
s.logger.Info("[State] PositionOpening -> OpenPositionReady")
}

View File

@ -70,6 +70,9 @@ type Strategy struct {
DisableProfitStatsRecover bool `json:"disableProfitStatsRecover"`
DisablePositionRecover bool `json:"disablePositionRecover"`
// EnableQuoteInvestmentReallocate set to true, the quote investment will be reallocated when the notional or quantity is under minimum.
EnableQuoteInvestmentReallocate bool `json:"enableQuoteInvestmentReallocate"`
// KeepOrdersWhenShutdown option is used for keeping the grid orders when shutting down bbgo
KeepOrdersWhenShutdown bool `json:"keepOrdersWhenShutdown"`