Skip to content

Pine Script

Pine Script: A Practical Guide and Function Reference

Pine Script is TradingView's programming language for building custom indicators, strategies and libraries that run on TradingView's servers and calculate bar by bar on a chart. The current version is Pine Script v6, released in November 2024. A script can plot values, simulate orders in a backtest and create alert events. Pine Script cannot place broker orders itself: an alert, such as a webhook alert, has to carry the signal to another tool.

Use this guide to learn Pine Script basics, choose between an indicator and a strategy, understand when scripts calculate, and find the function reference you need. If your goal is to send an existing script’s signals to MT5, start with Add alerts to your script. If you cannot edit the source, use Connect your strategy to check its available alert routes.

Illustrative cover for the Pine Script guide: abstract code lines, chart bars evaluated one by one and an alert signal, with the official PineConnector logo.
Pine Script calculates on each bar and can create alert events. Delivery and execution happen outside the script.

Choose your Pine Script task

Start with the part of this guide, or the separate page, that matches what you are trying to do.

Pine Script at a glance

Key facts as of 25 September 2026:

  • What it is: TradingView's own programming language for trading tools that run on TradingView's servers.[1]
  • Current version: Pine Script v6, introduced in November 2024. New Pine updates apply only to v6.[2] A script selects it with //@version=6.[3]
  • Script types: Every script has one declaration statement: indicator(), strategy() or library().[3]
  • How it runs: Indicators run once per historical bar and again on each realtime update. Strategies run once per bar by default; execution settings can add historical and realtime executions.[4]
  • Alert methods: alert(), alertcondition() in indicators, and order fill events in strategies.[5]
  • Placing trades: Not possible from a script. External tools act on alert signals sent by webhook.[6]
  • Official documentation: TradingView's Pine Script v6 User Manual and Reference Manual.[7]

Pine Script function reference: every page in this library

Each page answers one question about a built-in function or behaviour. Each page states its own review scope and date. Pages marked "existing" were published earlier and may target an older version.

Conditions and values

Timing, confirmation and repainting

Strategies

Alerts

Versions and display

Illustrative diagram of the Pine Script library: calculate, time, simulate, signal, then delivery and execution, with the functions covered at each stage.
Illustrative map of how this library's pages relate. Pine Script calculates and signals; delivery and broker execution happen outside the script.

Which version of Pine Script should you use?

TradingView introduced Pine Script v6 in November 2024. Its release notes say future Pine updates apply only to v6 and recommend converting v5 scripts to access new features.[2] The annotation //@version=6 tells the compiler which version a script uses. When the annotation is missing, the compiler assumes version 1.[3]

To upgrade, open the v5 script in the Pine Editor and choose "Convert code to v6" from the "Manage script" menu. The converter needs v5 code that compiles, and in rare cases the result has compilation errors to fix by hand.[8] The migration guide lists the changes that affect v5 scripts, including these:[8]

  • "int" and "float" values are no longer cast to "bool" implicitly.
  • "bool" values can no longer be na, so na(), nz() and fixnan() no longer accept them.
  • The and and or operators evaluate lazily, so the second expression may not run.
  • Dividing two "const int" values can return a fractional value.
  • The when parameter of strategy order functions, deprecated in v5, is removed. Use an if block instead.

The removed when parameter matters for older tutorials. A call such as strategy.entry("Long", strategy.long, when = buySignal) is v5 syntax that v6 no longer accepts.[8] TradingView's primer notes that six versions of Pine Script have been released, and advises using documentation that matches your version.[9]

On the PineConnector side, the manual examples in Pine Script for PineConnector retain v5.[10] The Pine Script converter accepts supported v5 and v6 source.[11] Upgrading the language and adding PineConnector alerts are separate tasks. The v5 vs v6 comparison covers the full list.

Indicator, strategy or library: which script type do you need?

Every Pine script contains one declaration statement, and that call identifies the script type.[3]

  • Indicator, indicator(): calculates and plots values. TradingView notes that indicators cannot be used in backtesting, and that they often use fewer resources than strategies.[9] Indicators can create alerts with alert() or alertcondition().[5]
  • Strategy, strategy(): adds strategy.* order commands. A broker emulator simulates trades on historical and realtime bars.[12] Results appear in the strategy report, formerly the Strategy Tester.[2] By default a strategy calculates once per closed bar; an order created then can fill no earlier than the next bar's open. No commission applies unless configured.[12]
  • Library, library(): holds reusable functions for other scripts. A library must be published, privately or publicly, before another script can use it.[13]

A strategy's position is TradingView's simulation, not a broker account. PineConnector's converter documentation states that it does not resynchronise simulated and broker positions.[11] For the move from indicator to strategy, see converting a Pine Script indicator into an automated strategy. For order commands, see the strategy.entry() and strategy.exit() reference.

What does a minimal Pine Script v6 script look like?

Illustrative sketch; not compiled or tested. The indicator below plots two EMAs and calls alert() when the fast EMA crosses above the slow one. The lengths and message values are placeholders, not recommended settings. Use a connected demo setup, choose your own broker symbol and size, and check every account attached to the intended License ID before enabling alerts. This is not an instruction to place a live trade.[14][18]

//@version=6
indicator("EMA cross alert sketch", overlay = true)
fastLen = input.int(9, "Fast EMA length")
slowLen = input.int(21, "Slow EMA length")
fast = ta.ema(close, fastLen)
slow = ta.ema(close, slowLen)
plot(fast, "Fast EMA", color.blue)
plot(slow, "Slow EMA", color.orange)
bullCross = ta.crossover(fast, slow)
msg = "LicenseID,buy," + syminfo.ticker + ",vol_lots=0.01,sl_pips=20"
if bullCross
    alert(msg, alert.freq_once_per_bar_close)

The sketch uses these Pine functions and values, each checked against the v6 Reference Manual:[7]

  • ta.ema(source, length) returns an exponential moving average.
  • ta.crossover(source1, source2) is true when the first series is above the second on the current bar, after being at or below it on the previous bar.
  • alert(message, freq) takes a "series string" message, so the text can include syminfo.ticker, the symbol name without its exchange prefix.
  • alert.freq_once_per_bar_close allows the alert only on the realtime bar's last script update, when the bar closes.

The message follows the PineConnector Syntax order, LicenseID,command,symbol,parameters, with explicit units: vol_lots for volume and sl_pips for the stop distance.[14] Replace LicenseID with the ID shown in Portal → Connections. The symbol must match the broker's symbol exactly, including any suffix.[14] syminfo.ticker is only correct where the chart name and the broker name match.

Here the message lives in the script, not in the alert dialog. alert() does not fill the dialog's Message field.[15] Create a TradingView alert on the script and choose "Any alert() function call" as the condition.[15] Then tick Webhook URL and enter https://webhook.pineconnector.com, as the converter documentation shows.[11] TradingView allows webhook alerts only with two-factor authentication enabled.[16] The TradingView webhook setup guide covers the dialog step by step.

How does Pine Script run on a chart?

A Pine script runs as if inside a loop over the chart's bars. Indicators execute once per historical bar, from the first available bar to the most recent. Strategies also do so by default, but their execution settings can add runs within a historical bar.[4] On the open realtime bar, an indicator executes again on each new update. Before recalculation, rollback resets ordinary variables and outputs to their confirmed state. Variables declared with varip can retain values across updates.[4]

TradingView's July 2026 changes added calc_on_every_history_tick and the Bar detalization setting for historical intrabar detail. The compatible use_bar_magnifier identifier remains. These settings affect historical calculations and fills; they do not turn historical bars into live alerts.[2][5]

A realtime bar's final values are unknown until the bar closes. barstate.isconfirmed is true on the closing update.[4][7] After a reload, the realtime bars from the previous run become historical bars.[4] Two consequences matter for automation:

  • Strategies differ by default. They execute once per bar, including realtime bars, rather than on every update.[4]
  • Repainting. TradingView says a script repaints when it behaves differently on historical and realtime bars.[6] A condition that is true mid-bar can be false at the close. Waiting for confirmation avoids that case, but TradingView notes that non-repainting signals arrive later.[17]
Illustrative diagram of the Pine Script execution model: default historical execution, repeated indicator runs on the realtime bar, and a confirmed update at bar close.
Illustrative summary of TradingView's execution model. Indicators rerun on realtime updates; strategies run once per bar by default, with configurable extra historical or realtime executions.

Three pages cover the common timing traps: confirming signals with barstate.isconfirmed, request.security() and lookahead, and why pivots are confirmed late.

How do Pine scripts create alerts?

Pine code creates alert events; it never creates a running alert. A user creates the alert from TradingView's "Create Alert" dialog, and alerts trigger only on realtime bars.[5] TradingView offers three mechanisms:

Scroll horizontally to read every column.

Mechanism Script types Message
alert() Indicators and strategies A "series string" built at runtime. Frequency is set in code: once per bar (default), once per bar close, or all.[5]
alertcondition() Indicators only; no effect in a strategy A "const string" fixed when the script compiles. It can hold placeholders such as {{close}}.[5][15]
Order fill events Strategies only Set per order with alert_message, then placed in the alert's Message field as {{strategy.order.alert_message}}.[15]

Each alertcondition() call defines one option in the dialog's Condition menu. By contrast, all alert() calls in a script count as one alert.[15] The alertcondition() vs alert() comparison explains which to choose.

Creating an alert saves a snapshot of the script, its inputs and the chart context on TradingView's servers. Later edits do not reach that alert, so an edited script needs a new alert.[15] See why an alert still uses old Pine Script settings.

A webhook alert sends an HTTP POST request with the alert message in the body.[16] PineConnector reads a message in its syntax, and the receiving EA requests the order in MetaTrader.[14] Keep the stages separate when checking a signal:

  1. The condition is true on the chart, and the alert triggers.
  2. TradingView delivers the webhook.
  3. PineConnector processes the signal, and the EA sends an order request.
  4. The broker accepts or rejects the request. An accepted request can then fill and open a position.

PineConnector's Test your setup guide follows one demo order from TradingView to the broker.[18]

Where can you learn Pine Script?

TradingView's documentation is the primary source, and it comes in two parts:[9]

  • The Pine Script v6 User Manual explains concepts, starting with a primer (first steps, first indicator, next steps). It covers the execution model, alerts, strategies and repainting. The release notes and migration guides record changes between versions.[2][8]
  • The Pine Script v6 Reference Manual documents each language construct: functions, variables and their arguments. In the Pine Editor, press Ctrl or Cmd and select a keyword to open its entry.[9]

The User Manual's pages also link to community help: a Pine Q&A chat, Stack Overflow, Telegram and Reddit.[1]

For PineConnector workflows, Pine Script for PineConnector is a course of 22 lessons in 5 modules. The modules are Pine Script foundations, Build signals, Connect to PineConnector, Work with strategies, and Check and maintain your workflow.[10] Add alerts to your script shows complete examples for adding messages by hand.[19]

How to use this Pine Script library

  1. Write the rule before the code. Define each condition so it gives one true-or-false answer at a known time. The measurable-conditions worksheet shows the method.
  2. Open the page for each function you use. Function pages give the v6 signature, examples and the pitfalls that affect alerts.
  3. Check timing before alerts. Read the barstate.isconfirmed and request.security() pages before relying on a mid-bar or higher-timeframe value.
  4. Choose one alert route per signal. PineConnector's course advises avoiding overlapping alert routes for the same strategy event.[10]
  5. Verify on demo. Compare the chart, the alert log, PineConnector's result and the demo account for each signal. From a TradingView backtest to a verified demo trade lists the checks.

What Pine Script cannot do, and common mistakes

  • Treating compilation as verification. PineConnector's converter documentation notes that compilation does not verify delivery or the broker outcome.[11]
  • Mixing versions. Code copied from a v5 example into a v6 script can fail, for example through when arguments or na booleans.[8]
  • Using alertcondition() in a strategy. The call has no effect unless the script is an indicator.[15]
  • Keeping stale alerts. Editing code or inputs does not update an existing alert. Remove the old alert before enabling its replacement to avoid duplicate requests.[15][11]
  • Acting on mid-bar values. On the open bar, a condition can be true on one update and false at the close.[4]
  • Reading simulated fills as broker fills. The broker emulator fills orders from chart data and its own assumptions about price movement inside a bar.[12]
  • Ignoring resource limits. Scripts use TradingView's cloud resources, so TradingView imposes limits on them.[1]

A precise rule is not evidence of a useful one. Pine Script executes the rule you write, and a strategy report is a simulation. The backtesting library explains how to test a rule before relying on it.

Frequently asked questions

What is Pine Script used for?

Pine Script is used to write custom TradingView indicators, strategies and libraries. Indicators calculate and plot values, strategies simulate orders for backtesting and forward testing, and libraries share reusable functions. Scripts can also create alert events, which a TradingView alert can send to another tool by webhook. Pine Script cannot place broker orders by itself.

What is the latest version of Pine Script?

Pine Script v6 is the current version as of 25 September 2026. TradingView introduced it in November 2024 and said future Pine updates would apply only to v6. A script selects its version with the annotation //@version=6. The Pine Editor can convert a v5 script that compiles, though some changes may need manual fixes.

Where is the Pine Script documentation?

TradingView publishes the Pine Script v6 User Manual at tradingview.com/pine-script-docs and the v6 Reference Manual at tradingview.com/pine-script-reference/v6. The User Manual explains concepts such as the execution model, alerts and strategies. The Reference Manual lists each built-in function with its arguments. In the Pine Editor, Ctrl-click or Cmd-click a keyword to open its entry.

Is PineScript the same as Pine Script?

Yes. "PineScript", "Pine" and "Pine language" all refer to Pine Script, TradingView's own programming language. TradingView's documentation writes the name as Pine Script and often shortens it to Pine, as in Pine Editor. Scripts written in Pine Script run on TradingView's servers and calculate on the data of a TradingView chart.

Where can I find a Pine Script tutorial?

TradingView's Pine Script User Manual begins with a primer: first steps, a first indicator and next steps. For connecting scripts to MetaTrader through PineConnector, the Pine Script for PineConnector course on docs.pineconnector.com has 22 lessons in 5 modules, from a first edit to checking a workflow on a demo account.

Reviewed 25 September 2026. Facts were checked against the linked sources on that date. Nothing in this article was tested on a trading account and no code was compiled.

Sources

  1. TradingView – Welcome to Pine Script v6, accessed 25 September 2026.
  2. TradingView – Pine Script release notes, accessed 25 September 2026.
  3. TradingView – Script structure, accessed 25 September 2026.
  4. TradingView – Execution model, accessed 25 September 2026.
  5. TradingView – Alerts, accessed 25 September 2026.
  6. TradingView – Pine Script FAQ: Strategies, accessed 25 September 2026.
  7. TradingView – Pine Script v6 Reference Manual, accessed 25 September 2026.
  8. TradingView – To Pine Script version 6 (migration guide), accessed 25 September 2026.
  9. TradingView – Next steps (Pine Script primer), accessed 25 September 2026.
  10. PineConnector – Pine Script for PineConnector, accessed 25 September 2026.
  11. PineConnector – Pine Script converter, accessed 25 September 2026.
  12. TradingView – Strategies, accessed 25 September 2026.
  13. TradingView – Libraries, accessed 25 September 2026.
  14. PineConnector – PineConnector Syntax, accessed 25 September 2026.
  15. TradingView – Pine Script FAQ: Alerts, accessed 25 September 2026.
  16. TradingView Help Center – How to configure webhook alerts, accessed 25 September 2026.
  17. TradingView – Repainting, accessed 25 September 2026.
  18. PineConnector – Test your setup, accessed 25 September 2026.
  19. PineConnector – Add alerts to your script, accessed 25 September 2026.

PineConnector executes the instructions you send it. It does not select trades, manage money, or hold funds. Trading carries risk, and past performance of any strategy does not indicate future results.

2 comments

Back to PiCo Blog

Run your strategies on our managed infrastructure

Bridge routes TradingView alerts to MetaTrader. Edge hosts and runs MT5 for you.

Core Trial

Then $59/month unless you cancel.

$7for 7 days

  • 1 account connection
  • 1 hosted MT5
  • Unlimited alerts, routed by Bridge
Try Core for $7

Trusted by 66,000+ traders in 156 countries and territories · <1s typical latency

Your Edge MT5 runs in our cloud · nothing to install · built on Microsoft Azure