Creating Alerts in TradingView (With Pine Script)

📘 Creating Alerts with Pine Script (2 Options)

TradingView supports two methods for creating alerts using Pine Script — both compatible with Xerolite. This guide shows you how to implement both:

  • Option 1: Use alert() function in Pine Script to hardcode the alert message
  • Option 2: Use strategy.entry() or strategy.order() with TradingView placeholders

⚙️ Option 1: alert() Function (Fully in Code)

This method hardcodes the entire JSON alert inside your Pine Script. Best for simple Buy/Sell logic.

//@version=5
indicator("MA Crossover", overlay=true)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)

longCond = ta.crossover(fast, slow)
shortCond = ta.crossunder(fast, slow)

if (longCond)
    alert('{"action":"BUY","symbol":"AAPL","qty":"10","currency":"USD","asset_class":"STOCK","exch":"SMART"}', alert.freq_once_per_bar)

if (shortCond)
    alert('{"action":"SELL","symbol":"AAPL","qty":"10","currency":"USD","asset_class":"STOCK","exch":"SMART"}', alert.freq_once_per_bar)
  1. Open TradingView and go to the chart you want
  2. Click the “Pine Editor” and paste your script
  3. Click “Add to Chart”
  4. Create alert with “Any alert() function call” condition
  5. Paste your Xerolite webhook URL

❌ No placeholders supported. All values are fixed in code.


⚙️ Option 2: strategy.entry() + Dynamic Placeholders

This method uses Pine Script for logic and the TradingView UI for dynamic alert configuration using {{placeholders}}.

//@version=5
strategy("MA Crossover Strategy", overlay=true)
fast = ta.sma(close, 10)
slow = ta.sma(close, 30)

longCond = ta.crossover(fast, slow)
shortCond = ta.crossunder(fast, slow)

if (longCond)
    strategy.entry("Buy", strategy.long, qty=10)

if (shortCond)
    strategy.entry("Sell", strategy.short, qty=10)
  1. Add this script to your chart via Pine Editor
  2. Click “Add to Chart”
  3. Click 🔔 Alert → Select “Strategy” as condition
  4. Setup your xerolite Webhook URL
  5. Paste this JSON message with placeholders:
{
  "action": "{{strategy.order.action}}",
  "qty": "{{strategy.order.contracts}}",
  "symbol": "AAPL",
  "currency": "{{syminfo.currency}}",
  "order_type": "LIMIT",
  "price": "{{strategy.order.price}}",
  "asset_class": "STOCK",
  "exch": "SMART"
}

✅ Placeholders let you insert live values from your strategy like action, quantity, or order price.

📘 Recommended for advanced users who want to automate dynamically.

See: MA Crossover Strategy Automation with Webhooks

👉 Full list of TradingView placeholders supported by Xerolite


📌 Summary

Featurealert() Methodstrategy.entry() + UI
Code LocationFully in scriptPart in script + TradingView UI
Placeholders Support
Dynamic Qty / Price
Beginner-Friendly⚠️ Intermediate
Recommended ForSimple alertsFlexible strategy automation

🛠️ Smart Alert Builder

Use the Xerolite Smart Alert Builder to generate:

  • ✅ Static JSON for alert()
  • ✅ Dynamic JSON with TradingView placeholders
Xerolite Smart Alert Builder

✅ It helps prevent typos and ensures full compatibility with Xerolite webhook format.

🔗 Related Guides