Merge pull request #1749 from r03921081/task/change_circuitbreaker

Use new circuitbreaker in common strategy
This commit is contained in:
c9s 2024-10-06 12:10:22 +08:00 committed by GitHub
commit 113695eabf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -8,6 +8,7 @@ import (
"github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/fixedpoint" "github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/risk/circuitbreaker"
"github.com/c9s/bbgo/pkg/risk/riskcontrol" "github.com/c9s/bbgo/pkg/risk/riskcontrol"
"github.com/c9s/bbgo/pkg/types" "github.com/c9s/bbgo/pkg/types"
) )
@ -19,7 +20,7 @@ type RiskController struct {
CircuitBreakEMA types.IntervalWindow `json:"circuitBreakEMA"` CircuitBreakEMA types.IntervalWindow `json:"circuitBreakEMA"`
positionRiskControl *riskcontrol.PositionRiskControl positionRiskControl *riskcontrol.PositionRiskControl
circuitBreakRiskControl *riskcontrol.CircuitBreakRiskControl circuitBreakRiskControl *circuitbreaker.BasicCircuitBreaker
} }
// Strategy provides the core functionality that is required by a long/short strategy. // Strategy provides the core functionality that is required by a long/short strategy.
@ -78,12 +79,12 @@ func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, se
if !s.CircuitBreakLossThreshold.IsZero() { if !s.CircuitBreakLossThreshold.IsZero() {
log.Infof("circuitBreakLossThreshold is configured, setting up CircuitBreakRiskControl...") log.Infof("circuitBreakLossThreshold is configured, setting up CircuitBreakRiskControl...")
s.circuitBreakRiskControl = riskcontrol.NewCircuitBreakRiskControl( s.circuitBreakRiskControl = circuitbreaker.NewBasicCircuitBreaker(strategyID, instanceID)
s.Position, s.OrderExecutor.TradeCollector().OnProfit(func(trade types.Trade, profit *types.Profit) {
session.Indicators(market.Symbol).EWMA(s.CircuitBreakEMA), if profit != nil && s.circuitBreakRiskControl != nil {
s.CircuitBreakLossThreshold, s.circuitBreakRiskControl.RecordProfit(profit.Profit, trade.Time.Time())
s.ProfitStats, }
24*time.Hour) })
} }
} }
@ -91,5 +92,6 @@ func (s *Strategy) IsHalted(t time.Time) bool {
if s.circuitBreakRiskControl == nil { if s.circuitBreakRiskControl == nil {
return false return false
} }
return s.circuitBreakRiskControl.IsHalted(t) _, isHalted := s.circuitBreakRiskControl.IsHalted(t)
return isHalted
} }