Introduction
Without the right trading tools, you will not be able to perform effective technical analysis. A strong trading strategy will help you avoid common mistakes, improve risk management, and increase your ability to spot opportunities and exploit them to your advantage.
For many, TradingView is a convenient charting platform. Offering a hub of technical analysis tools, the powerful HTML5 web application is used by millions to track movements in the Forex, cryptocurrency and traditional stock markets.
TradingView has many powerful features: it allows us to track assets on multiple trading platforms and share trading ideas on our social network. In this article, we will focus on setting it up. We'll be using Pine Script, TradingView's proprietary programming language, which gives us detailed control over our chart layouts.
Let's get started!
What is Pine Script?
Pine Script is a scripting language that can be used to modify TradingView charts. The platform already provides you with many features for this, but Pine Script allows you to take it one step further. If you want to change the colors of your candles or test a new strategy on history, the Pine Editor will let you customize the real-time charts as you see fit.
The code itself has excellent documentation, so be sure to check out the user manual for more information. Our goal in this guide is to discuss some basics and introduce indicators that can be useful for cryptocurrency trading.
Settings
Getting started with Pine Script is incredibly easy. Any code we write runs on the TradingView servers, so we can access the editor and develop our scripts from the browser without any additional downloads or setup.
In this guide, we are going to chart the Bitcoin/Binance USD (BTCBUSD) currency pair. If you don't have one yet, create a free account (there is also a professional subscription, but it is not required for this guide).
Follow this link and you will see a graph similar to the following:
Yours will probably be updated.
Here we want a fully functional graph - click on the button to access it. This gives us a much more detailed overview, drawing tools, and options for constructing trendlines, among other things.
Full-featured schedule. You can adjust the timeframe by clicking on the highlighted tabs.
We will not discuss how to use the various tools available, but if you are serious about technical analysis, we recommend that you familiarize yourself with them. In the lower left corner (highlighted in the image) you will see several different tabs - click on Pine Editor.
Pine Editor
Magic happens in this editor. We'll tell it what we want to do and then click Add to Schedule to see our instructions above. Note that confusion can arise if we include several instructions at once, so we will remove them between examples (right-click on chart > "Remove Indicators").
As you can see, we already have several lines of code. Click Add to Chart to see what happens.
The second graph is added below the main one. The new graph represents the same data. Hover over My Script and click the cross to delete it. Now let's dive into the code.
study("My Script")
This first line simply sets up our instruction. This only requires naming the indicator (in this case "My Script"), but there are also some additional options we can add. One of them is an overlay, which tells TradingView to place the indicator on an existing chart (rather than a new segment). As you can see from the first example, the default is false. Although we won't see it in action right now, overlay = true adds the indicator to the existing chart.
plot(close)
This line is an instruction to construct the closing price of Bitcoin. plot just gives us a line chart, but we can also display candles and bars, as we'll see shortly.
Now let's try the following:
//@version=4 study("My Script", overlay=true) plot(open, color=color.purple)
After you add this, you should see a second graph (which looks like the original is shifted to the right). All we've done is plot the opening price chart instead, and since the current day's open is the previous day's close, it's obvious that they have an identical shape.
Fine! Let's get rid of the current instructions (remember, we do this by right-clicking and selecting Remove Indicators). Hover over Bitcoin / BUSD and click the Hide button to clear the current chart as well.
Many traders prefer candlestick charts because they give us more information than a simple chart like the one we just created. Let's add them now.
//@version=4 study("My Script", overlay=true) plotcandle(open, high, low, close)
It's a good start, but the lack of color makes the graph confusing. Ideally, we should have red candles when the opening price is greater than the closing price for a given timeframe, and green candles when the closing price is greater than the opening price. We'll add a line above the plotcandle() function:
//@version=4 study("My Script", overlay=true) colors = open >= close ? color.red : color.green plotcandle(open, high, low, close)
The code looks at each candle and checks if the open price is greater than/equal to the close price. If so, it means prices have fallen for the period, so the candle will be colored red. Otherwise, it will turn green. Modify the plotcandle() function to pass this color scheme to:
//@version=4 study("My Script", overlay=true) colors = open >= close ? color.red : color.green plotcandle(open, high, low, close, color=colors)
Remove the current indicators if you haven't already and add this one to the chart. Now we should have something similar to a normal candlestick chart.
Perfectly!
Charting moving averages (MA)
We've learned some basics. Let's move on to our first custom indicator – the Exponential Moving Average or EMA. This is a valuable tool as it allows us to filter out any market noise and smooth out price movement.
The EMA differs slightly from the simple moving average (SMA) in that it gives more weight to recent data. The indicator usually reacts to sudden movements and is often used for short-term play (for example, in day trading).
Simple Moving Average (SMA)
We could just as well construct an SMA to then compare the two indicators. Add this line to your script:
plot(sma(close, 10))
This represents the average of the previous ten days. Change the number in parentheses to see how the curve changes with different lengths.
The SMA is based on the previous ten days.
Exponential Moving Average (EMA)
The EMA will be a bit more difficult to understand, but don't worry. Let's break down the formula first:
EMA = (Close - previous day's EMA) * multiplier - previous day's EMA
So what does this tell us? Well, for each day we calculate a new moving average based on the previous one. The multiplier is what "weights" the last period and is calculated according to the following formula:
Multiplier = 2 / (EMA Length + 1)
As with simple moving averages, we need to specify what the length of the EMA will be. Syntactically, the EMA construction function is similar to the SMA function. Plot it next to 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 types of moving average.
➠ Want to start trading cryptocurrency? Buy Bitcoin on Binance!
Built-in scripts
Until now, we've been writing our code by hand so you can experience it. But let's look at something that can save us time, especially if we're writing more complex scripts and we don't want to build them from scratch.
On the right-hand side of the editor, click New. You will see a menu with various technical indicators. Click the Moving Average Exponential button to see the source code of the EMA indicator.
Add it to the schedule.
This one is different from ours - you can notice the input() functions. This is good in terms of convenience as you can click on this field…
... and it's easy to change some values in the pop-up window by clicking the settings icon.
We'll add a couple of input() functions to our next script to demonstrate this.
Construction of the Relative Strength Index (RSI) indicator
The Relative Strength Index (RSI) is another important indicator in technical analysis. It is known as a momentum indicator, which means it measures the speed at which assets are bought and sold. Presented on a scale of 0 to 100, the RSI attempts to inform investors whether assets are overbought or oversold. Generally, an asset can be considered oversold if its value is less than or equal to 30, and vice versa, it can be overbought with a value greater than or equal to 70.
If you go to New > RSI Strategy, you can see for yourself. RSI is usually measured over a 14-digit period (ie 14 hours or 14 days), but you can adjust this parameter to suit your own strategy.
Add it to the schedule. You should now see some arrows (defined by the strategy.entry() function in the code). RsiLE indicates the potential long of an asset as it may be oversold. RsiSE highlights possible points at which you can short an asset when it is overbought. Please note that as with all indicators, you should not necessarily rely on them as reliable evidence that prices will go down/up.
Backtesting
We have a way to test our custom indicators. While past performance is no guarantee of future results, backtesting our scripts can give us an idea of how effective they are at collecting signals.
Below we will give an example of a simple script. We are going to create a simple strategy that opens a long position when the price of BTC falls below $11,000 and closes the position when the price exceeds $11,300. We can then see how profitable this strategy has been historically.
//@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 we have defined entry and exit as variable inputs, which means we can change them on the graph later. We also set the price variable, which takes on the closing value for each period. Then we have some logic in the form of if statements. If the part in parentheses is "true", then the block with the indentation below it will be launched. Otherwise, it will be skipped.
So, if the price is less than or equal to the desired entry, the first expression evaluates to "true" and we open a long position. As soon as the price becomes equal or exceeds the desired exit, the second block will be triggered, which closes all long positions.
We'll mark the chart with arrows that show where we entered/exited, so we've specified how to mark those points with the comment parameter (in this example, "BuyTheDip" and "SellTheNews"). Copy the code and add it to the graph.
Now you can see the indicators on the chart. You may need to zoom out.
TradingView automatically applies your rules to older data. You will also notice that it switches from the Pine Editor to the Strategy Tester tab. It allows you to see an overview of your potential profit, a list of deals and the performance of each individual deal.
Positions we opened and closed.
We tie everything together
It's time to write your own script using some of the concepts we already know. We are going to combine the EMA and RSI and use their values to paint candles to get information that we can easily visualize.
This information should not be considered as financial advice. There is no objectively correct way to use this indicator. Like the rest, it should be used with other tools to develop your own strategy.
Now let's work on our new script. Remove all your indicators from the chart and hide the Bitcoin/BUSD chart so we have a clean canvas to work with.
Let's start with the definition of our research. Feel free to call it whatever you want, just remember to set overlay = true.
study(title="Binance Academy Script", overlay=true)
Remember our EMA formula. We need to provide a multiplier with the length of the EMA. Let's do this via input, which requires an integer (ie no decimals). We will also set a minimum value (minval) and a 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 determine the EMA value for each candle on our chart:
study(title="Binance Academy Script", overlay=true) emaLength = input(title="EMA Length", type=input.integer, defval=25, minval=0) emaVal = ema(close, emaLength)
Perfectly. We will provide the RSI length in a similar 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 it:
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, we will collect the logic that colors the candles depending on the EMA and RSI values. Consider a situation where (a) the closing price of the candle is above the EMA and (b) when the RSI is above 50.
Why? Well, you may decide that these indicators can be used together to tell you when to go long or short on Bitcoin. For example, you might think that meeting both of these conditions means that now is a good time to open a long position. Or, conversely, you can use it to tell you when not to open a short position, even if other indicators say otherwise.
So, our next line will 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
Simply put, if the EMA is above the closing price and the RSI is above 50, we color the candle green. Otherwise, it will turn red.
Next, we will build the EMA:
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, let's draw the candles, making sure to turn on the color parameter
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 the script is ready! Add it to the graph to see it in action.
BTC/BUSD chart with EMA/RSI indicator.
Final thoughts
In this article, we've covered some basic examples of what you can do with TradingView's Pine Editor. At this point, you should be sure to do simple scripts for price charts to get additional information from your own indicators.
We've covered just a couple of indicators here, but you can easily implement more complex ones, either by choosing the built-in scripts from New or by writing them yourself.
Lacking inspiration? The following articles may give you some ideas for your project:
A quick guide to the Parabolic SAR indicator
A Guide to Mastering the Fibonacci Retracement
Explanation of lag and lead indicators
Explanation of the MACD indicator