dca3

package
v1.64.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 14, 2026 License: AGPL-3.0 Imports: 20 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	ID = "dca3"

	OpenPositionSide = types.SideTypeBuy
	TakeProfitSide   = types.SideTypeSell
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Collector

type Collector struct {
	// contains filtered or unexported fields
}

func NewCollector

func NewCollector(logger logrus.FieldLogger, symbol string, groupID uint32, filterGroupID bool, ex types.Exchange, queryService CollectorQueryService) *Collector

func (Collector) CollectCurrentRound

func (rc Collector) CollectCurrentRound(ctx context.Context, sinceLimit time.Time) (Round, error)

func (*Collector) CollectRoundTrades

func (rc *Collector) CollectRoundTrades(ctx context.Context, round Round) ([]types.Trade, error)

CollectRoundTrades collect the trades of the orders in the given round. The trades' fee are processed (feeProcessing = false)

func (*Collector) CollectRoundsFromOrderID

func (rc *Collector) CollectRoundsFromOrderID(ctx context.Context, fromOrderID uint64) ([]Round, error)

type CollectorQueryService

type CollectorQueryService interface {
	types.ExchangeTradeHistoryService
	types.ExchangeOrderQueryService
	QueryClosedOrdersDesc(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) ([]types.Order, error)
}

type LogLevel

type LogLevel int64
const (
	LogLevelNone LogLevel = iota
	LogLevelInfo
	LogLevelWarn
	LogLevelWarnFirst
	LogLevelError
)

type ProfitStats

type ProfitStats struct {
	Symbol string       `json:"symbol"`
	Market types.Market `json:"market,omitempty"`

	FromOrderID     uint64           `json:"fromOrderID,omitempty"`
	Round           int64            `json:"round,omitempty"`
	QuoteInvestment fixedpoint.Value `json:"quoteInvestment,omitempty"`

	CurrentRoundProfit fixedpoint.Value            `json:"currentRoundProfit,omitempty"`
	CurrentRoundFee    map[string]fixedpoint.Value `json:"currentRoundFee,omitempty"`
	TotalProfit        fixedpoint.Value            `json:"totalProfit,omitempty"`
	TotalFee           map[string]fixedpoint.Value `json:"totalFee,omitempty"`

	// used to flexible recovery
	OpenPositionPVs []types.PriceVolume `json:"openPositionPVs,omitempty"`

	types.PersistenceTTL
}

func (*ProfitStats) AddTrade

func (s *ProfitStats) AddTrade(trade types.Trade)

func (*ProfitStats) NewRound

func (s *ProfitStats) NewRound()

func (*ProfitStats) String

func (s *ProfitStats) String() string

type Round

type Round struct {
	OpenPositionOrders []types.Order
	TakeProfitOrders   []types.Order
}

Round contains the open-position orders and the take-profit orders 1. len(OpenPositionOrders) == 0 -> not open position 2. len(TakeProfitOrders) == 0 -> not in the take-profit stage 3. There are take-profit orders only when open-position orders are cancelled 4. We need to make sure the order: open-position (BUY) -> take-profit (SELL) -> open-position (BUY) -> take-profit (SELL) -> ... 5. When there is one filled take-profit order, this round must be finished. We need to verify all take-profit orders are not active

type State

type State int64
const (
	None State = iota
	StateIdleWaiting
	StateOpenPositionReady
	StateOpenPositionMOQReached
	StateTakeProfitOrderReset
	StateTakeProfitReached
)

type StateMachine

type StateMachine struct {
	// contains filtered or unexported fields
}

func NewStateMachine

func NewStateMachine(logger *logrus.Entry) *StateMachine

func (*StateMachine) Close

func (s *StateMachine) Close()

func (*StateMachine) EmitNextState

func (s *StateMachine) EmitNextState(state State)

func (*StateMachine) GetState

func (s *StateMachine) GetState() State

func (*StateMachine) OnClose

func (s *StateMachine) OnClose(cb func())

func (*StateMachine) OnStart

func (s *StateMachine) OnStart(cb func())

func (*StateMachine) RegisterTransitionHandler

func (s *StateMachine) RegisterTransitionHandler(from State, to State, fn TransitionHandler)

func (*StateMachine) RegisterWarnFirstLoggers

func (s *StateMachine) RegisterWarnFirstLoggers(from State, to State, logger *util.WarnFirstLogger)

func (*StateMachine) Run

func (s *StateMachine) Run(ctx context.Context)

func (*StateMachine) UpdateState

func (s *StateMachine) UpdateState(state State)

func (*StateMachine) WaitForRunningIs

func (s *StateMachine) WaitForRunningIs(isRunning bool, checkInterval, timeout time.Duration) bool

type Strategy

type Strategy struct {
	Position       *types.Position `json:"position,omitempty" persistence:"position"`
	ProfitStats    *ProfitStats    `json:"profitStats,omitempty" persistence:"profit_stats"`
	PersistenceTTL types.Duration  `json:"persistenceTTL"`

	Environment     *bbgo.Environment
	ExchangeSession *bbgo.ExchangeSession
	OrderExecutor   *bbgo.GeneralOrderExecutor
	Market          types.Market

	Symbol string `json:"symbol"`

	// setting
	QuoteInvestment  fixedpoint.Value `json:"quoteInvestment"`
	MaxOrderCount    int64            `json:"maxOrderCount"`
	PriceDeviation   fixedpoint.Value `json:"priceDeviation"`
	TakeProfitRatio  fixedpoint.Value `json:"takeProfitRatio"`
	CoolDownInterval types.Duration   `json:"coolDownInterval"`

	// OrderGroupID is the group ID used for the strategy instance for canceling orders
	OrderGroupID              uint32 `json:"orderGroupID"`
	DisableOrderGroupIDFilter bool   `json:"disableOrderGroupIDFilter"`

	// RecoverWhenStart option is used for recovering dca states
	RecoverWhenStart          bool `json:"recoverWhenStart"`
	DisableProfitStatsRecover bool `json:"disableProfitStatsRecover"`
	DisablePositionRecover    bool `json:"disablePositionRecover"`

	// KeepOrdersWhenShutdown option is used for keeping the grid orders when shutting down bbgo
	KeepOrdersWhenShutdown bool `json:"keepOrdersWhenShutdown"`

	// UseCancelAllOrdersApiWhenClose close all orders even though the orders don't belong to this strategy
	UseCancelAllOrdersApiWhenClose bool `json:"useCancelAllOrdersApiWhenClose"`

	LogFields logrus.Fields `json:"logFields"`

	// PrometheusLabels will be used as the base prometheus labels
	PrometheusLabels prometheus.Labels `json:"prometheusLabels"`

	// callbacks
	common.StatusCallbacks
	// contains filtered or unexported fields
}

func (*Strategy) CleanUp

func (s *Strategy) CleanUp(ctx context.Context) error

func (*Strategy) Close

func (s *Strategy) Close(ctx context.Context) error

func (*Strategy) ContinueNextRound

func (s *Strategy) ContinueNextRound()

func (*Strategy) Defaults

func (s *Strategy) Defaults() error

func (*Strategy) EmitPositionUpdate

func (s *Strategy) EmitPositionUpdate(position *types.Position)

func (*Strategy) EmitProfit

func (s *Strategy) EmitProfit(profitStats *ProfitStats)

func (*Strategy) ID

func (s *Strategy) ID() string

func (*Strategy) Initialize

func (s *Strategy) Initialize() error

func (*Strategy) InstanceID

func (s *Strategy) InstanceID() string

func (*Strategy) OnPositionUpdate

func (s *Strategy) OnPositionUpdate(cb func(*types.Position))

func (*Strategy) OnProfit

func (s *Strategy) OnProfit(cb func(*ProfitStats))

func (*Strategy) PauseNextRound

func (s *Strategy) PauseNextRound()

PauseNextRound will stop openning open-position orders at the next round

func (*Strategy) Run

func (*Strategy) Subscribe

func (s *Strategy) Subscribe(session *bbgo.ExchangeSession)

func (*Strategy) UpdateProfitStats

func (s *Strategy) UpdateProfitStats(ctx context.Context) (bool, error)

UpdateProfitStats will collect round from closed orders and emit update profit stats return true, nil -> there is at least one finished round and all the finished rounds we collect update profit stats successfully return false, nil -> there is no finished round! return true, error -> At least one round update profit stats successfully but there is error when collecting other rounds

func (*Strategy) UpdateProfitStatsUntilSuccessful

func (s *Strategy) UpdateProfitStatsUntilSuccessful(ctx context.Context) error

func (*Strategy) Validate

func (s *Strategy) Validate() error

type TransitionHandler

type TransitionHandler func(context.Context) (error, LogLevel)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL