Use Cursor to Write a TradingView Pine Script Indicator

intermediate 25 min · · By Alpha Guy · cursor pine-script

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

  1. Open TradingView and go to Pine Editor (bottom panel)
  2. Paste the generated code
  3. Click “Add to Chart”
  4. 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

  1. Be specific about the version — always say “Pine Script v5” (refer to the Pine Script documentation for syntax details)
  2. Describe the visual output — tell the AI exactly what you want plotted
  3. Test on multiple timeframes — an indicator that works on 1H may not work on 1D
  4. Don’t trust blindly — always backtest before trading with any indicator

Next Steps

Disclaimer: This article is for educational purposes only and is not financial advice. Trading cryptocurrencies involves substantial risk of loss. Past performance does not guarantee future results. Always do your own research before making any trading decisions. Read full disclaimer →
Alpha Guy
Alpha Guy

Founder of VibeTradingLab. Ex-Goldman Sachs engineer, 2025 Binance Top 1% Trader. Writes about using AI tools to build trading systems that actually work. Currently nomading between Bali, Dubai, and the Mediterranean.

Got stuck? Have questions?

Join our Telegram group to ask questions, share your bots, and connect with other AI traders.

Join Telegram