What is the main difference between alert() and alertcondition() in Pine Script?

Asked by Radhesh Joshi · SOLVED
1 reply 283 views Jump to solution Reply
Radhesh Joshi Original Post
Strategy Builder

I’m creating alerts for my TradingView indicator and I noticed that Pine Script provides both alert() and alertcondition().

I’m a little confused about why there are two different functions and when I should use each one.

Can someone explain the main difference between alert() and alertcondition() with a simple example?

Posted Aug 8
Reply

    1 Reply

    Tech Support AlgoDelta
    Tech Support AlgoDelta Automation Expert ·

    Yes sir, both are used for creating alerts in Pine Script, but they work differently and are useful in different situations.

    –alertcondition() is mainly used to create a specific alert condition that appears as an option when creating an alert from the TradingView interface.

    For example:

    //@version=6
    indicator("Alert Condition Example", overlay=true)
    
    fastEMA = ta.ema(close, 9)
    slowEMA = ta.ema(close, 21)
    
    buySignal = ta.crossover(fastEMA, slowEMA)
    
    alertcondition(
        buySignal,
        title="Buy Signal",
        message="Buy Signal Generated"
    )

    After adding this indicator to the chart, you can select the Buy Signal condition while creating a TradingView alert.

    –alert() is used to trigger an alert directly from your Pine Script when your code reaches that statement.

    For example:

    if buySignal
        alert("BUY Signal Generated", alert.freq_once_per_bar_close)

    Here, the alert is triggered when buySignal becomes true.

    So the main difference is:

    -> alertcondition() → Defines an alert condition that can be selected from TradingView’s alert creation window.

    -> alert() → Directly triggers an alert from inside the script when the specified condition is reached.

    Another important difference is that alert() allows you to create dynamic messages using values calculated by your script, which is useful when you want to send changing information such as price, indicator values, or signal details.

    => If you want to expose predefined conditions for users to select while creating an alert, alertcondition() is a good choice.

    => If you want your script to trigger alerts programmatically with dynamic messages, alert() is generally more suitable.

    So both functions are related to alerts, but the way they are configured and triggered is different.

    Verified Solution
      Markdown supported · ```json for code blocks · paste or drop screenshots

      Be specific and kind. Never share API keys, passwords or personal account details.

      Welcome to the community

      Log in or create your account in under a minute.

      1. 1Mobile
      2. 2E-mail
      3. 3Password
      +91

      Have a password?

      By continuing you agree to the community guidelines. We’ll send a one-time code by SMS.