Pine Script basics and resources

What is Pine Script?

Pine Script is TradingView's programming language for custom indicators, strategy backtests, and chart alerts. Scripts run on TradingView's servers and work with market data as a series of bars. Start with an indicator to display a calculation; use a strategy when you need simulated orders and a performance report.

See the decision guide

What can Pine Script do?

Use Pine Script to calculate moving averages, combine indicator conditions, mark chart events, or test entry and exit rules. Chart prices such as open, high, low, and close are available as series. Pine runs within TradingView, with limits on runtime, memory, script size, and requested data. It is not a general-purpose environment for installing Python packages or running an external application.

Indicator, strategy, or library?

The declaration statement selects the script type. The //@version=6 line is a separate compiler annotation that selects the language version. Strategies can draw plots too; the difference is access to simulated orders and strategy results.

TypeUse it forWhat to check
indicator()Plots, shapes, tables, and alert conditionsDo values and signals match the intended calculation?
strategy()Simulated entries and exits through the broker emulatorInspect fills, costs, position sizing, and Strategy Tester results.
library()Reusable functions and types imported by other scriptsCheck the exported API and required library version.

How scripts run across bars

On a chart, an indicator calculates once per historical bar, from the oldest available bar forward. On the open realtime bar it recalculates as data updates. Strategies execute at bar close by default; calculation settings can change that behavior. Code outside an explicit loop still runs repeatedly because Pine manages this bar-by-bar execution.

  • close[1] reads the previous bar close. It is not the previous tick.
  • Ordinary declarations initialize each time their scope executes. var initializes the first time its scope executes and preserves its value across bars.
  • := updates an existing variable. = declares and initializes one.
  • During realtime updates, rollback restores applicable state to the last committed bar close. var alone does not preserve changes between ticks.

Pine Script tutorial: a moving average with a confirmed marker

This complete v6 indicator plots a simple moving average and marks a close crossing above it. It has no orders, stop, or profit target. The crossover is calculated on every execution, then the marker waits for the chart bar to close. The example teaches inputs, series calculations, a plot, and an alert condition.

  • input.int() adds a length setting with a minimum of one bar.
  • ta.sma() calculates the average; the ta namespace contains technical-analysis functions.
  • ta.crossover() is true when close is now above the SMA and was at or below it on the previous bar.
  • plotshape() marks the event. alertcondition() exposes a selectable condition; it does not create a running alert.
Complete indicator: SMA and a confirmed close crossoverpinescript
//@version=6
indicator("First Pine Script: SMA Cross", overlay=true)

length = input.int(20, "SMA length", minval=1)
average = ta.sma(close, length)
crossedAbove = ta.crossover(close, average)
confirmedCross = crossedAbove and barstate.isconfirmed

plot(average, "SMA", color=color.blue, linewidth=2)
plotshape(confirmedCross, title="Confirmed cross", style=shape.triangleup, location=location.belowbar, color=color.green, size=size.tiny)
alertcondition(confirmedCross, "Close crossed above SMA", "The bar closed across its SMA.")

Run the example and inspect the result

Open the Pine Editor from a TradingView chart. In its script menu, create a new indicator, replace the template with the complete example, save it, and select Add to chart. Read any compiler diagnostics before checking the chart. These are repeatable checks to perform yourself, not a claim that this example has been chart-tested here.

  • Set the length to 20. Compare the line with TradingView's Simple Moving Average using the same close source and length.
  • Inspect a marked bar: close must be above its SMA, while the previous close was at or below the previous SMA.
  • Change the length. The calculation and historical markers should update; the first bars can lack an SMA while it gathers enough values.
  • To receive notifications, create a chart alert, select this indicator and its crossover condition, and choose Once Per Bar Close. Recreate the alert after changing code or inputs.

Understand repainting before relying on signals

The open bar's close and average can move, so a crossover seen before the close may disappear. barstate.isconfirmed delays this example's marker until the chart bar closes. That does not make every script non-repainting: requested higher-timeframe data, future-data leaks, drawings moved into the past, and data revisions need separate review. A closed chart bar does not necessarily mean a requested higher-timeframe bar has closed.

Choose how to build your next script

Write a small indicator by hand when you want to learn the language. Use the Pine Script AI Coding Agent to draft or revise code from rules you can check. Use the Visual Pine Script Editor when your conditions can be expressed with its available indicators and settings. In each workflow, review the output in TradingView before treating it as a working trading system.

  • For a manual script, change one input or condition at a time and compare the result with the previous version.
  • For an AI draft, specify the script type, timeframe, signal rule, and whether signals must wait for bar close.
  • For a visual build, define each comparison and how multiple conditions combine before exporting the script.
Where Pineify fits

Pine Script AI Coding Agent

Draft, explain, and revise indicators or strategies, then review syntax diagnostics. Chart behavior and trading assumptions still need separate checks.

Also useful: Visual Pine Script Editor. Combine supported indicators and conditions visually, then generate Pine Script for TradingView.

Explore Pine Script AI coding

Frequently asked questions

Educational programming example, not investment advice. A crossover marker is not a validated trading recommendation. Compilation does not establish profitability or correct live behavior.

Sources and verification