Key Takeaways
Pine Script is TradingView's built-in scripting language for creating custom technical indicators and trading strategies directly in the browser.
Pine Script v6 is the current version. It builds on v5's “ta” namespace (for example, ta.ema() and ta.rsi()), adds a stricter type system, enables dynamic data requests by default, and includes a built-in code converter to help migrate older scripts.
You can build simple indicators like SMAs, EMAs, and color-coded candlesticks in just a few lines of code.
TradingView's Strategy Tester allows you to backtest custom strategies against historical price data to check for logical flaws.
No software installation is required. Pine Script runs entirely in the TradingView web editor.
Introduction
Effective technical analysis depends on having the right tools for the job. TradingView is one of the most widely used charting platforms for cryptocurrency, forex, and stock markets, and it comes with a large set of built-in indicators. But what sets it apart is how much you can customize it.
Pine Script is TradingView's own scripting language. It lets you create custom indicators, alerts, and strategies that run directly on your charts. This guide walks through the basics of Pine Script v6, the current version, covering how to set up your first script, plot indicators, and combine them into a reusable tool.
What Is Pine Script?
Pine Script is a lightweight scripting language developed by TradingView. You write scripts inside the Pine Editor, which is built into the TradingView interface. When you click Add to Chart, your script runs on TradingView's servers and the output appears directly on your chart. No downloads or installations are needed.
Pine Script v6 is the current version and is what you should use for any new scripts. It builds on the cleaner syntax and ta namespace introduced in v5, and adds several improvements: a stricter type system, dynamic data requests enabled by default, and better-organized function handling. The language includes a comprehensive user manual and a large community of published scripts you can browse on TradingView.
What Is New in Pine Script v6?
If you've used Pine Script v5 before, here are the most practically relevant changes in v6:
Stricter type system: In v5, numeric values (int and float) could be implicitly used as booleans in conditions. In v6 this is no longer allowed. If your condition uses a number, you must explicitly cast it using the bool() function. This makes code less ambiguous and easier to debug.
Dynamic requests by default: In v5, calls to request.*() functions (such as request.security()) had to be placed in the global scope and required fixed symbol and timeframe arguments. In v6, dynamic requests are enabled by default, meaning you can call request.*() inside loops and conditional blocks, and pass series values as arguments. This significantly expands what multi-symbol and multi-timeframe scripts can do.
Built-in v5-to-v6 converter: The Pine Editor can automatically convert a v5 script to v6. Open the "Manage script" dropdown and select "Convert code to v6". The converter handles most changes automatically, though some cases may require manual review.
Multiline strings (April 2026): v6 now supports multiline string literals using triple-quote delimiters (""" or '''). This makes it easier to write formatted labels, tooltips, and log messages across multiple lines without using the \n escape sequence.
Earlier versions of Pine Script (v3, v4, v5) are still executable on TradingView, but v5 is now deprecated and v6 is recommended for all new scripts.
Setting Up TradingView
You can start with a free TradingView account. Open a chart for any asset, such as BTC/USDT, and look for the Pine Editor icon in the toolbar on the right. Click it to open the code editor.
When you click Add to Chart, the script runs and results appear on your active chart. To remove a script, right-click anywhere on the chart and select Remove Indicators. Keeping the chart clean between examples helps you see each result clearly.
The Pine Editor
The Pine Editor comes with a default two-line script. Here is a minimal example in Pine Script v6:
The indicator() function defines the script. In v5 and v6, this replaces the older study() function used in v4 and earlier. The overlay=true parameter places the result on the main chart rather than in a separate panel below. The plot(close) line draws a line using the close price for each bar.
Plotting Candlestick Charts
A line chart shows closing prices, but candlestick charts show open, high, low, and close together, giving you more information about price action. Here is how to plot color-coded candlesticks:
This script checks each bar: if the open price is greater than or equal to the close, the candle is red (price moved down). If the close is higher than the open, the candle is green. This is the standard color convention used in most charting software.
Plotting Moving Averages
Moving averages smooth out price data over a set number of periods, making trends easier to identify. Pine Script v6 includes built-in functions for both the simple moving average (SMA) and the exponential moving average (EMA) under the ta namespace.
The simple moving average (SMA)
The SMA calculates the average of closing prices over a given number of periods. To plot a 10-period SMA:
Changing the number adjusts the look-back window. A longer period creates a smoother line that reacts more slowly to recent changes.
The exponential moving average (EMA)
The exponential moving average (EMA) gives more weight to recent prices using a multiplier calculated as 2 / (length + 1). This makes it more responsive to recent price changes compared to the SMA. To plot both side by side:
You will notice a slight difference: the EMA tracks price movements more closely during fast-moving markets, while the SMA lags slightly behind.
Built-in scripts
TradingView includes pre-built versions of many indicators. In the Pine Editor, click New and select an indicator like Moving Average Exponential to view its source code. These scripts often use typed input functions such as input.int() or input.float(), which create interactive settings panels on the chart. This lets you change parameters such as length without editing the code.
Plotting the Relative Strength Index (RSI) Indicator
The RSI indicator measures the speed and size of recent price changes. It produces a value between 0 and 100. Readings above 70 are often associated with potentially overbought conditions, while readings below 30 may suggest potentially oversold conditions. These are general reference levels, not guarantees of price direction.
In Pine Script v6, the RSI function is ta.rsi(close, length). To add a ready-made RSI strategy, click New > RSI Strategy in the Pine Editor. This adds directional markers to the chart based on RSI thresholds and switches the view to the Strategy Tester tab.
RSI is typically measured over 14 periods, but you can adjust this value to suit your own approach. Longer periods produce a smoother indicator with fewer signals; shorter periods are more sensitive.
Backtesting Your Strategy
Pine Script includes a strategy() mode that lets you backtest rules against historical price data. Here is an example framework using input variables so you can adjust the levels on the chart without editing the code:
After adding this to the chart, TradingView opens the Strategy Tester tab. This shows a log of entries, exits, and summary statistics based on historical price data. Past performance is not a reliable indicator of future results. Backtesting is useful for checking whether the logic of a strategy makes sense, but it does not predict future outcomes.
Note: In v6, the default long and short margin percentage for strategies is 100%, and the strategy engine trims the oldest orders instead of raising an error when the 9,000-trade limit is exceeded. These changes may affect how your backtest results compare to the same script run in v5.
Combining Indicators
One of the most practical applications of Pine Script is combining multiple indicators into a single custom view. The following script colors candlesticks based on EMA and RSI conditions together:
This colors a candle green when the close price is above the EMA and the RSI is above 50, and red otherwise. The input.int() functions create settings panels on the chart so you can adjust both lengths interactively. This type of visualization makes it easier to spot when two conditions align on the same bar.
This should not be treated as financial advice, and no indicator combination removes uncertainty from trading. Use custom scripts as part of a broader research and analysis process.
FAQ
What version of Pine Script should I use?
Pine Script v6 is the current version and the one you should use for any new scripts. Earlier versions such as v5 are still supported but deprecated. If you have an existing v5 script, the Pine Editor includes a built-in converter: open the "Manage script" dropdown and select "Convert code to v6".
What happened to the study() function?
The study() function was renamed to indicator() in Pine Script v5, and this continues in v6. It serves the same purpose: defining the script name and properties such as whether it overlays on the main chart or appears in a separate panel. If you see old scripts using study(), they are written in v4 or earlier.
Can I use Pine Script for free?
Yes. The Pine Editor and basic Pine Script functionality are available on all TradingView plans, including the free tier. Some features, such as running multiple scripts simultaneously, require a paid plan. Writing and testing custom scripts does not require a subscription.
What is the ta namespace in Pine Script?
The ta namespace was introduced in Pine Script v5 and continues in v6. Built-in technical analysis functions that used to be called directly, such as ema(), sma(), and rsi(), are now accessed as ta.ema(), ta.sma(), and ta.rsi(). This makes the code more explicit and easier to read at a glance.
What changed in Pine Script v6 for strategies?
A few defaults changed in v6 strategies. The default margin percentage for both long and short positions is now 100%. The strategy engine also trims the oldest orders when the 9,000-trade limit is exceeded, rather than throwing an error. If you are migrating a v5 strategy to v6, review your backtest results as these changes may affect performance statistics.
Is backtesting reliable for predicting future performance?
No. Backtesting shows how a strategy would have performed on historical data, not how it will perform going forward. Markets change, and conditions that existed in the past may not repeat. Backtesting is useful for identifying logical flaws in a strategy, but it should not be used as the sole basis for any trading decision.
Closing Thoughts
Pine Script v6 makes it straightforward to build and test custom indicators on TradingView without any external software or installations. Starting with simple scripts and gradually adding complexity is a practical approach for learning the language. The Pine Editor's built-in library of example scripts and the built-in v5-to-v6 converter are useful starting points for anyone upgrading from an earlier version.
Indicators are tools for analysis, not signals that guarantee specific outcomes. No combination of indicators removes uncertainty from trading. Use Pine Script as part of a broader research process, and always consider risk management as a core part of any strategy.
Further Reading
Disclaimer: This content is presented to you on an "as is" basis for general information and educational purposes only, without representation or warranty of any kind. It should not be construed as financial, legal, or other professional advice, nor is it intended to recommend the purchase of any specific product or service. You should seek your own advice from appropriate professional advisors. Where the content is contributed by a third-party contributor, please note that those views expressed belong to the third-party contributor, and do not necessarily reflect those of Binance Academy. Digital asset prices can be volatile. The value of your investment may go down or up and you may not get back the amount invested. You are solely responsible for your investment decisions and Binance Academy is not liable for any losses you may incur. For more information, see our Terms of Use, Risk Warning and Binance Academy Terms.