alert or alertcondition
Use
alert when the message carries a number computed on this bar, as above. Fire it only on a closed bar (barstate.isconfirmed): with one alert per bar, an alert raised on the first tick carries that tick’s price and cannot be taken back (see How to avoid a result that changes after the bar closes). Use alertcondition to offer the user a named condition
to set up themselves. alert fires at most once per bar, on realtime bars only, and only when the script runs as an alert; on
historical bars it does nothing, so nothing is raised while the script works through history. That is not a fault.
Strategies
A strategy’s orders are simulated andstrategy.entry and strategy.exit have no argument for an alert message, so a custom payload comes from alert
in the same script (see Webhooks for algo trading and Backtest a crossover):
The mistake to avoid
Puttingalertcondition in an if to make it conditional. The compiler says: alertcondition() must be called in the global scope.
The condition is its first argument; use alert when you need a branch. See Alerts.