From e575236db8f113d4765328e3d5a9755f1ca67dbe Mon Sep 17 00:00:00 2001 From: c9s Date: Fri, 10 Jun 2022 01:21:59 +0800 Subject: [PATCH] pivotshort: adjust shadow ratio calculation --- config/pivotshort-ETHUSDT.yaml | 7 +++++++ pkg/bbgo/activeorderbook.go | 6 ++++-- pkg/strategy/pivotshort/strategy.go | 5 ++--- pkg/strategy/xmaker/strategy.go | 3 +-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/config/pivotshort-ETHUSDT.yaml b/config/pivotshort-ETHUSDT.yaml index 1186ecb8c..cccbe4a70 100644 --- a/config/pivotshort-ETHUSDT.yaml +++ b/config/pivotshort-ETHUSDT.yaml @@ -35,11 +35,18 @@ exchangeStrategies: roiStopLossPercentage: 1% # roiTakeProfitPercentage is the take profit percentage of the position ROI (currently the price change) + # force to take the profit ROI exceeded the percentage. roiTakeProfitPercentage: 25% # lowerShadowRatio is used to force taking profit when the (lower shadow height / low price) > lowerShadowRatio + # you can grab a simple stats by the following SQL: + # SELECT ((close - low) / close) AS shadow_ratio FROM binance_klines WHERE symbol = 'ETHUSDT' AND `interval` = '5m' AND start_time > '2022-01-01' ORDER BY shadow_ratio DESC LIMIT 20; lowerShadowRatio: 3% + cumulatedVolume: + minVolume: 50_000 + window: 5 + marginOrderSideEffect: repay backtest: diff --git a/pkg/bbgo/activeorderbook.go b/pkg/bbgo/activeorderbook.go index e5037bc70..d678eab53 100644 --- a/pkg/bbgo/activeorderbook.go +++ b/pkg/bbgo/activeorderbook.go @@ -69,6 +69,8 @@ func (b *ActiveOrderBook) waitAllClear(ctx context.Context, waitTime, timeout ti // GracefulCancel cancels the active orders gracefully func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) error { + waitTime := CancelOrderWaitTime + log.Debugf("[ActiveOrderBook] gracefully cancelling %s orders...", b.Symbol) startTime := time.Now() @@ -86,9 +88,9 @@ func (b *ActiveOrderBook) GracefulCancel(ctx context.Context, ex types.Exchange) log.WithError(err).Errorf("[ActiveOrderBook] can not cancel %s orders", b.Symbol) } - log.Debugf("[ActiveOrderBook] waiting %s for %s orders to be cancelled...", CancelOrderWaitTime, b.Symbol) + log.Debugf("[ActiveOrderBook] waiting %s for %s orders to be cancelled...", waitTime, b.Symbol) - clear, err := b.waitAllClear(ctx, CancelOrderWaitTime, 5*time.Second) + clear, err := b.waitAllClear(ctx, waitTime, 5*time.Second) if clear || err != nil { break } diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index e5c017bf3..72f14ca6d 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -298,7 +298,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se } return - } else if roi.Compare(s.Exit.RoiTakeProfitPercentage) > 0 { + } else if roi.Compare(s.Exit.RoiTakeProfitPercentage) > 0 { // disable this condition temporarily s.Notify("%s TakeProfit triggered at price %f, ROI take profit percentage by %s", s.Symbol, kline.Close.Float64(), roi.Percentage(), kline) if err := s.activeMakerOrders.GracefulCancel(ctx, s.session.Exchange); err != nil { log.WithError(err).Errorf("graceful cancel order error") @@ -307,8 +307,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se if err := s.ClosePosition(ctx, fixedpoint.One); err != nil { log.WithError(err).Errorf("close position error") } - return - } else if !s.Exit.LowerShadowRatio.IsZero() && kline.GetLowerShadowHeight().Div(kline.Low).Compare(s.Exit.LowerShadowRatio) > 0 { + } else if !s.Exit.LowerShadowRatio.IsZero() && kline.GetLowerShadowHeight().Div(kline.Close).Compare(s.Exit.LowerShadowRatio) > 0 { s.Notify("%s TakeProfit triggered at price %f: shadow ratio %f", s.Symbol, kline.Close.Float64(), kline.GetLowerShadowRatio().Float64(), kline) if err := s.activeMakerOrders.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 41adb9f9d..c69e40a78 100644 --- a/pkg/strategy/xmaker/strategy.go +++ b/pkg/strategy/xmaker/strategy.go @@ -807,8 +807,7 @@ func (s *Strategy) CrossRun(ctx context.Context, orderExecutionRouter bbgo.Order defer tradeScanTicker.Stop() defer func() { - if err := s.activeMakerOrders.GracefulCancel(context.Background(), - s.makerSession.Exchange); err != nil { + if err := s.activeMakerOrders.GracefulCancel(context.Background(), s.makerSession.Exchange); err != nil { log.WithError(err).Errorf("can not cancel %s orders", s.Symbol) } }()