Skip to main content
On this page

How to Run Pine Script in TradingView: Step-by-Step Guide

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

To run Pine Script in TradingView, open Supercharts, open the Pine Editor, create a new indicator or strategy, paste your code, save it, and click Add to chart. A free TradingView Basic account is enough to compile and run your first custom script in a browser or the desktop app.

Pine Script is TradingView's built-in scripting language for custom indicators and backtestable trading strategies. This visual walkthrough shows each step in the editor and includes a complete Pine Script v6 MACD indicator you can test immediately.

Custom Supertrend indicator running on a TradingView chart

What You Need Before You Start​

You only need a free TradingView account and an open chart in either your web browser or the TradingView desktop application. If you are on the free Basic plan, note that TradingView limits the number of saved personal scripts and active indicators per chart. If you hit a save prompt right away, see our guide to the TradingView free plan saved-script limit.

The Best Pine Script Generator

Step-by-Step: Running Your First Pine Script​

Step 1: Open the Pine Editor​

Open Supercharts in TradingView and click the Pine icon on the right toolbar to open the Pine Editor. The screenshot below shows the older interface where Pine Editor appeared as a bottom tab under the chart; for a quick text reference on the updated workspace layout and diagnostics, see our Pine Script v6 step-by-step walkthrough.

Opening the Pine Editor from a TradingView chart

Step 2: Create a New Script​

Open the script menu at the top of the Pine Editor and select Create new -> Indicator or Create new -> Strategy. Choose an indicator when you want to plot lines, histograms, or signals on the chart. Choose a strategy when you want TradingView's Strategy Tester to simulate entries, exits, and performance metrics.

Selecting Create new Indicator or Strategy in the TradingView Pine Editor

Step 3: Paste or Write Your Pine Script Code​

Replace the starter template with your own code or paste an existing script. If you are modifying a community script, change one parameter or plot at a time so you can immediately see how each edit affects the chart.

Pasting Pine Script code into the TradingView Pine Editor

Step 4: Save Your Script​

Press Ctrl+S (or Cmd+S on macOS) or click Save and give the script a clear name. TradingView stores saved personal scripts in your cloud account, so you can open and edit them from any signed-in device.

Saving a custom Pine Script in TradingView cloud storage

Step 5: Add the Script to Your Chart​

Click Add to chart to compile the code and render its output. If compilation fails, read the first error message in the editor console and inspect the line number it highlights before fixing anything else, since one syntax error often triggers cascading messages below it.

Clicking Add to chart to run Pine Script on TradingView

A Working Pine Script v6 MACD Example You Can Run​

Copy and paste this complete Pine Script v6 indicator into the Pine Editor to verify your setup. Setting overlay=false places the MACD in its own pane below the price chart.

//@version=6
indicator("My Custom MACD", shorttitle="MACD", overlay=false)

// Input parameters
fastLength = input.int(12, title="Fast Length", minval=1)
slowLength = input.int(26, title="Slow Length", minval=1)
signalLength = input.int(9, title="Signal Length", minval=1)

// Calculate MACD components
fastMA = ta.ema(close, fastLength)
slowMA = ta.ema(close, slowLength)
macdLine = fastMA - slowMA
signalLine = ta.ema(macdLine, signalLength)
histogram = macdLine - signalLine

// Plot the results
plot(macdLine, color=color.blue, title="MACD")
plot(signalLine, color=color.red, title="Signal")
plot(histogram, color=color.gray, style=plot.style_columns, title="Histogram")

How each section works:

  • //@version=6 tells TradingView to compile the script with Pine Script v6.
  • indicator(..., overlay=false) declares an indicator that renders in a separate oscillator pane.
  • input.int() creates editable length settings in the indicator's gear menu.
  • ta.ema() calculates the fast, slow, and signal exponential moving averages.
  • plot() draws the MACD line, signal line, and histogram columns on the chart.

Once this compiles and plots cleanly, you can customize colors or combine signals with other tools. See our guide on combining multiple indicators in Pine Script for a structured pattern.

Building Pine Script Without Writing Code From Scratch​

If you have a trading idea but do not want to debug syntax errors by hand, Pineify lets you assemble TradingView indicators and strategies visually or with an AI Coding Agent that validates Pine Script v6 syntax before you paste it into TradingView. Review the Pineify feature overview if you want to compare visual building with manual coding.

Pineify visual editor for building TradingView Pine Script indicators

Key Concepts to Know After Your First Script​

Indicators vs. Strategies​

Indicators calculate and display data such as moving averages, momentum, or volatility, and can expose alert conditions. Strategies declare strategy() instead of indicator(), simulate buy and sell orders on historical bars, and report metrics in the Strategy Tester. Neither type places live broker orders on its own simply by being added to a chart.

Backtesting Before Risking Live Capital​

Always test strategy rules on historical data before using them to inform live trades. Check how your assumptions about commissions, slippage, and order fills change the results in our TradingView backtesting guide.

Expanding Into Multi-Timeframe and Alert Workflows​

When you are ready to go beyond single-pane indicators, you can pull higher-timeframe data with our multi-timeframe Pine Script guide, convert entry rules into a testable Pine Script strategy, or walk through the full Pine Editor workflow from first script to live strategy.

Common Beginner Mistakes to Avoid​

MistakeWhy It Causes ProblemsHow to Avoid It
Cramming too many rules into version 1Makes compilation and logic bugs hard to isolate.Start with one indicator or entry rule, verify it on the chart, then add filters one by one.
Ignoring repainting behaviorRealtime bars and unconfirmed higher-timeframe data can shift historical signals, distorting backtests.Confirm signals on bar close and follow our guide on avoiding repainting in Pine Script.
Skipping risk controls in strategiesGood entry signals can still fail without position sizing, stop-loss, and drawdown limits.Define explicit stop-loss and sizing rules before evaluating net profit.
Mixing Pine Script versionsPasting v4 or v5 snippets into a //@version=6 script triggers syntax and namespace errors.Check the //@version= header and official v6 syntax in the TradingView Pine Script documentation.

Frequently Asked Questions​

Can I run Pine Script on a free TradingView account?

Yes. A free TradingView Basic account can open the Pine Editor, compile Pine Script v6 code, and add custom indicators or strategies to a chart. Free accounts do have limits on saved scripts, indicators per chart, and script execution time.

Why did my Pine Script compile but not appear on the chart?

Check whether overlay=true or overlay=false matches the values you are plotting. An oscillator plotted with overlay=true can be compressed at the bottom of a price chart, and scripts whose series evaluate to na will not draw visible lines.

Should I use Pine Script v5 or v6 for new scripts?

Use //@version=6 for new scripts. It is TradingView's current version and matches the active Pine Script documentation. Older v5 scripts still run, but combining v5 syntax with a v6 header can cause compilation errors.

Do I have to publish my Pine Script to run it?

No. Saving a script stores it privately in your TradingView account so you can add it to your own charts. Publishing is a separate step for sharing code with other users.