From bb94d4a1bddc9a29ba19fadcf91b002523e22963 Mon Sep 17 00:00:00 2001 From: austin362667 Date: Fri, 13 May 2022 18:05:25 +0800 Subject: [PATCH] pivotshort: clean up strategy --- pkg/strategy/pivotshort/pivot.go | 119 --------------------- pkg/strategy/pivotshort/pivot_callbacks.go | 15 --- pkg/strategy/pivotshort/strategy.go | 35 ++++-- 3 files changed, 27 insertions(+), 142 deletions(-) delete mode 100644 pkg/strategy/pivotshort/pivot.go delete mode 100644 pkg/strategy/pivotshort/pivot_callbacks.go diff --git a/pkg/strategy/pivotshort/pivot.go b/pkg/strategy/pivotshort/pivot.go deleted file mode 100644 index 5b16619dd..000000000 --- a/pkg/strategy/pivotshort/pivot.go +++ /dev/null @@ -1,119 +0,0 @@ -package pivotshort - -import ( - "fmt" - "time" - - "github.com/c9s/bbgo/pkg/indicator" - "github.com/c9s/bbgo/pkg/types" -) - -var zeroTime time.Time - -type KLineValueMapper func(k types.KLine) float64 - -//go:generate callbackgen -type Pivot -type Pivot struct { - types.IntervalWindow - - // Values - Lows types.Float64Slice // higher low - Highs types.Float64Slice // lower high - - EndTime time.Time - - UpdateCallbacks []func(valueLow, valueHigh float64) -} - -func (inc *Pivot) LastLow() float64 { - if len(inc.Lows) == 0 { - return 0.0 - } - return inc.Lows[len(inc.Lows)-1] -} - -func (inc *Pivot) LastHigh() float64 { - if len(inc.Highs) == 0 { - return 0.0 - } - return inc.Highs[len(inc.Highs)-1] -} - -func (inc *Pivot) calculateAndUpdate(klines []types.KLine) { - if len(klines) < inc.Window { - return - } - - var end = len(klines) - 1 - var lastKLine = klines[end] - - if inc.EndTime != zeroTime && lastKLine.GetEndTime().Before(inc.EndTime) { - return - } - - var recentT = klines[end-(inc.Window-1) : end+1] - - l, h, err := calculatePivot(recentT, inc.Window, KLineLowPriceMapper, KLineHighPriceMapper) - if err != nil { - log.WithError(err).Error("can not calculate pivots") - return - } - inc.Lows.Push(l) - inc.Highs.Push(h) - - if len(inc.Lows) > indicator.MaxNumOfVOL { - inc.Lows = inc.Lows[indicator.MaxNumOfVOLTruncateSize-1:] - } - if len(inc.Highs) > indicator.MaxNumOfVOL { - inc.Highs = inc.Highs[indicator.MaxNumOfVOLTruncateSize-1:] - } - - inc.EndTime = klines[end].GetEndTime().Time() - - inc.EmitUpdate(l, h) - -} - -func (inc *Pivot) handleKLineWindowUpdate(interval types.Interval, window types.KLineWindow) { - if inc.Interval != interval { - return - } - - inc.calculateAndUpdate(window) -} - -func (inc *Pivot) Bind(updater indicator.KLineWindowUpdater) { - updater.OnKLineWindowUpdate(inc.handleKLineWindowUpdate) -} - -func calculatePivot(klines []types.KLine, window int, valLow KLineValueMapper, valHigh KLineValueMapper) (float64, float64, error) { - length := len(klines) - if length == 0 || length < window { - return 0., 0., fmt.Errorf("insufficient elements for calculating VOL with window = %d", window) - } - var lows types.Float64Slice - var highs types.Float64Slice - for _, k := range klines { - lows.Push(valLow(k)) - highs.Push(valHigh(k)) - } - - pl := 0. - if lows.Min() == lows.Index(int(window/2.)-1) { - pl = lows.Min() - } - ph := 0. - if highs.Max() == highs.Index(int(window/2.)-1) { - ph = highs.Max() - } - - return pl, ph, nil -} - -func KLineLowPriceMapper(k types.KLine) float64 { - return k.Low.Float64() -} - -func KLineHighPriceMapper(k types.KLine) float64 { - return k.High.Float64() -} diff --git a/pkg/strategy/pivotshort/pivot_callbacks.go b/pkg/strategy/pivotshort/pivot_callbacks.go deleted file mode 100644 index 1a921eb00..000000000 --- a/pkg/strategy/pivotshort/pivot_callbacks.go +++ /dev/null @@ -1,15 +0,0 @@ -// Code generated by "callbackgen -type Pivot"; DO NOT EDIT. - -package pivotshort - -import () - -func (inc *Pivot) OnUpdate(cb func(valueLow float64, valueHigh float64)) { - inc.UpdateCallbacks = append(inc.UpdateCallbacks, cb) -} - -func (inc *Pivot) EmitUpdate(valueLow float64, valueHigh float64) { - for _, cb := range inc.UpdateCallbacks { - cb(valueLow, valueHigh) - } -} diff --git a/pkg/strategy/pivotshort/strategy.go b/pkg/strategy/pivotshort/strategy.go index e3ffc76eb..692b8ae76 100644 --- a/pkg/strategy/pivotshort/strategy.go +++ b/pkg/strategy/pivotshort/strategy.go @@ -5,6 +5,7 @@ import ( "fmt" "github.com/c9s/bbgo/pkg/bbgo" "github.com/c9s/bbgo/pkg/fixedpoint" + "github.com/c9s/bbgo/pkg/indicator" "github.com/c9s/bbgo/pkg/types" "github.com/sirupsen/logrus" ) @@ -52,7 +53,7 @@ type Strategy struct { session *bbgo.ExchangeSession - pivot *Pivot + pivot *indicator.Pivot // StrategyController bbgo.StrategyController @@ -186,7 +187,7 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se iw := types.IntervalWindow{Window: s.PivotLength, Interval: s.Interval} st, _ := session.MarketDataStore(s.Symbol) - s.pivot = &Pivot{IntervalWindow: iw} + s.pivot = &indicator.Pivot{IntervalWindow: iw} s.pivot.Bind(st) session.UserDataStream.OnStart(func() { @@ -194,6 +195,10 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se }) var lastLow fixedpoint.Value + futuresMode := s.session.Futures || s.session.IsolatedFutures + d := s.CatBounceRatio.Div(s.NumLayers) + q := s.Quantity.Div(s.NumLayers) + log.Info(futuresMode) session.MarketDataStream.OnKLineClosed(func(kline types.KLine) { if kline.Symbol != s.Symbol || kline.Interval != s.Interval { @@ -230,19 +235,33 @@ func (s *Strategy) Run(ctx context.Context, orderExecutor bbgo.OrderExecutor, se if !lastLow.IsZero() { - futuresMode := s.session.Futures - d := s.CatBounceRatio.Div(s.NumLayers) - q := s.Quantity.Div(s.NumLayers) for i := 0; i < int(s.NumLayers.Float64()); i++ { balances := s.session.GetAccount().Balances() quoteBalance, _ := balances[s.Market.QuoteCurrency] baseBalance, _ := balances[s.Market.BaseCurrency] p := lastLow.Mul(fixedpoint.One.Add(s.CatBounceRatio.Sub(fixedpoint.NewFromFloat(d.Float64() * float64(i))))) - if (futuresMode && q.Mul(p).Compare(quoteBalance.Available) < 0) || q.Compare(baseBalance.Available) < 0 { - s.placeOrder(ctx, p, q, orderExecutor) - s.tradeCollector.Process() + // + if futuresMode { + //log.Infof("futures mode on ") + if q.Mul(p).Compare(quoteBalance.Available) < 0 { + s.placeOrder(ctx, p, q, orderExecutor) + s.tradeCollector.Process() + } + } else if s.Environment.IsBackTesting() { + //log.Infof("spot backtest mode on ") + if q.Compare(baseBalance.Available) < 0 { + s.placeOrder(ctx, p, q, orderExecutor) + s.tradeCollector.Process() + } + } else { + //log.Infof("spot mode on ") + if q.Compare(baseBalance.Available) < 0 { + s.placeOrder(ctx, p, q, orderExecutor) + s.tradeCollector.Process() + } } + } //s.placeOrder(ctx, lastLow.Mul(fixedpoint.One.Add(s.CatBounceRatio)), s.Quantity, orderExecutor) }