mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
Merge pull request #1318 from c9s/narumi/common-risk
CHORE: add IsHalted method to common.Strategy for CircuitBreakRiskControl
This commit is contained in:
commit
cf31796224
|
@ -19,7 +19,6 @@ type CircuitBreakRiskControl struct {
|
||||||
lossThreshold fixedpoint.Value
|
lossThreshold fixedpoint.Value
|
||||||
haltedDuration time.Duration
|
haltedDuration time.Duration
|
||||||
|
|
||||||
isHalted bool
|
|
||||||
haltedAt time.Time
|
haltedAt time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,10 +58,10 @@ func (c *CircuitBreakRiskControl) IsHalted(t time.Time) bool {
|
||||||
c.profitStats.TodayPnL.Float64(),
|
c.profitStats.TodayPnL.Float64(),
|
||||||
unrealized.Float64())
|
unrealized.Float64())
|
||||||
|
|
||||||
c.isHalted = unrealized.Add(c.profitStats.TodayPnL).Compare(c.lossThreshold) <= 0
|
isHalted := unrealized.Add(c.profitStats.TodayPnL).Compare(c.lossThreshold) <= 0
|
||||||
if c.isHalted {
|
if isHalted {
|
||||||
c.haltedAt = t
|
c.haltedAt = t
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.isHalted
|
return isHalted
|
||||||
}
|
}
|
||||||
|
|
|
@ -88,3 +88,7 @@ func (s *Strategy) Initialize(ctx context.Context, environ *bbgo.Environment, se
|
||||||
24*time.Hour)
|
24*time.Hour)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Strategy) IsHalted(t time.Time) bool {
|
||||||
|
return s.circuitBreakRiskControl.IsHalted(t)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
@ -111,13 +112,12 @@ func (s *Strategy) Run(ctx context.Context, _ bbgo.OrderExecutor, session *bbgo.
|
||||||
|
|
||||||
session.UserDataStream.OnStart(func() {
|
session.UserDataStream.OnStart(func() {
|
||||||
// you can place orders here when bbgo is started, this will be called only once.
|
// 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) {
|
s.activeOrderBook.OnFilled(func(order types.Order) {
|
||||||
if s.activeOrderBook.NumOfOrders() == 0 {
|
if s.activeOrderBook.NumOfOrders() == 0 {
|
||||||
log.Infof("no active orders, replenish")
|
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)
|
log.Infof("%+v", kline)
|
||||||
|
|
||||||
s.cancelOrders(ctx)
|
s.cancelOrders(ctx)
|
||||||
s.replenish(ctx)
|
s.replenish(ctx, kline.EndTime.Time())
|
||||||
})
|
})
|
||||||
|
|
||||||
// the shutdown handler, you can cancel all orders
|
// 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)
|
submitOrders, err := s.generateSubmitOrders(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Error("failed to generate submit orders")
|
log.WithError(err).Error("failed to generate submit orders")
|
||||||
|
|
Loading…
Reference in New Issue
Block a user