Functions

Alerts

Fire notifications from a script with alert() and alertcondition().

Script alerts are not available on kiyotaka.ai yet. alert() and alertcondition() compile and evaluate, but the chart cannot yet arm a kScript alert or deliver its notifications; the alerts you can create from the chart today are price alerts. This page documents the language so your scripts are ready when script alerts ship.

Alerts let a script notify you when something happens on the chart: a crossover, a level break, a regime change. You define the condition; the engine fires when it's met and delivers the message. There are two ways to raise one:

  • alert(...): fire inline with a dynamic, formatted message. Use it when you want to build the alert text from live values.
  • alertcondition(...): declare a named condition the chart can wire up to its alert system. Use it for reusable, user-configurable alerts.

Fire when X crosses Y

The most common alert is a crossover. Build the boolean condition, then pass it to alert along with the message and how often it may fire:

//@version=2
define(title="MA cross alert", position="onchart", axis=true)

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange)
timeseries fast = sma(source=trade.close, period=9)
timeseries slow = sma(source=trade.close, period=21)

// Fire once per bar when the fast MA crosses above the slow MA.
var goldenCross = crossover(seriesA=fast, seriesB=slow)
alert(message=format("Golden cross at {0}", trade.close), condition=goldenCross, freq="once_per_bar")

plotLine(value=fast, colors=["#16a34a"], width=2, label=["Fast"], desc=["9-bar SMA"])
plotLine(value=slow, colors=["#dc2626"], width=2, label=["Slow"], desc=["21-bar SMA"])

alert()

alert(message, condition, freq) fires whenever condition is true on a bar.

ArgumentTypeDescription
messagestringThe alert text. Build it dynamically with format(...) to include live values.
conditionbooleanThe alert fires on bars where this is true.
freqstringHow often it may re-fire, e.g. "once_per_bar".

alertcondition()

alertcondition(condition, title, message) declares a named alert the chart can attach to. Instead of firing directly, it exposes a condition users can select when creating an alert on your indicator.

ArgumentTypeDescription
conditionbooleanWhen the alert is eligible to fire.
titlestringThe name shown in the alert dropdown.
messagestringDefault message for the alert.
//@version=2
define(title="RSI overbought", position="offchart", axis=true)

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange)
var r = rsi(source=trade.close, period=14)

// Exposes "Overbought" as a selectable alert condition on this script.
alertcondition(condition=r > 70, title="Overbought", message="RSI above 70")

plotLine(value=r, colors=["#7c3aed"], width=2, label=["RSI"], desc=["14-bar RSI"])

Both, side by side

This script fires a direct alert on one bar and declares an alertcondition on another, so you can see how each is wired.

alerts_fire.ks
//@version=2
define(title="Alerts", position="offchart", axis=true)

timeseries trade = ohlcv(symbol=currentSymbol, exchange=currentExchange)
var directTrigger = barIndex == 10
var conditionTrigger = barIndex == 11

alert(message=format("direct alert at bar {0}", barIndex), condition=directTrigger, freq="once_per_bar")
alertcondition(condition=conditionTrigger, title="Condition Alert", message="alertcondition payload")

plotLine(value=trade.close, colors=["#2563eb"], width=2, label=["Close"], desc=["Close series"])

When this runs, the direct alert fires on bar 10 and the condition fires on bar 11, each carrying its message and once_per_bar frequency.