How to create custom indicator in TradingView using Pine Script?

How to create custom indicator in TradingView using Pine Script?

In this episode, we are going to create a multi time frame indicator. Sometimes, we want data from one TF on another timeframe.

Simple example is daily pivot point. Regardless of any timeframe we are trading like 5M, 15Min, 1 hour, we always want out pivot points to be plotted based on the daily timeframe levels. That’s how it works as well. Hope you have noticed it.

In this approach, we are going to follow a similar approach for Bollinger band indicated we are creating. For example, we will be plotting 15-minute Bollinger bands directly on a 5-minute chart.

We start by declaring a name for the script and indicating it is a study. This is the name that will be shown on the charts.

Next, we set some user inputs. We need to duplicate most of the inputs from the actual Bollinger band indicator for our custom indicator.

tf = input(title=”BBands Timeframe”, type=input.resolution, defval=”60″)

len = input(title=”Length”, type=input.integer, defval=20)

stddev = input(title=’StdDev’, type=input.integer, defval=2)

I am not going through the syntax now as we discussed about it in detail in the earlier episodes

Now, We can create the Bollinger band indicator from a built-in helper function.

[middle, upper, lower] = bb(close, len, stddev)

There are three values returned from this function. The lower, mid, and upper band. These are saved individually to variables.

But the BB values should be calculated based on a different time frame. So, we can use the security() function to point to the time frame chosen by the user. TF data is stored in the tf variable created by the earlier user input.

hbbandsMid = security(syminfo.tickerid, tf, middle, barmerge.gaps_on, barmerge.lookahead_off)

hbbandsUpper = security(syminfo.tickerid, tf, upper, barmerge.gaps_on, barmerge.lookahead_off)

hbbandsLower = security(syminfo.tickerid, tf, lower, barmerge.gaps_on, barmerge.lookahead_off)

The ticker symbol remains the same, so we’ve used syminfo.tickerid which will return whichever ticker is being displayed on the main chart. If you want to plot Nifty’s BB on BNF, you can do that by changing the ticker symbol like we did in the last episode.

And that does it, all that’s left is to plot the new indicator.

plot(hbbandsMid)

plot(hbbandsUpper)

plot(hbbandsLower)

We can now see Bollinger bands from a 15-minute chart displayed on a 5-minute chart.

The inputs allow for easy customization of Bollinger band parameters and allows this indicator to work with any time frame combination. (show 4–5 tf)


Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/

To know more, checkout:

https://youtu.be/_mq1OTVyjXM

 

Leave a Reply

Your email address will not be published. Required fields are marked *

error: Content is protected !!