T2strategyRSIPythonStrategy Design

Building an RSI Divergence Bot from Scratch

Building an RSI Divergence Bot from Scratch

RSI divergence is one of the most reliable reversal signals in technical analysis. In this deep-dive we walk through identifying divergence programmatically, filtering false signals, and wiring it into a live trading loop.

12 min readFeb 28, 2026

RSI divergence is a momentum disagreement. Price keeps printing new extremes while momentum stops confirming. That mismatch can be useful, but only if you define it with precise rules and strict context filters.

RSI chart with divergence markings
Mark divergence only on confirmed swing points. Mid-candle guesses create noisy entries.

Define Divergence Like a Machine

Most traders overfit divergence by drawing lines manually. A bot needs deterministic structure: what is a swing high, what is a swing low, and how far apart pivots must be before they count.

  • Bullish divergence: price lower low, RSI higher low.
  • Bearish divergence: price higher high, RSI lower high.
  • Minimum pivot distance: at least 5 to 8 candles between pivots.
  • Context filter: only take long setups when higher timeframe trend is neutral or up.

Reference Detection Logic

QuantumEdge

Explore these ideas in live bot templates

See how this setup translates into production-ready workflows.

Browse QuantumEdge bot templates
python
import pandas as pd

def compute_rsi(close: pd.Series, period: int = 14) -> pd.Series:
    delta = close.diff()
    gain = delta.clip(lower=0)
    loss = -delta.clip(upper=0)
    avg_gain = gain.ewm(alpha=1 / period, min_periods=period).mean()
    avg_loss = loss.ewm(alpha=1 / period, min_periods=period).mean()
    rs = avg_gain / avg_loss
    return 100 - (100 / (1 + rs))

def bullish_divergence(price_ll_prev, price_ll_curr, rsi_ll_prev, rsi_ll_curr):
    return price_ll_curr < price_ll_prev and rsi_ll_curr > rsi_ll_prev

Execution Filters That Remove Bad Signals

  • Ignore setups during major macro event windows.
  • Require at least one reversal candle after divergence detection.
  • Reject entries if spread widens past a pre-defined threshold.
  • Cancel if your stop placement violates max risk-per-trade.

In live markets, most divergence events fail because momentum can stay weak for a long time. The confirmation layer matters more than the indicator math.

Deployment Checklist

  • Paper trade for 100 plus signals before touching live capital.
  • Use fixed fractional risk sizing and cap concurrent exposure.
  • Track slippage per venue and session, not just overall PnL.
  • Enable a drawdown kill switch and a circuit breaker for API instability.

QuantumEdge

Ready to test this in your own account?

Create your QuantumEdge account and move from theory to execution.

Start on QuantumEdge

Related Articles

QuantumEdge

Want similar strategies already organized for deployment?

Explore bot library