What You’ll Build
A custom TradingView indicator that detects RSI divergences and highlights them on your chart with visual alerts.
Prerequisites
- A free TradingView account
- Cursor IDE installed (free tier is enough)
- Basic understanding of what RSI is
Step 1: Set Up Cursor for Pine Script
Open Cursor and create a new file called rsi-divergence.pine.
Cursor doesn’t have built-in Pine Script support, but it understands the language well enough to generate and edit code.
Step 2: Describe Your Indicator
In Cursor’s AI chat (Cmd+K), type:
Write a TradingView Pine Script v5 indicator that:
1. Calculates RSI with a configurable period (default 14)
2. Detects bullish divergence (price makes lower low, RSI makes higher low)
3. Detects bearish divergence (price makes higher high, RSI makes lower high)
4. Plots arrows on the chart where divergences are found
5. Sends an alert when a divergence is detected
Step 3: Review the Generated Code
Cursor will generate something like:
//@version=5
indicator("AI RSI Divergence Detector", overlay=true)
// Inputs
rsiPeriod = input.int(14, "RSI Period", minval=1)
lookback = input.int(5, "Pivot Lookback", minval=1)
// RSI calculation
rsi = ta.rsi(close, rsiPeriod)
// Pivot detection
priceLow = ta.pivotlow(low, lookback, lookback)
priceHigh = ta.pivothigh(high, lookback, lookback)
rsiLow = ta.pivotlow(rsi, lookback, lookback)
rsiHigh = ta.pivothigh(rsi, lookback, lookback)
// Bullish divergence: price lower low + RSI higher low
bullDiv = not na(priceLow) and priceLow < ta.valuewhen(not na(priceLow), low[lookback], 1)
and rsiLow > ta.valuewhen(not na(rsiLow), rsi[lookback], 1)
// Bearish divergence: price higher high + RSI lower high
bearDiv = not na(priceHigh) and priceHigh > ta.valuewhen(not na(priceHigh), high[lookback], 1)
and rsiHigh < ta.valuewhen(not na(rsiHigh), rsi[lookback], 1)
// Plot signals
plotshape(bullDiv, title="Bullish Divergence", style=shape.triangleup,
location=location.belowbar, color=color.green, size=size.small)
plotshape(bearDiv, title="Bearish Divergence", style=shape.triangledown,
location=location.abovebar, color=color.red, size=size.small)
// Alerts
alertcondition(bullDiv, "Bullish RSI Divergence", "Bullish RSI divergence detected")
alertcondition(bearDiv, "Bearish RSI Divergence", "Bearish RSI divergence detected")
Step 4: Add It to TradingView
- Open TradingView and go to Pine Editor (bottom panel)
- Paste the generated code
- Click “Add to Chart”
- Switch to a 4H or Daily timeframe to see divergences
Step 5: Iterate with Cursor
Not happy with the results? Ask Cursor to refine:
- “Add a confirmation filter — only show divergences when volume is above average”
- “Color the candles when a divergence is active”
- “Add an input to toggle bullish/bearish independently”
Tips for Using AI with Pine Script
- Be specific about the version — always say “Pine Script v5” (refer to the Pine Script documentation for syntax details)
- Describe the visual output — tell the AI exactly what you want plotted
- Test on multiple timeframes — an indicator that works on 1H may not work on 1D
- Don’t trust blindly — always backtest before trading with any indicator
Next Steps
- Try Gemini instead — See how Google Gemini handles Pine Script (including chart screenshots) in our Gemini Pine Script tutorial
- See real results — Check out the 3 AI indicators I actually use after testing dozens of AI-generated indicators