Pine Script Dynamic Alert Messages
Builds alert text at runtime with str.tostring — symbol, price, size — instead of static dialog placeholders.
//@version=6
strategy("Dynamic Alert Message", overlay=true, calc_on_every_tick=false)
// ── Inputs ──────────────────────────────────────────────
qty = input.int(1, "Contracts", minval=1)
// ── Runtime message builder ──────────────────────────────
// {{ticker}} / {{close}} placeholders work only in the alert
// DIALOG message box — alert() strings must be built in code.
buildMsg(action) =>
action + " " + syminfo.ticker + " x" + str.tostring(qty) +
" @ " + str.tostring(close, format.mintick) + " [" + timeframe.period + "]"
// ── Entry logic (replace with your own conditions) ───────
longCondition = ta.crossover(ta.ema(close, 9), ta.ema(close, 21))
shortCondition = ta.crossunder(ta.ema(close, 9), ta.ema(close, 21))
if longCondition
strategy.entry("Long", strategy.long, qty=qty)
alert(buildMsg("BUY"), alert.freq_once_per_bar_close)
if shortCondition
strategy.entry("Short", strategy.short, qty=qty)
alert(buildMsg("SELL"), alert.freq_once_per_bar_close)
Settings
qty— contract quantity included in the message and the entry. Default 1.
How to use
- Paste into the Pine Editor and replace the EMA-crossover placeholder with your own entry conditions.
- Extend
buildMsgwith anything you track — stop price, target, session P&L — viastr.tostring. - Use
format.mintickfor prices so the string always matches the symbol’s tick grid. - For webhook JSON payloads, see the TradersPost and PickMyTrade snippets — same pattern, JSON-shaped.
Frequently Asked Questions
How do I include the price in a Pine Script alert?
Concatenate str.tostring(close, format.mintick) into the string you pass to alert(). format.mintick rounds to the symbol’s tick size so you never send a price the exchange cannot accept. The same pattern works for any series — stops, targets, position size.
What placeholders can I use in TradingView alerts?
Dialog placeholders like {{ticker}}, {{close}}, {{strategy.order.action}} and {{strategy.order.contracts}} are replaced by TradingView when the alert fires — but only in the alert dialog message box and alertcondition() text. Strings passed to the alert() function are not placeholder-processed; build those with str.tostring and concatenation instead.
Related
Want a complete eval-ready strategy with alerts done right?
This snippet is a building block. Our paid scripts are complete strategies — entries, safety rules, and runtime alert payloads pre-wired for automation, tested on 3 years of MES/NQ data. Instant delivery from $50.
View Plans — From $50