FOREX.com by StoneX logo
New York City skyline at dusk with One World Trade Center, featuring modern skyscrapers and a blue corporate overlay

Charts and chart patterns

How to use TradingView’s Pine Script

What is Pine Script?

Pine Script is TradingView’s native programming language designed to create custom trading tools such as indicators, strategies, and alerts on the platform. TradingView is one of the most popular technical analysis and trading platforms, used by over 50 million traders across the globe.

Compared to other programming languages, Pine Script is a lightweight script, designed to interact with TradingView in as few lines as possible. Many users compare it to Python, but traders experienced with any programming language should have an easy time learning Pine Script. Pine code is created with Pine editor, a native element of TradingView’s online platform.

Pros and cons of Pine Script

 

How to use Pine Script

You can start using Pine Script in a few quick steps.

  1. Log in to TradingView or open an account
  2. Go to tradingview.com/chart and open the Trading Panel
  3. Select FOREX.com from the list of brokers and log in using a live or demo account
  4. Select the Pine Editor button on the same toolbar as the Trading Panel

From there you can create custom indicators and strategies using Pine Script. Keep reading for introductions on how to create with Pine Script. 

Need a FOREX.com account? Create an account here. 

If you’re looking to use pre-defined TradingView indicators and charts, you can log in to FOREX.com’s web or mobile trading platforms and choose from over 50 TradingView indicators. They are integrated directly into FOREX.com web and mobile trading platforms.

How to write code in Pine Script

To create a Pine Script script, start by defining an indicator or strategy using the ‘indicator’ or ‘strategy’ keywords. Next, define the inputs such as the length of a moving average or the threshold for a particular indicator. Then, establish trading signals or alerts based on the indicator(s) used.  

For example, a script could be designed to generate a buy signal when a particular indicator crosses above a certain threshold or a sell signal when it crosses below a threshold. Before implementing, you can use TradingView’s backtesting feature to see how your scripted strategy would play out in past market conditions.

Below, we go through examples of both creating indicators and strategies in Pine Script.

How to create an indicator with Pine Script

Screenshot of source code showing a simple trading indicator script with license header and plot function

You will see this configuration upon opening Pine Script. The first two lines are comments, denoted by two forward slashes. The ‘author’ in the second line will be replaced with your TradingView username. These lines can be manually deleted. The fourth line, however, is not a comment but a directive signifying which version of Pine Script to use.

Line five is a declaration. You can specify whether you’re writing an indicator or a strategy, then assign it a name. The default name is ‘My Script’. Line six identifies what you would like to plot. In this example, close is a variable that contains the closing price of each bar.  

Screenshot of Pine Script code showing an indicator declaration and plot function beneath explanatory text.

Line five has a variety of modifiers. For example, if you’d like the closing price to be plotted on top of the price chart, you can write:

indicator("My script", overlay=true)

Designating the overlay as ‘false’ would open the plots of the closing price in a new window below the price chart. This option is the default, so you could exclude the declaration altogether.

If you want to overlay a 50-period SMA over the price chart, you would write:

Screenshot of Pine Script source code defining a trading indicator with a 50-period simple moving average overlay

To see your script in action, press the ‘Add to chart’ button. This will also save your script. Once added to your chart, a control panel will appear in the top left of the price chart.  

TradingView EUR/USD daily forex chart with a 50-period simple moving average overlay created using a Pine Script indicator

How to create a strategy with Pine Script

Now that we’ve covered how to create indicators with Pine Script, let’s look at strategies. For this example, we’ll stick with moving averages and create a simple strategy based on moving average crossovers. The Pine Script code outlined below establishes a strategy that utilizes moving averages to identify buy and sell signals based on crossovers between a 20-period moving average and the price level.

//@version=5

strategy("Simple Moving Average Strategy", overlay=true)

 

First, swap out the indicator declaration with a strategy declaration.

ma_length = input(title="Moving Average Length", type=input.integer, defval=20)

 

Then, define the length of the moving average.

 

ma = sma(close, ma_length)

plot(ma, color=color.blue)

 

These two lines define the plot of the moving average on the chart. The first line calculates the MA, and the second plots it on the chart.

 

buy_signal = crossover(close, ma)

sell_signal = crossunder(close, ma)

 

These lines define the buy and sell signals based on the crossover of price and the moving average.

 

if (buy_signal)

    strategy.entry("Buy", strategy.long)

if (sell_signal)

    strategy.close("Buy")

if (sell_signal)

    strategy.entry("Sell", strategy.short)

if (buy_signal)

    strategy.close("Sell")

 

These eight lines define the entry and exit conditions for your strategy. 

Screenshot of Pine Script v5 source code for a simple moving average trading strategy with buy and sell signals

Here is the complete strategy written out in Pine Script. This is a fairly short example, and strategies written with Pine code can become much more complex depending on the number of indicators, conditions, and calculations you include.

The most thorough way to learn Pine script is to start with TradingView’s user manual, updated for Pine Script v5. You can also find trade ideas, educational content, and video walkthroughs in the TradingView community hub. With the proliferation of AI technology, a growing number of traders with some coding experience are using chatbots like ChatGPT to code trading strategies in Pine Script.

How to backtest your Pine Script strategy

Backtesting your strategy with Pine Script in TradingView is incredibly simple. Open the strategy tester tab above the script window. You’ll then be shown an overview of how your strategy performed in the time period selected including your net profit, total closed trades, and the percent of profitable trades. Check out the above strategy in action and notice the Strategy Tester results at the bottom of the image.

TradingView EUR/USD daily chart showing a simple moving average strategy backtest with buy and sell signals and performance summary

Share this:

Open an account in minutes

Experience award-winning platforms with fast and secure execution.

Go to our Trading Academy

Choose one of our four market-leading educational courses.

A better trading experience

Discover how FOREX.com's platforms can give you an edge.

Economic calendar

Abstract blue background featuring embossed dollar and euro currency symbols 
It's your world. Trade it.