Chapter 7 of 9

Pine Script and alerts

Automating indicators and alerts on TradingView.

Pine Script is TradingView's programming language. You don't need to become a programmer to use it: a few lines can draw your key levels automatically and alert you when price reaches them, so you can stop staring at the screen waiting.

Your first indicator

Open the Pine Editor at the bottom of a TradingView chart, paste this in and click Add to chart. It draws two moving averages, marks their crossovers and defines alerts for both.

EMA cross with alerts (Pine Script v6)

//@version=6
indicator("EMA cross with alerts", overlay = true)

fastLen = input.int(20, "Fast EMA")
slowLen = input.int(50, "Slow EMA")

fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
plot(fast, "Fast EMA", color.teal)
plot(slow, "Slow EMA", color.orange)

crossUp   = ta.crossover(fast, slow)
crossDown = ta.crossunder(fast, slow)
plotshape(crossUp,   "Cross up",   shape.triangleup,   location.belowbar, color.green)
plotshape(crossDown, "Cross down", shape.triangledown, location.abovebar, color.red)

alertcondition(crossUp,   "Fast crossed above slow", "{{ticker}}: fast EMA crossed above slow EMA")
alertcondition(crossDown, "Fast crossed below slow", "{{ticker}}: fast EMA crossed below slow EMA")

Something more useful for SMC and ICT

The previous day's high and low are some of the most important liquidity levels. This script draws them on any intraday chart and can alert you when either is taken.

Previous day high and low (Pine Script v6)

//@version=6
indicator("Previous day high and low", overlay = true)

// The previous day's high and low, without looking into the future.
pdh = request.security(syminfo.tickerid, "D", high[1], lookahead = barmerge.lookahead_on)
pdl = request.security(syminfo.tickerid, "D", low[1],  lookahead = barmerge.lookahead_on)

plot(pdh, "Previous day high", color.green, style = plot.style_stepline)
plot(pdl, "Previous day low",  color.red,   style = plot.style_stepline)

alertcondition(ta.crossover(high, pdh), "PDH taken", "{{ticker}} traded above the previous day high")
alertcondition(ta.crossunder(low, pdl), "PDL taken", "{{ticker}} traded below the previous day low")

Setting up an alert

  1. Add the indicator to your chart.
  2. Open the alert dialog (the clock icon, or right-click the chart and choose Add alert).
  3. Choose your indicator as the condition, then pick the named alert, such as "PDH taken".
  4. Choose how often it can fire (usually once per bar close), and where to send it: app, email or webhook.
Pine conceptWhat it does
indicator() / strategy()Declares a script: indicators draw; strategies can also be backtested
input.*()Settings you can change without editing code
ta.*Built-in technical functions: ema, rsi, crossover, highest, lowest…
request.security()Data from another timeframe or symbol
alertcondition()A named condition you can attach alerts to

Key takeaways

  • Pine Script is TradingView's language for custom indicators, strategies and alerts.
  • A few lines can draw your key levels and alert you when price reaches them.
  • alertcondition() defines alerts; request.security() pulls higher-timeframe data.
  • Watch for repainting, and start by automating tasks you already do by hand.