PineHookPineHook

Strategy Alert Messages

How to use {{strategy.order.alert_message}} for dynamic, programmatic signals.

Instead of hardcoding a signal in the alert message field, you can define it directly in your Pine Script strategy code. This gives you dynamic control over every aspect of the signal at the time it fires.

Why Use Strategy Alert Messages?#

When you put {{strategy.order.alert_message}} in the alert message field, TradingView replaces it with the message defined in your strategy code. This lets you:

  • Calculate lot size, SL, and TP from live values like ATR or account equity.
  • Handle buy, sell, and close signals from a single alert.
  • Change signal parameters in code or via strategy inputs without editing the alert.

Quick Setup#

Step 1: Add Alert Messages to Your Strategy#

Use the alert_message parameter in your entry and exit functions:

//@version=5
strategy("My Strategy", overlay=true)

fastMA = ta.sma(close, 10)
slowMA = ta.sma(close, 20)

longCondition = ta.crossover(fastMA, slowMA)
shortCondition = ta.crossunder(fastMA, slowMA)

var string license = "YOUR_LICENSE_KEY"
var string symbol = syminfo.ticker
var string buyMessage = str.tostring(license) + ",buy," + str.tostring(symbol) + ",size_lots=0.1,sl_pips=50,tp_pips=100"
var string sellMessage = str.tostring(license) + ",sell," + str.tostring(symbol) + ",size_lots=0.1,sl_pips=50,tp_pips=100"

if (longCondition)
    strategy.entry("Long", strategy.long, alert_message = buyMessage)

if (shortCondition)
    strategy.entry("Short", strategy.short, alert_message = sellMessage)

Step 2: Create the Alert#

  1. Right-click the chart and select Add Alert.
  2. In the Condition dropdown, select your Pine Script strategy.
  3. In the Message tab, enter only:
{{strategy.order.alert_message}}
  1. In the Notifications tab, set the webhook URL to https://relay.pinehook.io/webhook.
  2. Click Create.

TradingView will use the message defined in your strategy code each time the alert fires.

Example: ATR-Based Strategy#

This example calculates stop loss and take profit dynamically from ATR:

//@version=5
strategy("ATR Strategy with PineHook", overlay=true)

licenseKey = input.string("YOUR_LICENSE_KEY", "PineHook License Key")
sizeLots = input.float(0.1, "Lot Size", minval=0.01, step=0.01)
atrMultiplierSL = input.float(1.5, "ATR Multiplier for SL")
atrMultiplierTP = input.float(3.0, "ATR Multiplier for TP")

atr = ta.atr(14)
fastMA = ta.sma(close, 10)
slowMA = ta.sma(close, 20)

slPips = math.round(atr * atrMultiplierSL * 10000)
tpPips = math.round(atr * atrMultiplierTP * 10000)

var string symbol = syminfo.ticker
buyMessage = str.tostring(licenseKey) + ",buy," + str.tostring(symbol) + ",size_lots=" + str.tostring(sizeLots) + ",sl_pips=" + str.tostring(slPips) + ",tp_pips=" + str.tostring(tpPips)
sellMessage = str.tostring(licenseKey) + ",sell," + str.tostring(symbol) + ",size_lots=" + str.tostring(sizeLots) + ",sl_pips=" + str.tostring(slPips) + ",tp_pips=" + str.tostring(tpPips)
var string closeLongMessage = str.tostring(licenseKey) + ",closelong," + str.tostring(symbol)
var string closeShortMessage = str.tostring(licenseKey) + ",closeshort," + str.tostring(symbol)

longCondition = ta.crossover(fastMA, slowMA)
shortCondition = ta.crossunder(fastMA, slowMA)

if (longCondition)
    strategy.entry("Long", strategy.long, alert_message = buyMessage)

if (shortCondition)
    strategy.entry("Short", strategy.short, alert_message = sellMessage)

if (shortCondition and strategy.position_size > 0)
    strategy.close("Long", alert_message = closeLongMessage)

if (longCondition and strategy.position_size < 0)
    strategy.close("Short", alert_message = closeShortMessage)

Strategy Functions That Support alert_message#

FunctionUse Case
strategy.entry()Opening positions
strategy.close()Closing specific positions
strategy.close_all()Closing all positions
strategy.exit()Setting SL/TP exits
strategy.cancel()Canceling pending orders

Multiple Order Types#

One strategy can handle all signal types:

//@version=5
strategy("Multi-Signal Strategy", overlay=true)

var string licenseKey = "YOUR_LICENSE_KEY"
var string symbol = syminfo.ticker
var string buyMessage = str.tostring(licenseKey) + ",buy," + str.tostring(symbol) + ",size_lots=0.1,sl_pips=50,tp_pips=100"
var string sellMessage = str.tostring(licenseKey) + ",sell," + str.tostring(symbol) + ",size_lots=0.1,sl_pips=50,tp_pips=100"
var string closeLongMessage = str.tostring(licenseKey) + ",closelong," + str.tostring(symbol)
var string closeShortMessage = str.tostring(licenseKey) + ",closeshort," + str.tostring(symbol)
var string closeAllMessage = str.tostring(licenseKey) + ",closeall," + str.tostring(symbol)

buySignal = ta.crossover(ta.rsi(close, 14), 30)
sellSignal = ta.crossunder(ta.rsi(close, 14), 70)
exitSignal = ta.cross(ta.rsi(close, 14), 50)

if (buySignal)
    strategy.entry("Long", strategy.long, alert_message = buyMessage)

if (sellSignal)
    strategy.entry("Short", strategy.short, alert_message = sellMessage)

if (exitSignal)
    strategy.close_all(alert_message = closeAllMessage)

JSON Works Too

All examples on this page use CSV format. JSON strings work the same way, just be careful with quote escaping. CSV is generally simpler for Pine Script string building.

Best Practices#

Store your license key as an input: Use input.string() so you can update it from strategy settings without editing the code, as shown in the ATR example above.

Escape quotes carefully when using JSON:

// Single quotes around the string
alert_message='{"key":"value"}'

// Or escaped double quotes
alert_message="{\"key\":\"value\"}"

Debug your messages with labels:

if (longCondition)
    label.new(bar_index, high, buyMessage, color=color.green, textcolor=color.white, size=size.small)

Test in the Strategy Tester first: Confirm alert messages are formatted correctly and signals trigger at the right time before creating live alerts.

Common Issues#

Message not updating:

  • Confirm you're using {{strategy.order.alert_message}} exactly in the alert message field.
  • Recreate the alert after modifying your strategy code.
  • Check that the strategy compiles without errors.

Invalid signal format:

  • Validate your JSON syntax if using JSON format.
  • Check for missing commas, unclosed quotes, or numbers wrapped in quotes.
  • Switch to CSV format to avoid quoting issues entirely.

Next Steps#