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/fixedpoint"
"github.com/c9s/bbgo/pkg/risk/circuitbreaker"
"github.com/c9s/bbgo/pkg/risk/riskcontrol"
"github.com/c9s/bbgo/pkg/types"
)
@ -19,7 +20,7 @@ type RiskController struct {
CircuitBreakEMA types.IntervalWindow `json:"circuitBreakEMA"`
positionRiskControl *riskcontrol.PositionRiskControl
circuitBreakRiskControl *riskcontrol.CircuitBreakRiskControl
circuitBreakRiskControl *circuitbreaker.BasicCircuitBreaker
}
// 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() {
log.Infof("circuitBreakLossThreshold is configured, setting up CircuitBreakRiskControl...")
s.circuitBreakRiskControl = riskcontrol.NewCircuitBreakRiskControl(
s.Position,
session.Indicators(market.Symbol).EWMA(s.CircuitBreakEMA),
s.CircuitBreakLossThreshold,
s.ProfitStats,
24*time.Hour)
s.circuitBreakRiskControl = circuitbreaker.NewBasicCircuitBreaker(strategyID, instanceID)
s.OrderExecutor.TradeCollector().OnProfit(func(trade types.Trade, profit *types.Profit) {
if profit != nil && s.circuitBreakRiskControl != nil {
s.circuitBreakRiskControl.RecordProfit(profit.Profit, trade.Time.Time())
}
})
}
}
@ -91,5 +92,6 @@ func (s *Strategy) IsHalted(t time.Time) bool {
if s.circuitBreakRiskControl == nil {
return false
}
return s.circuitBreakRiskControl.IsHalted(t)
_, isHalted := s.circuitBreakRiskControl.IsHalted(t)
return isHalted
}