Pine Script basics, from syntax to your first command

Autoview · Guide · Updated July 20, 2026

This page covers the fundamentals of Pine Script itself, the syntax, variables, data types, operators, and control structures you need before anything else makes sense, and ends with the simplest possible way to turn a condition into a live Autoview command. If you already know Pine Script and just want the automation mechanics, see Pine Script strategy alerts or Pine Script functions instead. This page is the level below both.

Every script starts with a declaration

A Pine Script needs exactly one declaration statement before anything else, and it decides what the rest of the script is allowed to do:

//@version=5
indicator("My Script")

indicator() is for scripts that plot or signal without placing simulated trades; strategy() adds backtesting and, with it, the {{strategy.order.*}} alert placeholders covered in the strategy alerts guide. Pick strategy() only if you want that backtest and those placeholders. Everything below works the same in either.

Variables: = declares, := reassigns

= creates a variable and gives it its first value. := changes the value of a variable that already exists. Mixing them up is a common early mistake, Pine won't let you := a name it hasn't seen a = for yet.

candleColor = color.white
if close > open
    candleColor := color.green
if close < open
    candleColor := color.red

candleColor is declared once with =, then conditionally reassigned with := depending on which if block runs.

Data types, and what "series" actually means

Pine Script's fundamental types are int, float, bool, color, and string. series is not a sixth type sitting alongside those, it's a qualifier, one of four (const, input, simple, series) that describe when a value becomes known rather than what kind of value it is. A series float is still a float, the series part just means its value can be different on every bar, which is true of almost anything derived from price or time.

The practical distinction: int, float, and bool tell you what you can do with a value (add two floats, negate a bool); the qualifier tells you whether that value is fixed for the whole script or can move bar to bar.

Operators

  • Arithmetic: +, -, *, /, %.
  • Comparison: <, <=, !=, ==, >, >=, all returning bool.
  • Logical: and, or, not.
range = high - low
isWideBar = range > ta.sma(range, 20) * 1.5

isWideBar is a bool: true on any bar whose range beats 1.5x its own 20-bar average range.

Control structures: if and for

if has no then keyword. The block that runs is whatever's indented under it:

if isWideBar
    label.new(bar_index, high, "Wide")
else if range > 0
    label.new(bar_index, high, "Normal")

for loops over an inclusive range, for i = 0 to 9 runs 10 times, i taking every value 0 through 9:

sum = 0.0
for i = 0 to 9
    sum := sum + close[i]
avg = sum / 10

That's a manual 10-bar average. It works, but Pine Script has ta.sma(close, 10) for exactly this, and TradingView's own docs recommend reaching for the built-in first, reserve loops for the cases nothing else covers.

Plotting

plot() draws a series on the chart. It's also how a value becomes available to {{plot("Name")}} in an alert message, covered in the strategy alerts guide:

plot(ta.sma(close, 10), "SMA 10", color.blue)

The simplest path to a live command

Once you can build a bool from an if and some comparisons, you already have everything alertcondition() needs. alertcondition(condition, title, message) takes that boolean, a name for TradingView's alert-creation dropdown, and a fixed message string, and it only works in an indicator() script; it compiles inside strategy() without error, but no alert can be created from it there.

//@version=5
indicator("Wide Bar Signal", overlay=true)

range = high - low
isWideBar = range > ta.sma(range, 20) * 1.5

alertcondition(isWideBar, title="Wide Bar", message="s=BTCUSDT b=buy q=1 t=market d=1")

Create the alert against the Wide Bar condition in TradingView's alert dialog, and every time isWideBar turns true, Autoview receives that exact message. The message here is fixed text, the same command every time, which is the tradeoff for alertcondition()'s simplicity. Once you need the command itself to change bar to bar (a computed size, a computed price), that's alert() plus str.tostring(), covered in the functions guide.

Test before you trust it

Notice the d=1 left in the example above. That's not a typo, it's Autoview's dry-run flag: with it set, Autoview logs what it would have done instead of sending the order. Let isWideBar trigger a few times, check the log for the side, size, and symbol you expected, then remove 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.

Set up your first alert