Pine Script TradingView User Guide
Pine Script TradingView User Guide
forex.com/en-ca/learn-trading/pine-script-user-guide-tradingview
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.
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.
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.
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:
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
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)
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.
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.
6/6