Pine Script functions, and getting their output into a command
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 - ma2f_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).
Look at the string before anything can trade on it
Building an Autoview command is just concatenating text. Before that text goes anywhere near an alert, print it where you can see it:
target = f_sma_diff(close, 10, 20)
commandText = "s=BTCUSDT b=buy q=1 t=market ftp=" + str.tostring(target)
if barstate.islast
label.new(bar_index, high, commandText, color=color.blue, textcolor=color.white)Run this on its own first, with no alert() call at all. The label on the chart's last bar shows the exact string commandText holds, including whatever number target resolved to. If that string reads wrong (a missing space, the wrong parameter name, a number with too many decimals), it's wrong before it's ever wired to anything that trades. Fix it here, confirm the label reads correctly, and only then move it into an alert() call.
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, string first, alert second
Step one: compute the value and build the command string, with nothing that can fire an order yet.
//@version=6
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)
commandText = "s=BTCUSDT b=buy q=1 t=market ftp=" + str.tostring(target)
if barstate.islast
label.new(bar_index, high, commandText, color=color.blue, textcolor=color.white)Confirm the label reads a real command, s=BTCUSDT b=buy q=1 t=market ftp=(a number), with f_target's output filled in for ftp=. Only after that checks out, step two: replace the label with the alert() call that actually sends it.
if longSignal
alert(commandText, alert.freq_once_per_bar)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.
Recreate the alert after you change the code
TradingView doesn't re-run your live editor code every time an alert fires. When you create an alert, TradingView takes a snapshot: as its own documentation puts it, it "saves a mirror image of the script and its inputs... at that moment." Editing f_target, commandText, or the alert() call afterward and saving the script does not update an alert already running from the old version. TradingView's own words: "Subsequent changes to your script's inputs or the chart will thus not affect running alerts previously created from them. If you want any changes to your context to be reflected in a running alert's behavior, you will need to delete the alert and create a new one." Every time you change the function or the string it builds, delete the old alert and recreate it against the updated script, rather than assuming a save carries forward.
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.