Use new circuitbreaker in common strategy

This commit is contained in:
Andy Liao 2024-09-18 22:35:35 +08:00
parent 37106c35b7
commit 7137343ba0

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
} }