Skip to main content
On this page

How to Write Pine Script in TradingView for Beginners

· 4 min read
Pineify Team
Pine Script and AI trading workflow research team

Write a Pine Script by choosing its type, declaring inputs, calculating series, and displaying the result. Start with a calculation you can check manually. The examples below use Pine v6 and focus on writing indicators; a profitable trading strategy requires additional rules and evidence.

Open a new indicator​

Open TradingView Supercharts and select Pine from the right toolbar in the current editor interface. Create a new indicator, replace the template source, save, and select Add to chart. The Pine Editor guide is the reference if your layout differs from an older bottom-tab screenshot.

A version annotation selects language behavior. Without one, Pine assumes version 1; modern source should explicitly declare the version it uses. Each script has an indicator(), strategy(), or library() declaration. See script structure.

Write a configurable moving average​

//@version=6
indicator("Configurable SMA", overlay=true)
length = input.int(14, "SMA length", minval=1)
source = input.source(close, "Source")
average = ta.sma(source, length)
plot(average, "SMA", color.blue, linewidth=2)

The integer input rejects lengths below one. The source input lets you choose the series being averaged. ta.sma() performs the calculation, and plot() displays it over the price chart because overlay is true.

For a simple verification, choose a short length and compare a historical output with the arithmetic mean of those source values. Then change the source or length and confirm the expected difference. This checks correctness without relying on a handpicked successful trade.

The Best Pine Script Generator

Understand current and historical values​

close refers to the current bar's close, which can change while the realtime bar develops. close[1] refers to the prior bar's close. A series is a sequence of values across executions, not a general-purpose array that you index or mutate arbitrarily.

Historical references can be unavailable near the beginning of a dataset, producing na. Indicators normally execute on realtime updates, whereas strategies default to bar-close calculation. TradingView's execution model explains this distinction. Decide whether your future signals require a confirmed bar before interpreting a changing value as a stable event.

Write MACD in its own pane​

This second complete example uses the built-in MACD function and plots its returned MACD, signal, and histogram series. Add it as a separate script, rather than combining two declarations in one source file.

//@version=6
indicator("MACD Learning Example", overlay=false)
fastLength = input.int(12, "Fast length", minval=1)
slowLength = input.int(26, "Slow length", minval=1)
signalLength = input.int(9, "Signal length", minval=1)
[macdLine, signalLine, histogram] = ta.macd(close, fastLength, slowLength, signalLength)
plot(macdLine, "MACD", color.blue)
plot(signalLine, "Signal", color.orange)
plot(histogram, "Histogram", color.gray, style=plot.style_histogram)
hline(0, "Zero", color.gray)

Compare it with TradingView's built-in MACD using matching source, lengths, and average settings. A manually coded SMA signal line would be a variant, not automatically the same calculation. The 12/26/9 defaults here demonstrate configuration; they are not a claim of optimal performance.

Diagnose errors by category​

Compilation errors concern syntax, names, types, or unsupported arguments. Runtime errors occur during execution, such as invalid historical access. A script can also compile and run yet calculate the wrong rule; compare outputs against a known calculation to detect that third category.

If an output is invisible, check the pane, scale, visibility controls, and whether the series is na. Do not assume a blank chart means a syntax error. Change one part at a time and keep a working version so the effect of each edit remains clear.

Add strategy or alert behavior deliberately​

An indicator does not generate a Strategy Tester report. A strategy needs explicit simulated order rules, sizing, and costs. Alert functions expose events, but you still create the running alert through TradingView's interface. Generated code from Pineify needs the same calculation and behavior checks as handwritten source.

Which Pine version should I learn?

Use the current v6 reference for new work. Older examples need version-specific interpretation and may require migration changes.

Why use input.int with minval?

It makes the intended type and valid range explicit, preventing invalid period lengths before the calculation runs.

Can I combine both examples in one file?

Not by pasting both complete scripts unchanged. A source file needs one declaration and a deliberate decision about pane placement and outputs.