Pine Script Dynamic Alert Messages

Builds alert text at runtime with str.tostring — symbol, price, size — so the payload always reflects the live bar instead of a static template.

Pine Script v6 Free · TradingView free plan
Pine Script
//@version=6
strategy("Target Filled - Dynamic Alert Example", overlay=true, calc_on_every_tick=false)

// --- Inputs ---------------------------------------------------
contracts = input.int(1, "Order Size", minval=1)

// --- Runtime message builder --------------------------------------
// {{ticker}} / {{close}} style placeholders only work in the alert
// DIALOG's message box. Strings passed to alert() must be built in code.
buildMsg(side) =>
    side + " " + syminfo.ticker + " x" + str.tostring(contracts) +
     " @ " + str.tostring(close, format.mintick) + " on " + timeframe.period

// --- Sample signal — replace with your own entry logic ---------
fastMA = ta.ema(close, 9)
slowMA = ta.ema(close, 21)

if ta.crossover(fastMA, slowMA)
    strategy.entry("Long", strategy.long, qty=contracts)
    alert(buildMsg("BUY"), alert.freq_once_per_bar_close)

if ta.crossunder(fastMA, slowMA)
    strategy.entry("Short", strategy.short, qty=contracts)
    alert(buildMsg("SELL"), alert.freq_once_per_bar_close)

Settings

InputDefaultPurpose
contracts1Contract quantity used for the entry and echoed into the alert text.

How to use

  • Paste into the Pine Editor and swap the EMA-crossover placeholder for your own entry conditions.
  • Extend buildMsg with anything else worth tracking — stop price, target, session P&L — via more str.tostring calls.
  • Use format.mintick whenever a price gets embedded, so the string always lands on the symbol's tick grid.
  • For a JSON-shaped payload aimed at a specific automation bridge, see the TradersPost and PickMyTrade webhook snippets — same idea, different output format.

Frequently Asked Questions

How do I include the current price in a Pine Script alert?
Concatenate str.tostring(close, format.mintick) into whatever string gets passed to alert(). format.mintick rounds to the symbol's own tick size, so the alert never sends a price the exchange would reject. The same pattern works for anything else worth tracking — a stop, a target, a position size.
What placeholders work in TradingView alerts?
Dialog placeholders like {{ticker}}, {{close}}, {{strategy.order.action}}, and {{strategy.order.contracts}} get substituted by TradingView, but only inside the alert dialog's message box and inside alertcondition() text. A string handed to the alert() function isn't placeholder-processed at all — build it with str.tostring and plain concatenation instead, as this snippet does.

Get a Pine Script tuned to your prop firm.

Invite-only on TradingView within 24 hours. From $19/mo, cancel anytime.