0% found this document useful (0 votes)
215 views

Pine Script TradingView User Guide

Pinescript

Uploaded by

Momo Mimo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
215 views

Pine Script TradingView User Guide

Pinescript

Uploaded by

Momo Mimo
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Pine Script (TradingView) user guide

forex.com/en-ca/learn-trading/pine-script-user-guide-tradingview

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

Pros Cons

TradingView isn’t just a trading and charting platform, Using Pine Script limits you to the
it’s also a social network for millions of traders. Many confines of TradingView’s
users publish Pine Script to the TradingView library, platform and data. While
which you can modify or use as pre-created scripts. TradingView has an extensive
Browsing the open-source library is also a good way database, obscure markets and
to become more familiar with Pine Script and learn select price data may not be
the language. available.

Using TradingView also gives you access to the TradingView lacks automated
platform’s plethora of data. Instead of having to execution, so your Pine Script
source your own data to create indicators or backtest cannot execute trades without
strategies, you can quickly grab data from the your approval. You can integrate
platform with a single line of Pine Script. This outside software with Pine Script,
resource lets you skip the harvesting and formatting but that will require third-party
of data so you can test more ideas faster. workarounds.

1/6
It’s worth emphasizing Pine Script is simpler than There are select databooks
most other programming languages. If you’ve tried to unavailable when using Pine
code your own trading algorithms with little prior Script. These include order book
experience, you know how challenging it is to learn a data, buy/sell volume, and tick
new script. In addition, TradingView’s Pine editor can data. If you’re looking to build
check for errors and give you suggestions on your indicators around these factors,
scripts. you’ll be hampered by Pine Script.

How to use Pine Script


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

1. Log into 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.

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

2/6
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.

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:

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.

3/6
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.

4/6
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.

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.

5/6
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.

More trading guides in this section:

6/6

You might also like