Back to TradingView PineScript for CFD Traders
Part 4

Alerts and automation: from signal to notification

Alerts in Pine fire on conditions. Routing them to email, webhook, or your broker's automation API is what closes the loop.

Advanced10 min readBeginner → AdvancedLesson 16 / 17
Try it in TradingView
Simple alert + structured JSON payload

alertcondition gives you a manual alert in the UI. The alert() call emits JSON you can route to a webhook — right-click the chart → Add Alert to wire it up.

//@version=5
indicator("Lesson 16 - Alerts", overlay=true)

fast = ta.ema(close, 9)
slow = ta.ema(close, 21)

plot(fast, "Fast EMA", color=color.blue, linewidth=2)
plot(slow, "Slow EMA", color=color.orange, linewidth=2)

longSignal = ta.crossover(fast, slow)

// Simple alert — pick "Golden cross" from the alert dialog's condition list.
alertcondition(longSignal, title="Golden cross",
     message="EMA cross long on {{ticker}} at {{close}}")

// Structured JSON — route this to a webhook to automate execution.
if longSignal
    alert('{"action":"buy","ticker":"' + syminfo.ticker +
          '","price":"'  + str.tostring(close) +
          '","stop":"'   + str.tostring(close * 0.98) +
          '","target":"' + str.tostring(close * 1.04) + '"}',
          alert.freq_once_per_bar_close)

plotshape(longSignal, "Long", shape.triangleup, location.belowbar,
          color=color.green, size=size.small)
To run it
  1. 1. Open any chart in TradingView and click Pine Editor at the bottom.
  2. 2. Select everything in the editor and paste this over it, then click Save.
  3. 3. Click Add to chart. Open the Strategy Tester tab if it is a strategy.

What an alert actually is

A Pine alert is a condition attached to a script. When the condition becomes true on the latest bar, TradingView sends a notification — email, SMS, push notification, or webhook. For a strategy, the built-in alert types are "Order fills only" and "Order fills and ...", which fire on every simulated order.

The custom alert pattern

longSignal = ta.crossover(fast, slow)
alertcondition(longSignal, title="Long signal", message="SMA cross long on {{ticker}} at {{close}}")

alertcondition takes a condition (series bool), a title, and a message template. Placeholders like {{ticker}} and {{close}} are substituted at firing time.

Strategy alerts

For strategies, use the built-in alert type "Order fills only" — Pine emits a JSON payload describing each fill. Most automation platforms (3Commas, Pineconnector, custom brokers) consume this JSON.

Webhook destinations

TradingView paid plans let you route alerts to a webhook URL. You post the alert payload to your own server, which translates it into a broker API call. This is how a Pine strategy fires a live order through a broker that does not have native TradingView integration.

JSON message template

strategy.entry("Long", strategy.long, when=longSignal)
strategy.exit("X", "Long", stop=close - stopDist, limit=close + stopDist * 2)
if strategy.position_size > 0 and strategy.position_size[1] == 0
    alert('{"action":"buy","ticker":"' + syminfo.ticker + '","qty":' + str.tostring(contracts) + ',"sl":' + str.tostring(close - stopDist) + ',"tp":' + str.tostring(close + stopDist * 2) + '}', alert.freq_once_per_bar_close)

The alert() function lets you fire a custom JSON message on a specific bar event. Use alert.freq_once_per_bar_close for "fire only when the bar closes and the condition is still true" — this avoids intra-bar whipsaw.

From signal to fill

Pine does not place live orders. The chain is: Pine fires alert → webhook → your server → broker API → live fill. Each hop is a place where something can break, so test on paper first.

What to take from this

The strategy is the easy part. The hard part is the wiring. Start with paper trading the alert messages, validate the chain, then go live with the smallest size your broker allows.

What you just did

Lesson 16 of 17 in TradingView PineScript for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.