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

View File

@ -14,6 +14,7 @@ import (
"github.com/c9s/bbgo/pkg/core"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/indicator"
"github.com/c9s/bbgo/pkg/risk/circuitbreaker"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
@ -98,6 +99,8 @@ type Strategy struct {
state *State
CircuitBreaker *circuitbreaker.BasicCircuitBreaker `json:"circuitBreaker"`
// persistence fields
Position *types.Position `json:"position,omitempty" persistence:"position"`
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 {
if s.Quantity.IsZero() || s.QuantityScale == nil {
return errors.New("quantity or quantityScale can not be empty")