From a3ca8326f2ddae90de17821bce8cd4f5bc8b2a00 Mon Sep 17 00:00:00 2001 From: austin362667 Date: Mon, 30 May 2022 15:18:50 +0800 Subject: [PATCH] strategy: refactor strategies using position interface --- pkg/strategy/ewoDgtrd/strategy.go | 2 +- pkg/strategy/grid/strategy.go | 2 +- pkg/strategy/pivotshort/strategy.go | 2 +- pkg/strategy/support/strategy.go | 8 ++++---- pkg/strategy/wall/strategy.go | 3 +-- pkg/strategy/xmaker/strategy.go | 2 +- 6 files changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/strategy/ewoDgtrd/strategy.go b/pkg/strategy/ewoDgtrd/strategy.go index 4c3defc07..aea493531 100644 --- a/pkg/strategy/ewoDgtrd/strategy.go +++ b/pkg/strategy/ewoDgtrd/strategy.go @@ -834,7 +834,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) { log.Infof("position changed: %s", position) s.Notify(s.Position) }) diff --git a/pkg/strategy/grid/strategy.go b/pkg/strategy/grid/strategy.go index af5a2bcab..5fb872bcc 100644 --- a/pkg/strategy/grid/strategy.go +++ b/pkg/strategy/grid/strategy.go @@ -620,7 +620,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } */ - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) { s.Notifiability.Notify(position) }) s.tradeCollector.BindStream(session.UserDataStream) diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index 6976ee4d8..baf79a264 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -179,7 +179,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) { log.Infof("position changed: %s", s.Position) s.Notify(s.Position) }) diff --git a/pkg/strategy/support/strategy.go b/pkg/strategy/support/strategy.go index 83768c56b..29392ddc1 100644 --- a/pkg/strategy/support/strategy.go +++ b/pkg/strategy/support/strategy.go @@ -499,13 +499,13 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se if !s.TrailingStopTarget.TrailingStopCallbackRatio.IsZero() { // Update trailing stop when the position changes - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) { // StrategyController if s.Status != types.StrategyStatusRunning { return } - if position.Base.Compare(s.Market.MinQuantity) > 0 { // Update order if we have a position + if position.(*types.Position).Base.Compare(s.Market.MinQuantity) > 0 { // Update order if we have a position // Cancel the original order if err := s.cancelOrder(s.trailingStopControl.OrderID, ctx, orderExecutor); err != nil { log.WithError(err).Errorf("Can not cancel the original trailing stop order!") @@ -515,12 +515,12 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se // Calculate minimum target price var minTargetPrice = fixedpoint.Zero if s.trailingStopControl.minimumProfitPercentage.Sign() > 0 { - minTargetPrice = position.AverageCost.Mul(fixedpoint.One.Add(s.trailingStopControl.minimumProfitPercentage)) + minTargetPrice = position.(*types.Position).AverageCost.Mul(fixedpoint.One.Add(s.trailingStopControl.minimumProfitPercentage)) } // Place new order if the target price is higher than the minimum target price if s.trailingStopControl.IsHigherThanMin(minTargetPrice) { - orderForm := s.trailingStopControl.GenerateStopOrder(position.Base) + orderForm := s.trailingStopControl.GenerateStopOrder(position.(*types.Position).Base) orders, err := s.submitOrders(ctx, orderExecutor, orderForm) if err != nil { log.WithError(err).Error("submit profit trailing stop order error") diff --git a/pkg/strategy/wall/strategy.go b/pkg/strategy/wall/strategy.go index 7fed0615c..a31d6c44d 100644 --- a/pkg/strategy/wall/strategy.go +++ b/pkg/strategy/wall/strategy.go @@ -302,7 +302,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } }) - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) { log.Infof("position changed: %s", s.Position) s.Notify(s.Position) }) @@ -340,7 +340,6 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se log.WithError(err).Errorf("can not place order") } - if err := s.activeAdjustmentOrders.GracefulCancel(ctx, s.session.Exchange); err != nil { log.WithError(err).Errorf("graceful cancel order error") } diff --git a/pkg/strategy/xmaker/strategy.go b/pkg/strategy/xmaker/strategy.go index 53ae10fde..a10a1d4bf 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -781,7 +781,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order } }) - s.tradeCollector.OnPositionUpdate(func(position *types.Position) { + s.tradeCollector.OnPositionUpdate(func(position types.PositionInterface) { s.Notifiability.Notify(position) }) s.tradeCollector.OnRecover(func(trade types.Trade) {