RSI divergence detector
Marks bars where price closes at a short-run extreme while RSI sits at the opposite extreme; about six a day.
RSIDivergenceDetector looks for price and RSI pulling in opposite directions over a short run. On a bar where the close is the lowest of the last half of divergence_window bars (rounded up, so 10 at the default 20) and the RSI is the highest of those same bars, the bar is marked bullish. The mirror case, a close at the top with the RSI at the bottom, is marked bearish. The RSI is the balance of up-moves against down-moves over rsi_window bars, each move weighted by the log of its bar's volume, scaled from -1 to +1.
How to read it
- Markers sit on the candle that sets the new short-run low (bullish) or high (bearish), not on a swing point.
- Bullish: the close is the lowest of the last 10 bars while RSI is the highest of those bars. Bearish is the reverse.
- Markers are sparse: 121 in a month of SPY one-minute bars at defaults, about six a day.
- Treat a marker as a flag to examine on the chart, not a signal to trade on.
What it outputs
- Divergence
- True on a bullish divergence (close at a short-run low, RSI at a short-run high). False on a bearish one (close at a short-run high, RSI at a short-run low). No marker on other bars.
When it fires
True on the bar whose close is the lowest of the last 10 while RSI is the highest of its last 10 (bullish). False on the mirror bar, a close at the highest with RSI at its lowest (bearish). Read it as a short-run extreme that RSI did not share, not a confirmed turn.
Settings
- rsi_window
- Bars of moves the RSI balances. Shorter makes RSI twitchier, so it sets short-run highs and lows more often. Longer smooths it, so its turns line up with a new price extreme less often. 1 never fires.
- divergence_window
- The test looks back over half this many bars, rounded up (10 bars at 20). From 3 up, each increase only removes markers, since the test must beat more bars. 1 and 2 never fire, because only one bar is left to compare.
Pitfalls
- Roll-off effect: both markers need RSI to move against the bar's move, which happens only when a bigger weighted move leaves the window rsi_window bars back. The turn may be an old move dropping out.
- Not the textbook divergence, which compares two price swing lows with two RSI lows. This compares the latest bar with the recent bars and checks nothing after it, so it flags a new extreme, not a confirmed turn.
- The sample counts are for default settings only, and no test shows what price did after a marker.