Divergence

Price makes a new swing extreme while RSI or MACD momentum does not.

Regular divergence (the TradingView Divergence Indicator rule) between the last two pivots of the oscillator (RSI of the close, or the MACD histogram of the close). A pivot low is an oscillator value strictly below the pivot_left values before it and the pivot_right values after it (pivot high: strictly above); it is known on the candle pivot_right candles after it. Each new pivot is compared with the previous pivot of its kind when the candles between them are min_spacing to max_spacing. bull: the low at the new pivot candle is below the low at the previous one and the oscillator is higher; bear: the high is above the previous high and the oscillator is lower. bull / bear are true on the candle the new pivot is known. bull_pivot_price / bear_pivot_price hold the low / high of the newer pivot candle of the last bullish / bearish divergence until the next one of that kind.

Divergence is a structure detector: it marks price structure on the chart and counts against the plan's detector limit.

Settings

SettingDefaultRangeWhat it does
oscrsirsi, macd_histOscillator compared with price.
rsi_length141 to 500RSI length.
fast121 to 500Fast EMA length.
slow262 to 500Slow EMA length.
signal91 to 500Signal EMA length.
pivot_left51 to 100Pivot lookback left: candles before a pivot whose oscillator value it must pass.
pivot_right51 to 100Pivot lookback right: candles after a pivot whose oscillator value it must pass (the divergence is known this many candles after the pivot).
min_spacing51 to 500Fewest candles between the two pivots.
max_spacing602 to 1000Most candles between the two pivots.

What it gives

OutputWhat it is
Bullish divergenceTrue on the candle a bullish divergence is confirmed.
Bearish divergenceTrue on the candle a bearish divergence is confirmed.
Bullish divergence pivot lowLow of the newer pivot candle of the last bullish divergence.
Bearish divergence pivot highHigh of the newer pivot candle of the last bearish divergence.

What it reads

It reads the whole candle (open, high, low, close and volume).

Warm-up

With its default settings, Divergence needs 211 closed candles before its first value. A backtest loads them before its start date, so the first trade can already use it.

Test it

Copy this sentence into the Algonney chat to backtest it:

Backtest a strategy on BTC that uses Divergence with its default settings