Home Forums Trading view What is the main difference between alert() and alertcondition() in Pine Script?

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

  • Author
    Posts
    • 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?

    • 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.

Viewing 1 reply thread
×

Start a Discussion

Get help from the AlgoDelta community.

×

Welcome Back!

Enter your email to sign in or create an account. No passwords needed.

⚠️

Delete This?

Are you sure you want to delete this? This action is permanent and cannot be undone.

Scroll to Top