How to create technical analysis indicators on TradingView

Without the right trading tools, you cannot conduct effective technical analysis. A strong trading strategy will help you avoid common mistakes, improve your risk management and increase your ability to identify and capitalize on opportunities.

For many people, TradingView is a charting platform that is hard to ignore because it has outstanding advantages such as providing many types of technical analysis tools, powerful HTML5 web application used by millions of people to track movement in the foreign exchange (forex), cryptocurrency and stock markets.

TradingView has many outstanding features, allowing users to track assets on many trading platforms and post trading ideas in social networks. This article will focus on the customization possibilities of this very popular platform. The analysis below uses the Pine Script programming language of TradingView for detailed control over the layout on the chart.

What is Pine Script?

Pine Script is a scripting language that can be used to edit TradingView charts. Although the platform has equipped its users with many features to do so, Pine Script takes it one step further. Whether you want to change the color of the candles or to re-test a new strategy, Pine Editor (the Pine editor) will help you customize the real-time chart as you see fit.

The code itself is fully documented, so readers can check out the manual for more information. The main goal of this tutorial is to discuss some basics and introduce indicators useful for cryptocurrency trading.

Establish

Getting started with Pine Script is simple. The code written below can all run on TradingView’s server, so it’s easy to access the editor and script development from the browser with no additional download or configuration required.

In this tutorial, we will chart the BTC / BUSD coin pair. If you haven’t already, go ahead and create a free account (there is also a professional registration, but not required for this tutorial).

Click the link under the path and you should see a chart that looks like this:

Your chart will likely be updated more.

Here, we will use the full featured chart. To get this chart click on the “full-featured chart” button to access. Accordingly, we will have a much more detailed look, drawing tools and options contracts to draw trend lines, and more.

Full featured chart. You can adjust the timeframe by clicking on the view above the highlighted tabs.

The article won’t discuss how to use the tools available, but if you really want to study technical analysis, you should familiarize yourself with them. In the bottom left (outlined in the image) you will see a few different tabs, click on Pine Editor.

Pine Editor

This editor will bring us a lot of interesting magic. Click “Add to Chart” to see the legend that appears above. Note that things can get messy if the creator includes multiple comments at once, so you can delete them in the examples (right click on the chart> Remove Indicators) .

You can see that there are already several lines of code there. Click “Add to Chart” to see what happens.

The second chart is added below the original. The new chart represents the same data. Hover over “My Script” (my script) and click on the cross to delete it. Now, let’s learn the code.

study (“My Script”)

This first line is just the comment setup. It asks for the name you want to call the indicator (“My Script” in this case), but also has some optional parameters you can add. One of them is an overlay, which requires TradingView to place the indicator on an existing chart (instead of in a new segment). As you can see from the first example, it defaults to false. While we won’t see it in action right now, when overlay = true will add the indicator to the existing chart.

plot (close)

This line is a guide to charting Bitcoin’s closing price. Plot simply gives us a line chart, but we can also display candles and bars as will be seen shortly.

Now, try the following lines of code:

// @ version = 4

study (“My Script”, overlay = true)

plot (open, color = color.purple)

When you add the above line, you will see the second chart (looks like the original chart shifted to the right). Instead, all we have done is plot the opening price and since the opening price for the current day is the closing price of the previous day, it means they are identical in shape.

Then please remove the existing annotations (by right clicking and clicking on “Remove Indicators”). Hover over BTC / BUSD and click the “Hide” button to delete the current chart.

Many traders prefer candlestick charts because they provide more information than simple lines like the chart we just did. Let’s add them next.

// @ version = 4

study (“My Script”, overlay = true)

plotcandle (open, high, low, close)

It’s a good start, but the lack of color makes them look a bit bland. Ideally, we should have red candles when the open is greater than the close within a given timeframe and green candles if the close exceeds the open. To do that, add a line above plotcandle () function:

// @ version = 4

study (“My Script”, overlay = true)

colors = open> = close? color.red: color.green

plotcandle (open, high, low, close)

Next, let’s look at every candlestick and check if the open is greater than or equal to the close. If the price has turned down during this time period, the chart will color red for the real body. Otherwise, it will color green. Modify the plotcandle () function to convert this palette to:

// @ version = 4

study (“My Script”, overlay = true)

colors = open> = close? color.red: color.green

plotcandle (open, high, low, close, color = colors)

Remove existing indicators if you don’t have one and add them to the chart. We will now look like a regular candlestick chart.

Draw a moving average (MA)
So the article has taught you some basics of line and candlestick charts. Next, let’s move on to the first custom indicator: exponential moving average (EMA). This is a valuable tool as it allows us to filter out any market noise and handle price action smoothly.

The EMA is slightly different from the simple moving average (SMA), in that it places more emphasis on the closest data. It tends to react more quickly to sudden movements and is often used for short-term play (eg, day trading).

Simple moving average (SMA)

To compare the two, we can also plot an SMA. Add this line to your script:

plot (sma (close, 10))

This is the moving average of the previous 10 days. Adjust the numbers in parentheses to see how the curve changes taking into account the different stretching times.

SMA during the previous 10 days.

Exponential Moving Average (EMA)

The EMA is a little more confusing, but don’t worry. First, let’s break down the formula:

EMA = (Close – Previous Day’s EMA) * Multiplier – Previous Day’s EMA

What does the above formula tell us? Yes, we calculate new moving average based on previous day’s levels. The coefficient (multiplier) “focuses” on the nearest time period and is calculated by the following formula:

Multiplier = 2 / (Length of EMA + 1)

As with simple moving averages, we need to determine how long the EMA will be (length). Syntactically, the function of drawing EMA charts is similar to the SMA. Plot the EMA along with the SMA so you can compare the two:

// @ version = 4

study (“My Script”, overlay = true)

plot (sma (close, 10))

plot (ema (close, 10))

You can see a slight difference in the two MA types above.

Built-in scripts

We have just gone through the manual coding tutorial so we can understand the implementation in detail. But if you want to save time, especially when you’re writing more complex scripts and don’t want to start over, the built-in script can help.

At the top, right of the editor, click “New”. You will get a drop-down menu with all the different types of technical indicators. Click on “Moving Average Exponential” to view the source code for the EMA indicator.

Let’s go ahead and add these lines to the chart.

This is different from ours – you’ll see the input () (input value) functions, which are quite convenient to use since you can click on this box…

… and easily change some of the values ​​in the pop-up window by clicking the “Setting” wheel.

hinh91

We’ll add a few input () functions in the next script to illustrate.

Plot the Relative Strength Index (RSI) chart

The Relative Strength Index (RSI) is another essential indicator of technical analysis. It’s called the motivation indicator, which means measuring the speed at which an asset is bought and sold. Represented on a scale of 0 to 100, the RSI attempts to notify investors of whether an asset is overbought or oversold. Typically, an asset can be considered oversold if it has a score less than or equal to 30 and overbought with a score greater than or equal to 70.

Go to “New> RSI Strategy”, you can see this for yourself. RSI is usually measured over a period of 14 (i.e. 14 hours or 14 days), but you are free to adjust that setting to suit your own strategy.

Add to chart and you should see a few arrows displayed now (defined by function strategy.entry () in code). RsiLE points to potential opportunities for long assets as it could be oversold. RsiSE highlights where it is possible to short an asset when it is overbought. Note that, as with all indicators, these indicators are not necessarily proof that the price will go down / up.

Review

There is a way for us to test our custom metrics. While past performance is not a guarantee of future results, testing scenarios can tell us how effective they are for signal acquisition.

Please observe the simple example scenario below for better understanding. The strategy is created with a long position entry point when the BTC price falls below $ 11,000 and exits the position when the price exceeds $ 11,300. Then it can be seen how profitable this strategy has historically been.

// @ version = 4

strategy (“ToDaMoon”, overlay = true)

enter = input (11000)

exit = input (11300)

price = close

if (price <= enter)

strategy.entry (“BuyTheDip”, strategy.long, comment = “BuyTheDip”)

if (price> = exit)

strategy.close_all (comment = “SellTheNews”)

Here, the entry and exit points have been defined as variables – both are inputs, meaning we can change them on the chart later. Also, set the closing price variable for each period. Then we have some logic in the form of an if statement. If the portion in parentheses is true, the block followed by it is initialized. If it is wrong, it will be ignored.

So if the price is less than or equal to the desired entry, the first expression will evaluate to true and we will open a long position. When the price is equal to or exceeds the desired exit level, the second block is triggered, closing all open positions.

We’ll annotate the chart with arrows showing the entry / exit places, so we’ll label these points with the comment parameter (in this example, “BuyTheDip” (buy at bottom) and “SellTheNews” ( selling news)). Copy the code and add it to the chart.

TradingView automatically applies your rules to older data. You will also notice it switches from the Pine Editor to the Strategy Tester tab. This allows you to see an overview of your potential profits, a list of transactions and each of their individual activities.

You can now see the indicators on the chart, which can be zoomed out if needed.

Combine all that has been learned
It’s time to write our own script using some of the concepts seen so far. You can combine EMA, RSI and use their values ​​to color the candles, providing insights for easy visualization.

This should not be construed as financial advice because using these indices has never been an absolutely accurate forecast of an objective situation. Instead, you should use it with other tools to develop your own strategy.

Now, stay tuned for the new scenario below. Remove all your indicators from the chart and hide the BTC / BUSD chart for a blank image.

Let’s start by identifying the study and naming it whatever you want, just make sure overlay = true. In the code below, the name is given as Binance Academy Script.

study (title = ”Binance Academy Script”, overlay = true)

Remember our EMA formula from earlier. We need to provide the coefficient with the length of the EMA. Note the input value is an integer (therefore, no decimal places). Also set minimum value (minval) and default value (defval).

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

Using this new variable, we can calculate the EMA value for each candle in the chart:

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

emaVal = ema (close, emaLength)

Great, continue with the RSI. Please provide the length in the same way:

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

emaVal = ema (close, emaLength)

rsiLength = input (title = “RSI Length”, type = input.integer, defval = 25, minval = 0)

And now, we can calculate:

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

emaVal = ema (close, emaLength)

rsiLength = input (title = “RSI Length”, type = input.integer, defval = 25, minval = 0)

rsiVal = rsi (close, rsiLength)

At this stage, use logic to color the candles depending on the EMA and RSI values. Suppose the situation of (a) the closing price of the candle is above the EMA and (b) where the RSI is above 50.

Why? Because these indicators can be used in combination to let you know when to long or short Bitcoin. For example, meeting both of these conditions means a good time to enter a long position. Or conversely, you can use it to notify when to not short, even if other metrics say otherwise.

So our next line should look like this:

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

emaVal = ema (close, emaLength)

rsiLength = input (title = “RSI Length”, type = input.integer, defval = 25, minval = 0)

rsiVal = rsi (close, rsiLength)

colors = close> emaVal and rsiVal> 50? color.green: color.red

That is, if the EMA value exceeds the close and the RSI exceeds 50, we will color the candlestick green. If not, it will color red.

Next, draw an EMA line:

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

emaVal = ema (close, emaLength)

rsiLength = input (title = “RSI Length”, type = input.integer, defval = 25, minval = 0)

rsiVal = rsi (close, rsiLength)

colors = close> emaVal and rsiVal> 50? color.green: color.red

plot (emaVal, “EMA”)

Finally, plot the candlestick chart, making sure to include the color parameters:

study (title = ”Binance Academy Script”, overlay = true)

emaLength = input (title = “EMA Length”, type = input.integer, defval = 25, minval = 0)

emaVal = ema (close, emaLength)

rsiLength = input (title = “RSI Length”, type = input.integer, defval = 25, minval = 0)

rsiVal = rsi (close, rsiLength)

colors = close> emaVal and rsiVal> 50? color.green: color.red

plot (emaVal, “EMA”)

plotcandle (open, high, low, close, color = colors)

And so have completed the script! Add it to the chart to get the results.

BTC / BUSD chart with EMA / RSI indicator.

Conclude

This article has shown you some basic examples of what can be done with TradingView’s Pine Editor. Now, you can confidently create simple price chart captions to gain insight from your own indicator.

Note that only a few indicators are mentioned here, but you can also easily create more complex indicators by selecting existing scenarios from “New” or writing them yourself.

Leave a Comment