Getting Started with Pine Script
Use the Pine editor to apply a custom indicator, test strategy rules, or reuse a saved library during replay.
Your first indicator
-
Open the editor
The drawer contains saved scripts, New Indicator, New Strategy, and New Library actions, a name field, the code editor, and an error console. The kind badge follows the script’s declaration.
-
Create an indicator
Choose New Indicator and replace the template with:
//@version=6indicator("SMA Cross", overlay=true)fastLen = input.int(10, "Fast length", minval=1)slowLen = input.int(30, "Slow length", minval=1)fastMa = ta.sma(close, fastLen)slowMa = ta.sma(close, slowLen)plot(fastMa, "Fast SMA", color=color.blue)plot(slowMa, "Slow SMA", color=color.orange)plotshape(ta.crossover(fastMa, slowMa), style=shape.triangleup,location=location.belowbar, color=color.green)plotshape(ta.crossunder(fastMa, slowMa), style=shape.triangledown,location=location.abovebar, color=color.red) -
Apply it
Click Apply to chart. The moving averages appear on the price pane, with markers at their crosses. The chart needs enough loaded history for both averages to calculate.
If compilation fails, read the error console and use its reported location to find the relevant code.
-
Adjust inputs and save
Open the gear control on the script’s legend row to change the fast and slow lengths. Confirm the dialog to keep the changes.
Give the script a name and click Save. Its source is stored in your account. The chart keeps updating as replay advances, including changes to the forming candle.
Your first strategy
Choose New Strategy and paste this example:
//@version=6strategy("SMA Cross Strategy", overlay=true, initial_capital=1000000, commission_type=strategy.commission.cash_per_contract, commission_value=2.5, slippage=1)
fastLen = input.int(10, "Fast length", minval=1)slowLen = input.int(30, "Slow length", minval=1)fastMa = ta.sma(close, fastLen)slowMa = ta.sma(close, slowLen)
plot(fastMa, "Fast SMA", color=color.blue)plot(slowMa, "Slow SMA", color=color.orange)
if ta.crossover(fastMa, slowMa) strategy.entry("Long", strategy.long)if ta.crossunder(fastMa, slowMa) strategy.entry("Short", strategy.short)Click Apply to chart, then open the Strategy Tester tab. It shows the simulated position, trade log, equity curve, and summary metrics. The example reverses direction at each opposite cross; it does not place a protective stop.
Market orders normally fill at the next candle’s open. Pine v6 defaults to 100% margin, so an entry needs sufficient simulated capital for its quantity and costs. If no trades appear, check the available history, entry signals, quantity, and margin settings. See Strategies for trailing stops, partial exits, FIFO, and margin liquidation.
Change the inputs to compare another run, then Save the version you want to keep.
Paste or import a large script
The editor supports UTF-8 documents up to 50 MiB, including paste, file import, save, reopen, and export. Use the sidebar’s import action for an existing file. Large documents switch to plain-text mode, and a large paste is checked before the editor enables saving and applying.
A 30 MiB comment-heavy file can compile and run if its executable body is within the workload limits. Large file support does not remove calculation limits: dense code, long loops, or excessive drawing output can still be rejected or time out.
If a paste or import is rejected, read the message and reduce the file size or workload it identifies. Account storage defaults to 100 MiB across saved scripts, so export or delete unused scripts if you reach that quota. See Limits.
Reuse a library
-
Save the library source
Choose New Library, set its saved name to
local/MathTools/1, and use://@version=6library("MathTools")export midpoint(float a, float b) => (a + b) / 2Click Save library. The saved name must match the exact
author/name/versionpath used by an import. -
Import it from an indicator
Choose New Indicator and paste:
//@version=6indicator("Library midpoint", overlay=true)import local/MathTools/1 as toolsplot(tools.midpoint(high, low), "Midpoint")Click Apply to chart. TestMax loads the matching source from your account and compiles its exports with the indicator.
TradingView libraries are not fetched automatically. For a script with existing imports, save each available library’s source under the exact imported path and version, including its dependencies. Missing or duplicate library names produce an error. A script can use up to 16 libraries within the combined 50 MiB source limit.
When a script needs more data
Supported request.security() and request.security_lower_tf() calls use data available in the current replay. Add a chart pane for a required symbol/timeframe and apply again if the error says its data is unavailable. Higher-timeframe aggregation and lower-timeframe arrays have specific dataset and session requirements; check Other symbols and timeframes.