xmaker: integrate circuitbreaker

This commit is contained in:
c9s 2024-08-24 11:42:07 +08:00
parent 92ad80f2f8
commit 14fff8dbad
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54
2 changed files with 14 additions and 1 deletions

View File

@ -116,7 +116,7 @@ type BasicCircuitBreaker struct {
} }
func NewBasicCircuitBreaker(strategyID, strategyInstance string) *BasicCircuitBreaker { func NewBasicCircuitBreaker(strategyID, strategyInstance string) *BasicCircuitBreaker {
return &BasicCircuitBreaker{ b := &BasicCircuitBreaker{
MaximumConsecutiveLossTimes: 8, MaximumConsecutiveLossTimes: 8,
MaximumHaltTimes: 3, MaximumHaltTimes: 3,
MaximumHaltTimesExceededPanic: false, MaximumHaltTimesExceededPanic: false,
@ -125,6 +125,8 @@ func NewBasicCircuitBreaker(strategyID, strategyInstance string) *BasicCircuitBr
strategyInstance: strategyInstance, strategyInstance: strategyInstance,
metricsLabels: prometheus.Labels{"strategy": strategyID, "strategyInstance": strategyInstance}, metricsLabels: prometheus.Labels{"strategy": strategyID, "strategyInstance": strategyInstance},
} }
b.updateMetrics()
return b
} }
func (b *BasicCircuitBreaker) getMetricsLabels() prometheus.Labels { func (b *BasicCircuitBreaker) getMetricsLabels() prometheus.Labels {

View File

@ -14,6 +14,7 @@ import (
"github.com/c9s/bbgo/pkg/core" "github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/risk/circuitbreaker"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util" "github.com/c9s/bbgo/pkg/util"
) )
@ -98,6 +99,8 @@ type Strategy struct {
state *State state *State
CircuitBreaker *circuitbreaker.BasicCircuitBreaker `json:"circuitBreaker"`
// persistence fields // persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"` Position *types.Position `json:"position,omitempty" persistence:"position"`
ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"` ProfitStats *ProfitStats `json:"profitStats,omitempty" persistence:"profit_stats"`
@ -630,6 +633,14 @@ func (s *Strategy) tradeRecover(ctx context.Context) {
} }
} }
func (s *Strategy) Defaults() error {
if s.CircuitBreaker == nil {
s.CircuitBreaker = circuitbreaker.NewBasicCircuitBreaker(ID, s.InstanceID())
}
return nil
}
func (s *Strategy) Validate() error { func (s *Strategy) Validate() error {
if s.Quantity.IsZero() || s.QuantityScale == nil { if s.Quantity.IsZero() || s.QuantityScale == nil {
return errors.New("quantity or quantityScale can not be empty") return errors.New("quantity or quantityScale can not be empty")