Pine Script functions, and getting their output into a command

Autoview · Guide · Updated July 20, 2026

Pine Script functions, built-in or your own, compute a number. On their own they just sit inside the script; nothing trades until that number reaches an alert. This page covers both halves: the functions themselves, and the specific mechanism, alert() plus str.tostring(), that carries a computed value out of Pine Script and into a command Autoview can execute. It applies to any script, indicators included, not just a strategy().

Built-in functions do the math for you

Pine Script's ta. namespace covers most of the technical-analysis math you'd otherwise have to write by hand. A few common ones:

  • ta.sma(source, length), the simple moving average.
  • ta.ema(source, length), the exponential moving average.
  • ta.rsi(source, length), the Relative Strength Index.

Call one by name with its arguments and it returns the computed value for the current bar:

rsi = ta.rsi(close, 14)

That's a 14-period RSI on the close price, recalculated every bar.

Writing your own function

When the built-ins don't cover what you need, write a function with =>. There's no return keyword in Pine Script. A function's result is whatever its last line evaluates to, whether that's a single expression right after => or the last statement of a multi-line indented block.

f_sma_diff(src, len1, len2) =>
    ma1 = ta.sma(src, len1)
    ma2 = ta.sma(src, len2)
    ma1 - ma2

f_sma_diff takes a source series and two lengths, computes two moving averages, and its last line, ma1 - ma2, is what the function returns. Call it anywhere in the script: diff = f_sma_diff(close, 10, 20).

The part that matters for automation: getting a number out

A computed value only becomes tradeable once it's inside an alert message Autoview receives. Pine Script gives you two functions for firing an alert, and only one of them can carry a value that changes bar to bar.

  • alertcondition() takes a constant message, fixed at compile time. It can't include a live function result; it can only use TradingView's own placeholders, like {{plot_0}}.
  • alert(message, freq) takes a dynamic message. Because it accepts a runtime string, you can build it with concatenation, and the number that lands in it can be different on every bar.

The freq argument controls how often alert() can fire: alert.freq_once_per_bar (the default) once per bar while the condition holds, alert.freq_once_per_bar_close only after the bar closes, alert.freq_all on every realtime update.

The function that turns a number into text for that message is str.tostring():

alert("s=BTCUSDT b=buy q=1 t=market ftp=" + str.tostring(target), alert.freq_once_per_bar)

str.tostring(target) converts the numeric variable target to a string, and + concatenates it onto the rest of the command. Whatever target holds on the bar the alert fires is exactly what lands in Autoview's ftp= parameter.

A worked example

Combine a custom function's output with str.tostring() and the whole command builds itself:

//@version=5
indicator("Example", overlay=true)

f_target(src, len) =>
    src + ta.sma(src, len) * 0.01

longSignal = ta.crossover(ta.sma(close, 10), ta.sma(close, 30))
target = f_target(close, 20)

if longSignal
    alert("s=BTCUSDT b=buy q=1 t=market ftp=" + str.tostring(target), alert.freq_once_per_bar)

f_target computes a take-profit level from the close and a moving average. When the fast SMA crosses over the slow one, the script fires an alert() whose message is a real Autoview command, s=BTCUSDT b=buy q=1 t=market ftp=(the number), with f_target's output for that bar filled in as the ftp= value. ftp (Fixed Take Profit) and its counterpart fsl (Fixed Stop Loss) attach a conditional order at that price the moment the position exists. See stops, targets, and trailing for the full parameter set.

Point this alert's webhook at your Autoview URL the same way you would any other alert. See automate TradingView alerts if you haven't wired one up before.

One symbol gotcha to know about

If a function-built command targets OANDA, don't reach for {{ticker}} for the symbol. It returns the exchange's raw concatenated form, EURUSD, but Autoview's OANDA parser requires the underscored form, EUR_USD. That mismatch fails silently at the exchange, not in Pine Script. Build the symbol into your string as a literal, "s=EUR_USD ", rather than pulling it from a placeholder when OANDA is the destination.

Test before you trust it

A function-built alert is still a webhook post, so dry-run it first. Add d=1 to the concatenated message and Autoview logs what it would have done instead of sending the order. Let a few signals fire, check the log for the side, size, and computed value you expected, then drop d=1 once it matches.

A note on what Autoview is. Autoview is an execution tool, not a trading or investment advisor. It places the orders your script and alerts tell it to; it doesn't generate signals or make any claim about results. Trading carries risk of loss, and you're responsible for the strategy you automate. See our disclosures.

Read the command reference