From c9ba81fcbb3719b385711bf9d974b4e70cba1eff Mon Sep 17 00:00:00 2001 From: Andy Cheng Date: Fri, 6 May 2022 16:52:00 +0800 Subject: [PATCH] strategy: Update bollmaker to support new strategy controller --- pkg/strategy/bollmaker/strategy.go | 69 +++++++++++------------------- 1 file changed, 26 insertions(+), 43 deletions(-) diff --git a/pkg/strategy/bollmaker/strategy.go b/pkg/strategy/bollmaker/strategy.go index ac4e311eb..6d485f60e 100644 --- a/pkg/strategy/bollmaker/strategy.go +++ b/pkg/strategy/bollmaker/strategy.go @@ -168,7 +168,7 @@ type Strategy struct { neutralBoll *indicator.BOLL // StrategyController - status types.StrategyStatus + bbgo.StrategyController } func (s *Strategy) ID() string { @@ -252,45 +252,6 @@ func (s *Strategy) ClosePosition(ctx context.Context, percentage fixedpoint.Valu 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 are cancelled.") - } - - s.tradeCollector.Process() - - return s.Persistence.Sync(s) -} - -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 -} - // Deprecated: LoadState method is migrated to the persistence struct tag. func (s *Strategy) LoadState() error { var state State @@ -544,7 +505,29 @@ func (s *Strategy) adjustOrderQuantity(submitOrder types.SubmitOrder) types.Subm func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, session *bbgo.ExchangeSession) error { // StrategyController - s.status = types.StrategyStatusRunning + s.Status = types.StrategyStatusRunning + + s.OnSuspend(func() { + 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 are cancelled.") + } + + s.tradeCollector.Process() + + _ = s.Persistence.Sync(s) + }) + + s.OnEmergencyStop(func() { + // Close 100% position + percentage := fixedpoint.NewFromFloat(1.0) + _ = s.ClosePosition(ctx, percentage) + }) if s.DisableShort { s.Long = &[]bool{true}[0] @@ -616,7 +599,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se s.tradeCollector.OnTrade(func(trade types.Trade, profit, netProfit fixedpoint.Value) { // StrategyController - if s.status != types.StrategyStatusRunning { + if s.Status != types.StrategyStatusRunning { return } @@ -666,7 +649,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { // StrategyController - if s.status != types.StrategyStatusRunning { + if s.Status != types.StrategyStatusRunning { return }