From 4a231b10c6105a2e650907759e5ba390a7f5d907 Mon Sep 17 00:00:00 2001 From: narumi Date: Thu, 21 Sep 2023 14:57:55 +0800 Subject: [PATCH] pull out ishalted method --- pkg/risk/riskcontrol/circuit_break.go | 7 +++---- pkg/strategy/common/strategy.go | 4 ++++ pkg/strategy/fixedmaker/strategy.go | 13 +++++++++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/risk/riskcontrol/circuit_break.go b/pkg/risk/riskcontrol/circuit_break.go index 921696e21..e8e8ad3b0 100644 --- a/pkg/risk/riskcontrol/circuit_break.go +++ b/pkg/risk/riskcontrol/circuit_break.go @@ -19,7 +19,6 @@ type CircuitBreakRiskControl struct { lossThreshold fixedpoint.Value haltedDuration time.Duration - isHalted bool haltedAt time.Time } @@ -59,10 +58,10 @@ func (c *CircuitBreakRiskControl) IsHalted(t time.Time) bool { c.profitStats.TodayPnL.Float64(), unrealized.Float64()) - c.isHalted = unrealized.Add(c.profitStats.TodayPnL).Compare(c.lossThreshold) <= 0 - if c.isHalted { + isHalted := unrealized.Add(c.profitStats.TodayPnL).Compare(c.lossThreshold) <= 0 + if isHalted { c.haltedAt = t } - return c.isHalted + return isHalted } diff --git a/pkg/strategy/common/strategy.go b/pkg/strategy/common/strategy.go index 4f159c7f6..4ad94e532 100644 --- a/pkg/strategy/common/strategy.go +++ b/pkg/strategy/common/strategy.go @@ -88,3 +88,7 @@ func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, se 24*time.Hour) } } + +func (s *Strategy) IsHalted(t time.Time) bool { + return s.circuitBreakRiskControl.IsHalted(t) +} diff --git a/pkg/strategy/fixedmaker/strategy.go b/pkg/strategy/fixedmaker/strategy.go index a54d055bb..fb4a7f226 100644 --- a/pkg/strategy/fixedmaker/strategy.go +++ b/pkg/strategy/fixedmaker/strategy.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "sync" + "time" "github.com/sirupsen/logrus" @@ -111,13 +112,12 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. session.UserDataStream.OnStart(func() { // you can place orders here when bbgo is started, this will be called only once. - s.replenish(ctx) }) s.activeOrderBook.OnFilled(func(order types.Order) { if s.activeOrderBook.NumOfOrders() == 0 { log.Infof("no active orders, replenish") - s.replenish(ctx) + s.replenish(ctx, order.UpdateTime.Time()) } }) @@ -125,7 +125,7 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo. log.Infof("%+v", kline) s.cancelOrders(ctx) - s.replenish(ctx) + s.replenish(ctx, kline.EndTime.Time()) }) // the shutdown handler, you can cancel all orders @@ -143,7 +143,12 @@ func (s *Strategy) cancelOrders(ctx context.Context) { } } -func (s *Strategy) replenish(ctx context.Context) { +func (s *Strategy) replenish(ctx context.Context, t time.Time) { + if s.IsHalted(t) { + log.Infof("circuit break halted, not replenishing") + return + } + submitOrders, err := s.generateSubmitOrders(ctx) if err != nil { log.WithError(err).Error("failed to generate submit orders")