Pine Editor TradingView Tutorial: First Indicator, Strategy, and Validation
Pine Editor is TradingView's workspace for writing Pine indicators, strategies, and libraries. A script can calculate values and simulate orders on chart data, but compiling it does not establish that its rules are correct or that a live broker will execute them. This tutorial uses Pine Script v6 and separates those steps.
1. Open and Save a Script
Open a Superchart and select the Pine icon on the right toolbar. TradingView's current editor instructions describe a floating panel, right docking, a separate window, and an optional bottom layout. The editor is no longer accurately described as a tab permanently at the bottom of every chart.
Use the script-name menu to create an indicator or strategy. Save a named copy, then use Add to chart. After editing an existing chart script, apply the updated source and check the result; typing text alone is not evidence that the chart is running that revision.
The editor also provides version history and a command palette. Use its displayed shortcut labels for your operating system. The F1 command palette is useful for finding settings when a remembered menu location has changed.
2. Understand a Minimal Indicator
Paste this complete example into a new indicator:
//@version=6
indicator("My First MA", overlay=true)
length = input.int(20, "Period", minval=1)
plot(ta.sma(close, length), title="SMA", color=color.orange)
The version annotation selects Pine v6. The declaration names the indicator and places it over the price chart. The input permits a positive period, and ta.sma calculates the average of close. The ta. namespace is required here; an unqualified sma call from an old tutorial is not the same function reference in v6.
An indicator executes across the chart's dataset. A 20-period calculation means 20 chart bars, not necessarily 20 days. On a five-minute chart it uses five-minute bars; on a daily chart it uses daily bars. Early values may be unavailable until there is enough history.
3. Build a Three-Line Moving-Average Ribbon
Create another indicator and replace the template with this source:
//@version=6
indicator("3-MA Ribbon", overlay=true)
fast = input.int(9, "Fast", minval=1)
mid = input.int(21, "Mid", minval=1)
slow = input.int(55, "Slow", minval=1)
plot(ta.sma(close, fast), title="Fast", color=color.teal)
plot(ta.sma(close, mid), title="Mid", color=color.blue)
plot(ta.sma(close, slow), title="Slow", color=color.purple)
Keep top-level statements at the left margin. Indentation has meaning in Pine and should not be copied from decorative formatting around a code block.
Save, add the script to a standard candlestick chart, and compare one line with TradingView's built-in SMA using the same length and close source. Change one input and check that only the corresponding line changes. Inspect the current bar separately: its close can change while the bar is forming.
The 9/21/55 values are demonstration settings. The ribbon does not prove that a slow line is support or that this combination is preferable for a particular stock or cryptocurrency. If no line appears, check visibility, available history, inputs, errors, and the pane where the indicator was added.
4. Convert a Signal Idea Into a Strategy
The next example reverses between long and short when a 9-period SMA crosses a 55-period SMA. It includes explicit illustrative costs and plots the averages so you can inspect the signals.
//@version=6
strategy("MA Cross Strategy", overlay=true, initial_capital=10000,
default_qty_type=strategy.percent_of_equity, default_qty_value=10,
pyramiding=0, commission_type=strategy.commission.percent,
commission_value=0.05, slippage=1,
calc_on_every_tick=false, process_orders_on_close=false)
fast = ta.sma(close, 9)
slow = ta.sma(close, 55)
longCond = ta.crossover(fast, slow)
shortCond = ta.crossunder(fast, slow)
if longCond
strategy.entry("Long", strategy.long)
if shortCond
strategy.entry("Short", strategy.short)
plot(fast, title="Fast SMA", color=color.teal)
plot(slow, title="Slow SMA", color=color.purple)
The final complete sources above were checked with Pineify's syntax checker for this revision. That check does not report a profitable backtest or validate live execution.
The strategy's details matter:
- A crossover is an event, not every bar on which the fast average remains higher.
- Opposite-direction
strategy.entryorders reverse an existing position under the default entry behavior. The order quantity needed for a reversal includes closing the old position. - The 10% setting controls position allocation. It is not a 10% maximum loss or stop-risk limit.
- The example has no protective stop or profit target; it exits through an opposite entry. An open final position can remain at the sample's end.
- The commission assumption is 0.05% of transaction value per filled order. Slippage is one tick. Substitute applicable assumptions for the instrument and broker.
TradingView's strategy documentation explains that default historical market orders commonly fill at the next bar's open after the signal is calculated. The example leaves close-order processing disabled. Inspect signal bars and fill bars separately in the Strategy Report; chart arrows represent modeled fills under the selected settings.
Keep the symbol, session, interval, date range, capital, costs, and source revision with any exported report. No ETHUSD win rate or fixed reward-to-risk outcome is claimed for this example. A strategy without defined stop/target distances does not inherently have a 1:2 risk/reward rule.
5. Debug Syntax, Values, and Timing
Read the first compiler diagnostic and its line location. Later messages can be consequences of the same original error.
| Problem | What to check |
|---|---|
| Unknown identifier or function | Spelling, namespace, Pine version, and variable scope |
| Plot in local scope | Keep plotting calls in global scope and control the plotted value instead |
| Missing values | Required history, unavailable data, and calculations producing na |
| Unexpected trades | Cross versus persistent condition, reversal behavior, sizing, and fill timing |
| Realtime/history mismatch | Unconfirmed values, intrabar state, and higher-timeframe requests |
Use Pine Logs through log.info, log.warning, or log.error to inspect values. Pine does not provide Python's print function. Plots and labels can also help, but they have scope and resource constraints.
Historical scripts normally execute once per bar. Indicators also update on realtime price changes; strategies default to bar-close execution unless their calculation options change. These distinctions are described in the execution model.
The limitations documentation specifies a 500-millisecond limit for a loop on one bar, alongside a separate total execution limit. There is no general 40-millisecond-per-bar rule as previously stated. Profile slow code instead of guessing that every loop is a problem.
6. Alerts and Live Execution Are Separate
A strategy's simulated orders do not automatically become live brokerage orders. TradingView supports chart trading through integrated brokers, but a Pine strategy still needs a separately supported execution arrangement if its alerts are to drive live automation.
Create and configure the required alert, then test its event, message, frequency, and receiving system. A webhook receiver or bridge must handle authentication, duplicate messages, symbol mapping, order size, failures, and position reconciliation. Do not assume a broker accepts an arbitrary TradingView webhook directly.
Changing the chart script does not update an already running alert's saved script/settings snapshot; recreate it when necessary. See TradingView's alert documentation.
7. Share or Publish With the Correct Access Model
TradingView's publishing documentation distinguishes discoverability from access. A public or private publication can have separate source/access settings.
| Source/access setting | Meaning |
|---|---|
| Open-source | Users can view the source; reuse still follows the applicable license and publishing rules |
| Protected | Source is hidden, but users can use the script without an author-granted invitation |
| Invite-only | Source is hidden and the author grants access to specific TradingView users |
A private link is not the same as an invite-only permission. Check account eligibility and publication rules before publishing. Use the publication editor for description headings; Pine has no built-in section function that formats a publication page.
Pineify can help assemble visual components or draft source with its AI Coding Agent. Review the generated source in Pine Editor and verify the same calculation, timing, and execution checks. TradingView also has AI Copilot for indicators and strategies; AI assistance does not replace those checks or grant publication eligibility.
8. Continue With Pine-Specific Concepts
Pine supports arrays, matrices, maps, tables, and requests for other symbols or timeframes. Study one feature at a time against a small expected result. A higher-timeframe request needs attention to when its value becomes confirmed.
Pine can import published Pine libraries using an author, library name, and explicit version. It cannot install arbitrary Python packages through pip. A versioned Pine dependency is therefore different from an external Python environment.
Frequently Asked Questions
Where is Pine Editor now?
Open the Pine icon on the right toolbar. The editor can float, dock to the right, open separately, or use the optional bottom layout.
Which Pine version do these examples use?
Pine Script v6. The examples use namespaced technical-analysis functions and were syntax-checked as complete scripts for this revision.
Does the moving-average strategy include a stop-loss?
No. It reverses on the opposite crossover and uses illustrative commission and slippage. Position allocation is not a stop-risk cap.
Can Pine import libraries?
Yes, published Pine libraries can be imported by explicit version. Arbitrary Python or JavaScript packages cannot be installed into a Pine script.
Does compiling a strategy start live trading?
No. Compilation checks source, and the Strategy Report models trades. Alerts and any live execution arrangement require separate configuration and verification.

