CRODL
Terms·Docs
Blog/Pine Script Indicator Alert Template for TradingView Webhooks
Pine Script Indicator Alert Template for TradingView Webhooks
Trading—

Pine Script Indicator Alert Template for TradingView Webhooks

Copy this Pine Script v6 alertcondition template for stop-loss and four take-profit events, or give the included prompt to your AI coding assistant.

Pine Script indicators do not produce strategy order-fill events, but they can expose named alert conditions for entries, stop losses, take profits, and any other boolean signal calculated by the indicator.

The template below adds separate conditions for a fixed stop loss and four take-profit levels in each direction. You can copy the alert block into an existing indicator or give the included prompt to an AI coding assistant and ask it to connect the conditions without changing the indicator's calculations.

This is an indicator alert pattern built with alertcondition(). If your script starts with strategy() and you want a webhook when an order fills, use the Pine Script strategy order-fill alert template instead.

Copy the Pine Script v6 Indicator Alert Template

Replace the ten placeholder booleans with the conditions already calculated by your indicator. Keep every alertcondition() call at the script's global scope; do not put them inside an if block.

// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// INDICATOR ALERT TEMPLATE — named SL and TP events for TradingView alerts.
//
// HOW TO USE:
//   1. Replace the PLACEHOLDER CONDITIONS with your indicator's existing events.
//   2. Keep the alertcondition() calls at global scope.
//   3. Add the indicator to a chart, create an alert, and select one named condition.
//   4. For a Crodl webhook, replace the alert's Message with the matching trigger JSON.
//   5. Repeat for every SL/TP action you want TradingView to send.
//@version=6
indicator("Indicator Alert Template", "IndAlertTmpl", overlay = true)

// ═════════════ PLACEHOLDER CONDITIONS — replace with your own ═════════════
// These values deliberately stay false until they are connected to real logic.
bool longSLhit = false
bool shortSLhit = false
bool longTPhit = false
bool shortTPhit = false
bool longTPhit2 = false
bool shortTPhit2 = false
bool longTPhit3 = false
bool shortTPhit3 = false
bool longTPhit4 = false
bool shortTPhit4 = false

// ═════════════ ALERT CONDITIONS — copy this reusable block ═════════════
// {{close}} is the current price of the bar when the condition triggers.
alertcondition(condition = longSLhit, title = "Buy Fixed SL", message = "Buy Fixed SL Hit @ {{close}}")
alertcondition(condition = shortSLhit, title = "Sell Fixed SL", message = "Sell Fixed SL Hit @ {{close}}")

alertcondition(condition = longTPhit, title = "Buy Take Profit 1", message = "Buy TP 1 Hit @ {{close}}")
alertcondition(condition = shortTPhit, title = "Sell Take Profit 1", message = "Sell TP 1 Hit @ {{close}}")
alertcondition(condition = longTPhit2, title = "Buy Take Profit 2", message = "Buy TP 2 Hit @ {{close}}")
alertcondition(condition = shortTPhit2, title = "Sell Take Profit 2", message = "Sell TP 2 Hit @ {{close}}")
alertcondition(condition = longTPhit3, title = "Buy Take Profit 3", message = "Buy TP 3 Hit @ {{close}}")
alertcondition(condition = shortTPhit3, title = "Sell Take Profit 3", message = "Sell TP 3 Hit @ {{close}}")
alertcondition(condition = longTPhit4, title = "Buy Take Profit 4", message = "Buy TP 4 Hit @ {{close}}")
alertcondition(condition = shortTPhit4, title = "Sell Take Profit 4", message = "Sell TP 4 Hit @ {{close}}")

This corrects two easy-to-miss problems in copied alert blocks: every string has a matching quote, and the fourth short take-profit message uses the same naming order as the other messages.

The template uses {{close}} because it is normally the most useful built-in placeholder for the price when the event is evaluated. Use {{open}} only if you intentionally want the opening price of the alert's bar—it is not automatically the stop-loss or take-profit level that was hit.

TradingView documents the available behavior and placeholders in its official alertcondition() guide and alerts FAQ.

Report the Calculated SL or TP Level

If the indicator already calculates an exact stop or target price, expose that numeric series through a hidden plot and reference the plot by title. This reports the calculated level instead of the bar's open or close.

// Example variables supplied by your existing indicator:
float longSLPrice = na
bool longSLhit = false

plot(longSLPrice, title = "Long SL Price", display = display.none)
alertcondition(
    condition = longSLhit,
    title = "Buy Fixed SL",
    message = 'Buy Fixed SL Hit @ {{plot("Long SL Price")}}'
)

The message remains a constant string. TradingView replaces the plot placeholder with the plotted value when the alert fires.

Give This Prompt to Your AI

Copy the prompt below, paste your complete indicator after it, and send both to your AI coding assistant.

Add TradingView indicator alerts to the Pine Script indicator below.

Requirements:
1. Do not change the indicator's signal calculations, plots, inputs, state, timing, or repaint behavior.
2. Keep its existing Pine Script version unless a version change is strictly required; explain any required change.
3. Reuse the indicator's existing boolean events for long/short fixed-stop hits and take-profit levels 1 through 4. Do not invent replacement trading logic when an equivalent event already exists.
4. Add ten alertcondition() calls at global scope. Do not place them inside if blocks, loops, or functions.
5. Use these exact titles: Buy Fixed SL, Sell Fixed SL, Buy Take Profit 1-4, and Sell Take Profit 1-4.
6. Give each condition a clear constant message. Use {{close}} for the trigger-bar price, or add a hidden titled plot and use {{plot("Plot Title")}} when an existing calculated SL/TP price should be reported.
7. Make each condition true only on the intended hit event. If the existing variable remains true for several bars, convert it to a one-bar event without changing the underlying signal.
8. Preserve all existing variable names where possible and avoid duplicate alerts for the same event.
9. Do not add {{strategy.order.alert_message}}. That placeholder is for strategy order fills, not indicators.
10. Return the complete, compiling Pine Script and briefly map each alert title to the condition it uses.

Here is my indicator:

[PASTE YOUR COMPLETE PINE SCRIPT HERE]

After the AI returns the modified script, compare it with the original. Only the alert wiring—and any minimal one-bar event guard that was genuinely required—should have changed.

Create the TradingView Indicator Alerts

After adding the indicator to a chart:

  1. Open Create Alert in TradingView.
  2. Select your indicator in Condition.
  3. Select one named condition, such as Buy Take Profit 1.
  4. Choose the trigger frequency. Once Per Bar Close is the safer default for confirmed, non-repainting signals; use an intrabar frequency only when the indicator was deliberately designed and tested for intrabar alerts.
  5. For a notification-only alert, keep or edit the human-readable message supplied by the script.
  6. For Crodl automation, delete that text and paste the complete JSON payload from the matching Crodl trigger into Message.
  7. Enable the webhook notification and use https://api.crodl.com/webhooks/trading as the webhook URL.
  8. Save the alert, then repeat these steps for each action that needs a different payload.

Each alertcondition() call appears as its own selectable condition. Creating one alert for Buy Take Profit 1 does not automatically activate the other nine conditions.

Important: Do not put {{strategy.order.alert_message}} in an indicator alert. Indicators do not create strategy order fills, so there is no order-specific message for that placeholder to retrieve. For indicator automation, paste the matching Crodl JSON directly into each TradingView alert's Message field.

TradingView saves a server-side snapshot of the indicator, its inputs, the symbol, and the timeframe when the alert is created. Delete and recreate alerts after changing the script or its settings.

Important Integration Checks

Make hit conditions one-bar events

If longTPhit stays true for five bars, TradingView may treat it as true on each eligible evaluation. Prefer an event that becomes true only when the target is first crossed or first marked as hit.

Choose bar-close or intrabar behavior deliberately

An alert can fire intrabar only if the script executes and the condition becomes true during the realtime bar. Values can change before the bar closes. Use Once Per Bar Close when the indicator's signal should be confirmed by the final candle.

Use separate Crodl payloads

A long stop, short stop, and each partial take profit may need different close directions or percentages. Create the corresponding close trigger in Crodl and paste its complete JSON into the matching TradingView alert.

Test every path

Start with demo trading or the smallest live size available. Trigger and verify long SL, short SL, and every enabled TP level before increasing risk.

Frequently Asked Questions

Can alertcondition() use a message from input.text_area()?

No. The message argument must be a constant string known when the script compiles. It can contain TradingView placeholders, but it cannot use an input or a string that changes from bar to bar. You can still replace the pre-filled message manually when creating the TradingView alert.

When should I use alert() instead?

Use alert() when the script must build a dynamic series-string message in Pine or when you intentionally want one selectable Any alert() function call alert to receive several events. Use alertcondition() when users should be able to select separately named conditions in TradingView.

Why does the template use {{close}} instead of {{open}}?

{{open}} is the opening price of the bar where the condition triggered. It is not necessarily the SL or TP price. {{close}} reports the bar's current or final close, while a hidden plot placeholder is the better choice for an exact calculated level.

Can I use this block inside a strategy?

Use alert_message on strategy order calls when the webhook must follow simulated order fills. TradingView's documentation notes that alertcondition() events are selectable for indicators, not strategies. See the strategy alert template for the order-fill pattern.


This content is for educational purposes only and does not constitute financial advice. Cryptocurrency trading involves significant risk of loss. Past performance does not guarantee future results.

Share this article

Crodl

Ready to automate your trading?

Connect your exchange, set up automations, and start trading smarter — all from one platform.

Start Trading Free

More articles

Pine Script Strategy Alert Template for TradingView Webhooks
Trading
Pine Script Strategy Alert Template for TradingView Webhooks
Breadth Trend
Indicators
Breadth Trend
All articles