How to Calculate and Plot SMA in TradingView using PineScript?

How to Calculate and Plot SMA in TradingView using PineScript?

In the previous episodes, we have learnt the basics. Now it’s time to take it up a little further technically.

How to get and plot the SMA?

In the previous episodes,w e learnt how to plot the price chart in trading view. Now, we can use that as a base to plot the SMA. The same process can also be used to apply any indicator.

But first, we need to learn how to use the security() function. Here is the basic syntax of security function

security(“NIFTY”, “D”, close)

The first value in the security function is the ticker symbol which is NIFTY.

Then we set the time frame to daily.

And lastly, we told Pine script we are interested in the closing price.

This will give the last daily closing price of NIFTY. We can save the return value of the function to a variable.

nifty_price = security(“NIFTY”, “D”, close)

Now the nifty_price variable will contain the latest daily close of nifty.

Studies created in Pine script need to have at least one output, otherwise it the script will generate a compiler error.

Let’s plot our variable so that it satisfies the Pine script rule about having an output.

plot(nifty_price)

After saving and adding to chart, this is what our screen looks like.

We now have nifty’s daily closing price plotted in the data window while the main window is showing a candlestick chart of banknifty.

Now that we can access a stock price, let’s go through an example of retrieving a simple moving average

If you’re not looking to get the 20 SMA specifically for nifty while viewing BNF chart, you can skip the security definition and just use the built-in close variable.

This will grab the closing price for whichever security you have showing in your main chart window.

There is a helper function for the SMA indicator built-in to Pine script. To access it, we simply use the sma() function.

nifty_sma = sma(nifty_price, 20)

The first parameter we need to pass in is the price value. In this case, we are using the closing price for nifty that we have stored in our nifty_price variable.

The second parameter is the length of the SMA. We are looking for a 20 period SMA.

Lastly, we will assign the SMA data to a separate variable and then plot it.

The simple moving average for Nifty is now plotted to our data window.

If you want modifications to SMA by using multipliers, you can do that very easily as well. Just look at this.

plot(nifty_sma *2)

plot(nifty_sma *2.3)

And going back to the thing I mentioned earlier, we don’t have to go for whole lot of trouble to plot SMA if we want to plot SMA of the same stock you are viewing in the chart. You can simply do this with an two liner like this:

shortSMA = sma(close, 10)

plot(shortSMA)

and here we go and plot another SMA

longSMA = sma(close, 30)

plot(longSMA)

So with this, we have learnt how to calculate and plot SMA using pine script. Let’s move on to strategies from the next episode.


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

To know more, checkout:

https://youtu.be/6Q3GheBbZ74

 

Leave a Reply

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

error: Content is protected !!