Get Started with Pine Script Code for TradingView

Pine Script code for TradingView

TradingView is a popular platform for stock traders and investors. It offers a range of powerful tools for technical analysis and charting. One of the key features of TradingView is its ability to integrate with custom indicators and strategies. This can be created using Pine Script, a programming language specifically designed for TradingView. If you’re new to TradingView and Pine Script, this blog post will provide you with a beginner-friendly introduction to Pine Script code for TradingView, with a focus on creating and testing your own custom trading strategies.

I. Introduction

TradingView is a web-based platform that allows users to view and share market insights and technical analysis. Pine Script is a coding language that allows users to create custom indicators and strategies for TradingView.

If you’re a stock trader or investor, having coding skills can be a major advantage in the stock market. By using Pine Script, you can create custom strategies that take advantage of candlestick patterns and other technical indicators to make informed trades.

II. Getting Started with Pine Script

To get started with Pine Script, you’ll need to create a TradingView account. Once you’ve created an account, you can start creating your own Pine Script strategies.

Creating a new Pine Script strategy is easy. Simply navigate to the “Pine Editor” tab on the TradingView platform and select “New Script”. You can then begin writing your own code.

Understanding the basic syntax of Pine Script is important before you start creating your own strategies. The language uses a range of operators, functions, and conditional statements that allow you to manipulate data and make decisions based on technical indicators.

III. Key Elements of Pine Script

To create effective Pine Script strategies, you’ll need to understand a range of key elements, including variables and data types, operators and expressions, functions and built-in indicators, and conditional statements and loops.

Variables and data types allow you to store and manipulate data, while operators and expressions allow you to perform calculations and make decisions based on that data. Functions and built-in indicators provide a range of tools for technical analysis, while conditional statements and loops allow you to make decisions based on the data you’ve collected.

IV. Examples of Pine Script Code for TradingView

To give you an idea of how Pine Script can be used to create custom trading strategies, here are a few examples:

Example 1: Simple Moving Average Crossover Strategy

This strategy buys when a shorter-term moving average crosses above a longer-term moving average, and sells when the shorter-term moving average crosses below the longer-term moving average.

Pine Script Example

Here, I wrote the strategy on TradingView Pine Editor and saved it. Now I will click on Add to Chart and the strategy will be added to the chart.

Moving Average Crossover Strategy

Example 2: Bollinger Bands Breakout Strategy

This strategy buys when the price breaks above the upper Bollinger Band and sells when the price breaks below the lower Bollinger Band.

Here also, you can write the strategy on the Pine Editor and attach it to a chart.

Example 3: Relative Strength Index (RSI) Strategy

This strategy uses the RSI to identify trend reversals and potential buy and sell signals. I will not code this strategy, rather I give it to you to do as a task and post in the comments. Code a strategy that gives a buy signal at RSI(14) < 30 and gives a sell signal at RSI(14) > 70. If you are unable to code it yourself, you can also take the help of Artificial Intelligence like ChatGPT to code it for you.

V. Testing and Optimizing TradingView Pine Script Codes

Once you’ve created a Pine Script strategy, it’s important to test and optimize it to ensure that it’s effective. TradingView offers a range of tools for backtesting your strategy, allowing you to see how it would have performed in historical market conditions.

Using the Pine Editor, you can refine and optimize your strategy to make it as effective as possible. By adjusting parameters like stop loss and target, you can also fine-tune your strategy to match your trading style and risk tolerance.

VI. FAQ on Pine Script Code for TradingView

How do I run a pine Script in TradingView?

To run a Pine Script in TradingView, you will need to open the Pine Editor by clicking on the “Pine Editor” button on the top toolbar. Then, paste your Pine Script code into the editor, save it, and compile it by clicking the “Compile” button. Once the script has been compiled, you can apply it to any chart in TradingView by selecting it from the “Indicators” menu and adding it to the chart.

Is TradingView Pine Script free?

Yes, TradingView’s Pine Script is free to use. You do not need to pay any fees or subscribe to any premium plans to use Pine Script.

How do I get the source code for a TradingView Script?

To get the source code for a TradingView script, simply open the Pine Editor and select the script from the “Indicators” menu. Then, click on the “Edit Source” button to open the source code in the editor.

How do I copy a Script in TradingView?

To copy a script in TradingView, you can either use the “Duplicate” button in the Pine Editor. Or you can copy and paste the code into a new script in the editor. Once you have a duplicate script, you can modify it as needed without affecting the original script.


In conclusion, coding skills are becoming increasingly important in the share market and stock exchange industry. TradingView’s Pine Script offers a great opportunity for traders to develop their technical analysis skills. Moreover, with Pine Script, traders can easily create and test their own trading strategies using a wide range of indicators and tools.

In this article, we covered the basics of Pine Script, key elements, and examples of Pine Script code. We also discussed how to test and optimize Pine Script strategies and answered some frequently asked questions. With this knowledge, Indian stock market newcomers can start using Pine Script to improve their trading strategies. Therefore, they can make informed decisions in bull or bear markets. For further learning and practice, we recommend checking out TradingView’s online community and educational resources.

To learn more about this, you can read the following book: TradingView Pine Script Programming From Scratch: Build indicators, signal generators, backtest and automate trading logic (Teach Yourself Series).

Visited 479 times, 1 visit(s) today

Author: Indrajit Mukherjee

Indrajit is a professional blogger and trading system developer. Amibroker expert, Wordpress expert, SEO expert and stock market analyst.Trading since 2002, he has started the journey of on 2008. He follows Indian and world stock markets closely.

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x