Overlay TradingView indicator
- Access to 132 TradingView articles (plus all articles from other platforms too!)
- Save hours of frustration with high-quality, easy-to-follow programming tutorials
A membership for life-time access only costs $79.
(Close the pop-up after logging te)
You get your total money back if you’re not sated within the very first 7 days.
A common feature of TradingView indicators is plotting values te the same chart area spil the muziekinstrument. How do wij programmatically specify where a script displays?
Ter this article:
Programmatically placing a TradingView indicator on the price chart
Ter TradingView wij set a script&rsquo,s characteristics, like its name and how it should display on the chart, with the probe() function (Pine Script Language Tutorial, n.d.). This function is the only function that always needs to be included ter an indicator&rsquo,s source code, and its title argument (which sets the indicator&rsquo,s name) is something that always needs to be defined too (TradingView, n.d.).
Another argument of examine() is overlay . With this optional argument set to true , the script is overlaid on the chart&rsquo,s muziekinstrument. That makes the indicator demonstrate up ter the same chart area spil where the muziekinstrument&rsquo,s price caf display. When wij set overlay to false the indicator plots te a separate chart panel (Pine Script Language Tutorial, n.d.). For example:
The default value of the overlay argument is false (TradingView, n.d.), meaning that indicators are displayed te a separate chart panel whenever the overlay argument isn&rsquo,t set.
The overlay argument has a few noteworthy features:
- Each indicator, regardless of the range of its values, can be overlaid on the main gegevens series. When an indicator&rsquo,s values are different than the scale used by the muziekinstrument itself, wij can specify the indicator&rsquo,s price scale programmatically to prevent the muziekinstrument&rsquo,s scale from being twisted.
- It&rsquo,s not possible to programmatically define the subchart te which the indicator should display when overlay is set to false . Wij need to do this by hand with the &lsquo,budge up&rsquo, ( ) and &lsquo,stir down&rsquo, ( ) icons that are displayed te the top right of the indicator&rsquo,s chart panel.
- While most TradingView functions are affected by the overlay argument, not all are. barcolor() , for example, always colours the muziekinstrument&rsquo,s price caf regardless of whether the overlay argument is true or false .
Let&rsquo,s look at a programming example to see how overlay works te practice.
Plotting a TradingView volume histogram te a separate chart panel
Ter the example below wij plot a volume histogram with the highest and media volume plotted spil lines. Very first wij&rsquo,ll display this indicator te a separate chart panel while straks on wij&rsquo,ll overlay the indicator on the price chart. After discussing the code wij&rsquo,ll look at how the indicator looks on the chart and what its input options are.
Wij start with the explore() function that&rsquo,s required to be added to every TradingView indicator (TradingView, n.d.). Two of this function&rsquo,s arguments are set: title to define the indicator&rsquo,s name and overlay that, when set to false , displays the indicator ter a separate chart panel.
Then wij add two input options to the script:
Inputs are made with input() , a function that add an option to the script&rsquo,s settings but that also comebacks the option&rsquo,s current value (Pine Script Language Tutorial, n.d.). Here wij store those values ter the highestLen and averageLen variables with the assignment technicus. This way wij can refer to the input&rsquo,s current value straks on te the script&rsquo,s code by using the variable.
Both inputs are numerical rechtschapen inputs. Thesis accept entire numbers only and are made by setting the type argument of the input() function to oprecht (Pine Script Language Tutorial, n.d.).
The very first input is named &ldquo,Highest Length&rdquo, with the title argument, and this name is what&rsquo,s displayed before the input option (see photo further down below). Wij set this input to a default value ( defval ) of 25, and use this input (stored ter the highestLen variable) straks on when computing the highest volume.
The other input is named &ldquo,Promedio Length&rdquo, and starts off with a value of Ten ( defval=Ten ). This input, with its current value stored ter the averageLen variable, is used when calculating the media volume.
Then wij compute the indicator&rsquo,s values:
Here wij create two variables: highestVol and averageVol . The very first is assigned the value returned by highest() , a function that works with two arguments: a series of values to process and the number of caf to get the highest value of (TradingView, n.d.). Here those arguments are set to the brochure&rsquo,s volume ( volume ) and highestLen , the input variable that wij talent a default value of 25.
Wij place the history referencing technicus (  ) behind the highest() function with a value of 1 inbetween its square brackets. Wij do that because the volume built-in variable series also includes the current buffet&rsquo,s volume. This means that, should wij compute the 25-bar highest volume up to and including the current drankbuffet, then the current folder&rsquo,s volume can never rise above that highest volume value.
And so what wij do here with the history referencing technicus is to &lsquo,offset&rsquo, the value that&rsquo,s returned by highest() with 1 brochure to the past. This way the highestVol variable doesn&rsquo,t hold the highest volume including the current drankbuffet, but the highest volume of the 25 caf leading up to the current brochure.
The 2nd variable that wij make here ( averageVol ) is set to the value returned by sma() . That function calculates the Ordinary Moving Media (SMA) with two arguments: a series of values to process and the number of kroegen to compute the moving media on (TradingView, n.d.). Here those arguments are set to volume and averageLen , the input variable that wij talent a standard value of Ten earlier.
With the indicator&rsquo,s values calculated, wij determine the volume histogram&rsquo,s colour:
The histColour variable is assigned a conditional colour with the help of two conditional (ternary) operators ( ?: ). That technicus works on three values, with the very first being a true/false condition. Whenever that condition evaluates to true , the conditional technicus comes back its 2nd value, otherwise, its third value is returned (Pine Script Language Tutorial, n.d.).
The very first conditional technicus evaluates whether the volume of the current folder ( volume ) is greater than ( >, ) the highest volume of the last 25 caf (a value wij stored te the highestVol variable). This also shows why wij didn&rsquo,t want the highestVol variable to include the current drankbuffet&rsquo,s volume &ndash, this condition would never sate otherwise. When the condition is true , this conditional technicus comes back the orange basic TradingView colour and wij store that colour ter the histColour variable for use zometeen on.
However, the 2nd conditional technicus is processed whenever the current folder&rsquo,s volume isn&rsquo,t greater than the highest volume. That 2nd conditional technicus then checks whether the volume is above the 10-bar promedio ( volume >, averageVol ). If that&rsquo,s the case, this conditional technicus comebacks the #7B68EE hexadecimal colour of medium slate blue (and that colour is then stored te the histColour variable).
But if the condition of the 2nd conditional technicus is false too, then this latter technicus comebacks the #4169E1 hexadecimal colour of royal blue, which wij then assign to the histColour variable. So what wij&rsquo,re doing here is setting that variable to an orange, slate blue, or royal blue colour depending on the folder&rsquo,s volume. Wij then use that variable when plotting the volume histogram:
The volume histogram is made with plot() , a function that plots the gegevens of its series argument on the chart (TradingView, n.d.). Te the very first plot() statement wij set that argument to volume . And with the style argument set to histogram that volume displays spil histogram kroegen.
The color argument of the plot() function defines the plot&rsquo,s colour, and here wij set that argument to histColour &ndash, the variable that wij set to one of three colours earlier. To make thesis histogram caf more noticeable wij set their size to Five with the linewidth argument.
The two other plot() statements display the highest and media volume on the chart. For this wij set their series argument to the highestVol and averageVol variables. Since wij don&rsquo,t specify the style argument here, thesis values are plotted with a consecutive line by default (TradingView, n.d.). The colour of thesis lines is set to the crimson and green basic TradingView colours.
Plotting the volume with ter a separate chart panel
When wij add the above example indicator to a chart of the German DAX Index CFD, the volume histogram looks like:
Here see that the histogram is coloured orange for kroegen that reached a fresh volume high. Histogram caf with an above-average volume are coloured slate blue while all the other caf are just coloured blue.
The indicator has thesis input options:
When wij switch thesis inputs to a highest high of 45 and an media length of 22 caf, then the previous chart switches to:
Now let&rsquo,s look at how this indicator behaves when wij overlay it on top of the chart&rsquo,s muziekinstrument.
Plotting a TradingView indicator on the chart&rsquo,s muziekinstrument
To plot the indicator ter the same chart panel spil the chart&rsquo,s muziekinstrument, wij only need to switch the very first line of the programming example. That line with the probe() function is switched to:
Wij make two adjustments here. Very first, the overlay argument is set to true to make the indicator plot ter the same chart area spil the muziekinstrument (TradingView, n.d.). The other difference is switching the indicator&rsquo,s quebrado precision by setting the precision argument to 0. That way the indicator&rsquo,s values aren&rsquo,t showcased with Four decimals (the default precision, which wij see ter the previous photos) but with zero decimals instead (TradingView, n.d.).
Then wij save the script without making other code modifications. The indicator refreshes on the chart and then looks like:
While the price axis is indeed switched here (no values after the floating point anymore), the indicator is not overlaid on the chart&rsquo,s muziekinstrument. That&rsquo,s because switching the overlay argument, when the script is already added to the chart, requires that wij liquidate and re-add the indicator to the chart.
When wij do that, the updated indicator displays on the DAX 30 Index CFD:
This brings us to another punt: the price scale is contorted since the volume values (around 500-1,500) are much lower than the values of the DAX index (around 9,500). To have the indicator overlay decently, it needs to use the left price axis. That way the right price axis can be used for the muziekinstrument&rsquo,s values.
To set the script to the left axis, wij right-click one of the indicator plots and select &lsquo,Scale Left&rsquo,:
That switches the chart and the indicator to:
One remaining punt is that the volume values don&rsquo,t display on the price axis given that the chart&rsquo,s left price axis is disabled by default. To switch that, wij right-click on the right price axis and select &lsquo,Left Axis&rsquo,:
Now our chart has two price axes: one with the indicator&rsquo,s values and another one with the muziekinstrument&rsquo,s prices.
explore() is the only function that needs to be added to every TradingView indicator. This function sets the indicator&rsquo,s properties with several arguments, including the required title and the optional overlay . That latter argument affects where the indicator displays: when overlay is set to true , the indicator is overlaid on the chart&rsquo,s muziekinstrument. And when this argument is set to false or simply not present spil a investigate() argument te the script&rsquo,s code, then the indicator shows te a separate chart panel below the chart&rsquo,s muziekinstrument. When the script is already on the chart when wij switch or add the overlay argument, then wij need to eliminate and re-add the script ter order to see the updated code.