Merge pull request #527 from andycheng123/improve/bollmaker-controller

Add StrategyController to bollmaker
This commit is contained in:
Yo-An Lin 2022-04-15 15:50:33 +08:00 committed by GitHub
commit 52b8f06e3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -160,6 +160,9 @@ type Strategy struct {
// neutralBoll is the neutral price section // neutralBoll is the neutral price section
neutralBoll *indicator.BOLL neutralBoll *indicator.BOLL
// StrategyController
status types.StrategyStatus
} }
func (s *Strategy) ID() string { func (s *Strategy) ID() string {
@ -239,6 +242,52 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu
return err return err
} }
// StrategyController
func (s *Strategy) GetStatus() types.StrategyStatus {
return s.status
}
func (s *Strategy) Suspend(ctx context.Context) error {
s.status = types.StrategyStatusStopped
// Cancel all order
if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil {
log.WithError(err).Errorf("graceful cancel order error")
s.Notify("graceful cancel order error")
} else {
s.Notify("All orders cancelled.")
}
s.tradeCollector.Process()
// Save state
if err := s.SaveState(); err != nil {
log.WithError(err).Errorf("can not save state: %+v", s.state)
} else {
log.Infof("%s position is saved.", s.Symbol)
}
return nil
}
func (s *Strategy) Resume(ctx context.Context) error {
s.status = types.StrategyStatusRunning
return nil
}
func (s *Strategy) EmergencyStop(ctx context.Context) error {
// Close 100% position
percentage, _ := fixedpoint.NewFromString("100%")
err := s.ClosePosition(ctx, percentage)
// Suspend strategy
_ = s.Suspend(ctx)
return err
}
func (s *Strategy) SaveState() error { func (s *Strategy) SaveState() error {
if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil { if err := s.Persistence.Save(s.state, ID, s.Symbol, stateKey); err != nil {
return err return err
@ -519,6 +568,9 @@ func (s *Strategy) adjustOrderQuantity(submitOrder types.SubmitOrder) types.Subm
} }
func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error {
// StrategyController
s.status = types.StrategyStatusRunning
if s.DisableShort { if s.DisableShort {
s.Long = &[]bool{true}[0] s.Long = &[]bool{true}[0]
} }
@ -568,6 +620,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.state.Position, s.orderStore) s.tradeCollector = bbgo.NewTradeCollector(s.Symbol, s.state.Position, s.orderStore)
s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) {
// StrategyController
if s.status != types.StrategyStatusRunning {
return
}
s.Notifiability.Notify(trade) s.Notifiability.Notify(trade)
s.state.ProfitStats.AddTrade(trade) s.state.ProfitStats.AddTrade(trade)
@ -613,6 +670,11 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se
}) })
session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { session.MarketDataStream.OnKLineClosed(func(kline types.KLine) {
// StrategyController
if s.status != types.StrategyStatusRunning {
return
}
if kline.Symbol != s.Symbol || kline.Interval != s.Interval { if kline.Symbol != s.Symbol || kline.Interval != s.Interval {
return return
} }