How To Plot HTF EMA in TradingView Chart using Pine Script?

MarketSecrets
6 min readMar 25, 2023

Today’s lesson will cover how to access higher timeframe price data in your custom Pine Script indicators. As always, this lesson builds on the knowledge from previous lessons, so if anything doesn’t make sense here please go back through the other lessons where everything we’re using up until now should be explained.

So, without any further ado — let’s get started!

Step 1: Getting User Input

The first step as always is to get our script parameters (or settings) from the user.

We’re going to need to get 4 inputs for this lesson.

· The first one is the timeframe we want to reference,

· the second is the EMA period length we’d like to use,

· the third is whether or not to color the EMA based on price being above or below it

· and the fourth is whether to smooth the EMA or not.

These settings will make more sense as we go, so I won’t spend any more time explaining them.

The way we get a timeframe input is using the input.resolution data type. So in Pine Script code, it would look something like this:

res = input(title=”EMA Timeframe”, type=input.resolution, defval=”D”)

This line of code will prompt the user to choose their timeframe from a drop-down box in the settings menu. By default, this “res” variable will be set to “D” — which is short for the Daily chart timeframe.

The other 3 inputs use data types we’ve already covered in previous lessons, so I won’t go into detail about them. We need the EMA Length as an integer, the Color EMA as a boolean, and the Smooth setting as a boolean

They should look something like this:

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

col = input(title=”Color EMA”, type=input.bool, defval=true)

smooth = input(title=”Smooth”, type=input.bool, defval=false)

The next step is to retrieve the EMA value using the timeframe input we just defined (with the variable name “res”).

To do this we need to first define our EMA variable, and then later we can input the EMA variable into the security() function to reference higher timeframe price data.

This probably sounds confusing, but don’t worry — as usual with Pine Script it’s extremely simple. It looks something like this:

// Calculate EMA

ema = ema(close, len)

emaSmooth = security(syminfo.tickerid, res, ema, barmerge.gaps_on, barmerge.lookahead_off)

emaStep = security(syminfo.tickerid, res, ema, barmerge.gaps_off, barmerge.lookahead_off)

In line 1, we’re creating an EMA variable using the closing price and a period length of 50 — which is the default setting for our input variable named “len”.

If we plot this to our chart we will get the current 50 EMA for whatever timeframe we have our chart set to as any normal EMA would plot. But we don’t want this — we want this EMA variable to be calculated using the higher timeframe closing price.

So in line 2 we’re using the security() function to request higher timeframe data. The security() function takes several input parameters.

1st Input: Symbol

The first is the “symbol” or instrument ID. Since we just want to access the current instrument’s higher timeframe, so we can use syminfo

2nd Input: Resolution

The second input is the timeframe resolution we want to reference — in this case “res” which is a timeframe determined by the user in the settings menu.

3rd Input: Requested Data

The third input is what price action data we want to request from the higher timeframe — in this case, we want the “ema” variable to be calculated using higher timeframe data.

Because the “ema” is using the closing price to calculate its value, this means the security() function will return the EMA value based on the closing price from the timeframe “res” (Daily chart by default).

When using the security() function to calculate the HTF EMA value we have two options we can work with in order to deal with gaps in price.

4th Input: Gaps

Remember that if we’re drawing the Daily chart’s EMA to our 1HR chart, it is not a smooth line by default. The reason for this is that the Daily chart EMA is calculated based on the closing price for each day.

So if you have an instrument that trades 24 hours per day such as crypto or forex, then the Daily EMA requires 24 hourly bars to close before the next EMA calculation is made.

What this results in is a “stepped” appearance to the EMA if we draw it to a lower timeframe. Look at this chart.

That is a Daily chart EMA being plotted to a 15 min chart.

Notice that each time the EMA “steps” down by several points and then draws as a solid horizontal line throughout the next few candles.

If we select smooth option from setting, it will look like this:

Obviously this option looks much “better” in an aesthetic sense. We’re used to seeing moving averages smoothed like this, but the EMA value plotted to the chart on each 1HR bar is not the real Daily EMA but an approximate average.

It’s essentially averaging the steps into a smoothed line. Depending on your trading style and preferences, you may prefer one drawing style over the other — which is why I’ve included the option to choose one or the other.

I hope that makes sense. If not, feel free to leave a comment below or study the official TradingView documentation.

One is to use barmerge.gaps_on, or barmerge.gaps_off the smoothnes and step.

5th Input: Lookahead

The final input that the security() function takes is a lookahead boolean value.

If lookahead is set to true then the script will reference the current closing price on live data, and on historical data, it will essentially “cheat” by drawing that day’s daily EMA based on the day’s closing price — which ordinarily we would not know until after the day had closed.

By default “lookahead” is set to false, so you technically don’t need to set this parameter if you don’t want to, But it’s always a good idea to declare these parameters yourself as a beginner so that you know what it’s set to.

Now alternatively, if we set lookahead to be turned on, then the script will literally be looking ahead in time and time-travelling to the future to reference data from when that particular market’s higher timeframe already closed.

This means that historical bars will cheat and know the future, and currently active bars will “repaint”.

So if we set lookahead to false then on the lower timeframe the script will draw the previous day’s closing price over the top of current price action, which makes a lot more sense if you want to trade using this information.

Given the fact that yesterday’s Daily chart has already closed, it’s safe to reference that data in historical price action — whereas if our script is referencing the current day’s Daily closing price before it closes this can cause all kinds of trouble and give us a false reading on the accuracy of any strategies we try to backtest using this indicator.

So in this particular case, we want to set barmerge.lookahead to off. This will also help with “repainting”, but that’s another topic for another day.

Step 2: Drawing the HTF EMA

The final step is to plot our higher timeframe EMA to the chart.

We created two EMA variables — one that is smooth using “barmerge.gaps_on”, and one that is stepped using “barmerge.gaps_off”.

We don’t want to draw both of these to the chart at the same time, so we can use the conditional operator (which looks something like this:

smooth ? emaSmooth : emaStep

We’ve also included an option to color the EMA based on whether price is above or below it. This can also be achieved using two conditional operators on the one line.

color=col ? close > emaStep ? color.green : color.red : color.black

Therefore if we put all this together in the plot, our final line of code will look something like this:

plot(smooth ? emaSmooth : emaStep, color=col ? close > emaStep ? color.green : color.red : color.black, style=plot.style_line, linewidth=2, title=”EMA (HTF)”)

Here we are saying if the smooth option is selected, plot the “emaSmooth” value to the chart — otherwise plot the “emaStep” value to the chart.

Then we’re saying: If the color option “col” is set to true, then if the current closing price is above the “emaStep” value set the color to green, otherwise set it to red — and if the color option is set to false, then ignore price action and set the color to black.

Then we’re setting the plot style to style_line, the line width to 2, and we’re titling the drawing to “EMA (HTF)” so that our users can change these style settings in the options menu in the chart.

And that’s it! We’re done. If you save your script and add it to the chart, it should look something like this by default:

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

To know more, checkout:

https://youtu.be/wA_CjkYrB1M

--

--

MarketSecrets

A full time trader and long-term investor, who loves stock market.