Using Pine Script Variables in Alerts: Indicators vs. Strategies

Master TradingView alert placeholders for indicators and strategies. Learn how to use {{ticker}}, {{strategy.order.action}}, and 20+ other dynamic variables to create powerful, automated alert messages.

pinescript-variables-complete-guide

When creating alerts in TradingView, you can use special placeholders to dynamically include variable values in the alert message. However, different placeholders apply depending on whether the alert is based on an indicator or a strategy. Below is a comprehensive breakdown of all available placeholders and their usage.

Placeholders for Indicators

These placeholders can be used when setting alerts based on indicators:

General Information

  • {{ticker}} – The symbol's ticker (e.g., AAPL, BTCUSD).
  • {{exchange}} – The exchange where the symbol is traded (e.g., NASDAQ, NYSE, CME). For delayed symbols, it may end with _DL or _DLY.
  • {{interval}} – The timeframe of the chart where the alert is created. On price-based alerts and Range charts, this always returns "1" (1-minute timeframe).

Bar Data (Applies to price alerts and indicator-based alerts)

  • {{close}}, {{open}}, {{high}}, {{low}} – The respective values of the bar that triggered the alert.
  • {{volume}} – The trading volume of the current bar.
  • {{time}} – The bar's timestamp in UTC (yyyy-MM-ddTHH:mm:ssZ format).
  • {{timenow}} – The exact time when the alert was triggered (differs from {{time}} if the alert is delayed).

Indicator-Specific Placeholders

  • {{plot_0}}, {{plot_1}}, ..., {{plot_19}} – Outputs from the indicator on which the alert is set. The numbering follows the order in which the series are defined in the indicator's Pine Script (up to 20 plots).
  • {{plot("Name")}} – Refers to an indicator's plot by its title (as set in plot(title="Name")).
  • For built-in indicators, the English name must be used when referencing plots.

Currency Information

  • {{syminfo.currency}} – The currency of the symbol (e.g., "USD", "EUR").
  • {{syminfo.basecurrency}} – The base currency of a forex pair (e.g., "EUR" for "EURUSD").

Example for a Volume indicator:

Volume: {{plot_0}}, Volume MA: {{plot_1}}

Placeholders for Strategies

When setting alerts on strategies, additional placeholders prefixed with {{strategy.}} provide trade execution details:

Order Execution Details

  • {{strategy.order.action}}"buy" or "sell" depending on the order executed.
  • {{strategy.order.contracts}} – The number of contracts traded.
  • {{strategy.order.price}} – The price at which the order was executed.
  • {{strategy.order.id}} – The unique ID of the executed order (set in strategy.entry, strategy.exit, or strategy.order).
  • {{strategy.order.comment}} – The order comment (if none, it defaults to strategy.order.id).
  • {{strategy.order.alert_message}} – A custom alert message set in strategy.entry, strategy.exit, or strategy.order (Pine v4+ required).

Position Tracking

  • {{strategy.position_size}} – The number of contracts in the current position (can be negative for short positions).
  • {{strategy.market_position}}"long", "flat", or "short", indicating the current market position.
  • {{strategy.market_position_size}} – The absolute size of the current position.
  • {{strategy.prev_market_position}} – The previous market position ("long", "flat", or "short").
  • {{strategy.prev_market_position_size}} – The absolute size of the previous position.

Using Placeholders in Pine Script

When using Pine Script, you can insert these placeholders into alert messages. Example for an indicator-based alert:

//@version=6
indicator("My Indicator", overlay=true)
plot(close, title="Series")
alertcondition(close > open, title="Price Alert", message="Price {{ticker}} = {{close}}")

For a strategy-based alert:

//@version=6
strategy("My Strategy", overlay=true)
strategy.entry("Long", strategy.long, when=close > open, alert_message="Trade executed: {{strategy.order.action}} at {{strategy.order.price}}")

Important Notes

  1. Case Sensitivity: All placeholders are case-sensitive and must use lowercase letters exactly as shown.
  2. Version Requirements: The {{strategy.order.alert_message}} placeholder requires Pine Script v4 or higher. All examples in this guide use Pine Script v6, the current version.
  3. Interval Behavior: The {{interval}} placeholder returns "1" for:
    • Price-based alerts (calculated on 1-minute data)
    • Range charts (built from 1-minute data)
  4. Plot Limitations: Only the first 20 plots (numbered 0-19) are accessible via placeholders.
  5. Built-in Indicators: When referencing plots by name in built-in indicators, you must use the English titles.

CrossTrade Integration: Automated Trading with Strategy Sync

CrossTrade's advanced trading platform can harness these TradingView placeholders for seamless automated trading. Our new Strategy Sync engine ensures your NinjaTrader 8 account stays perfectly synchronized with your TradingView strategy.

Example Alert Configuration

Here's how to configure a TradingView alert message to work with CrossTrade's automated trading system. Note that CrossTrade primarily utilizes strategy-related placeholders for automated trading, while indicator placeholders are mainly for informational purposes:

key=your-secret-key;
command=PLACE;
account=Sim101;
instrument=NQ1!;
action={{strategy.order.action}};
qty={{strategy.order.contracts}};
order_type=MARKET;
tif=DAY;

// --- The New Sync Engine ---
sync_strategy=true;
market_position={{strategy.market_position}};
prev_market_position={{strategy.prev_market_position}};
out_of_sync=flatten;

How Strategy Sync Works

  1. Dynamic Order Execution: The {{strategy.order.action}} and {{strategy.order.contracts}} placeholders automatically populate the trade direction and size based on your strategy's signals.
  2. Position Synchronization: The {{strategy.market_position}} and {{strategy.prev_market_position}} placeholders allow CrossTrade to track your strategy's intended position state.
  3. Automatic Reconciliation: When sync_strategy=true is enabled, our system continuously monitors for discrepancies between your TradingView strategy and live account positions.
  4. Out-of-Sync Protection: The out_of_sync=flatten parameter instructs the system to flatten all positions if synchronization is lost, protecting you from unintended exposure.
💡
Important Note: CrossTrade's automated trading functionality is designed specifically for strategy-based alerts. While all TradingView placeholders can be included in alert messages, CrossTrade's trading engine only processes strategy-related placeholders ({{strategy.*}}) for order execution and position management. Indicator placeholders like {{plot_0}} or basic OHLCV data serve informational purposes in your alerts but do not trigger automated trades.

Final Thoughts

TradingView's alert placeholder system provides traders with a powerful toolkit for creating dynamic, informative alert messages. Whether you're working with simple indicator-based alerts using basic placeholders like {{ticker}} and {{close}}, or building sophisticated strategy alerts that leverage position tracking with {{strategy.market_position}} and order details via {{strategy.order.action}}, these placeholders transform static alert messages into rich, contextual notifications.

The key to success lies in understanding which placeholders apply to your specific use case—indicators vs. strategies—and leveraging Pine Script v6's capabilities to create clean, maintainable code. With over 20 different placeholders available, from basic OHLCV data to advanced strategy position tracking, you have everything needed to build professional-grade alert systems that provide precise, actionable information exactly when you need it.

Remember to pay attention to case sensitivity, version requirements, and the specific behavior of placeholders like {{interval}} to ensure your alerts work reliably across all market conditions and timeframes.


Ready to automate your TradingView strategies with CrossTrade?

Start your free trial

Try CrossTrade for 7 days.

Sign Up