pull out ishalted method

This commit is contained in:
narumi 2023-09-21 14:57:55 +08:00
parent 49e9c8bbcf
commit 4a231b10c6
3 changed files with 16 additions and 8 deletions

View File

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

View File

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

View File

@ -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")