silverstreak
silverstreak

Reputation: 141

Plot the same line on any timeframe

The intention is to be able to recreate this chart but on any timeframe and for any symbol. I came across this question, according to which I should be able to plot a line based on one timeframe on any other timeframe. However, it doesn't seem to work. Here's the code I wrote:

//@version=5

// initialization
indicator(title="test", shorttitle="test", overlay=true)

// display 2-year MA multplier bands
dailyChart = request.security(syminfo.tickerid, "D", close)
y2Sma = ta.sma(dailyChart, 730)
y2x5Sma = y2Sma * 5
y2SmaPlot = plot(y2Sma, linewidth = 3, color = color.orange)
y2x5SmaPlot = plot(y2x5Sma, linewidth = 3, color = color.orange)
closePlot = plot(close, display = display.none)
fill(y2SmaPlot, closePlot, color = close < y2Sma ? color.new(color.red, 70) : na)
fill(y2x5SmaPlot, closePlot, color = close > y2x5Sma ? color.new(color.lime, 70) : na)

I then apply indicator to the BTC/USDT Binance pair.

For the daily chart, it works perfectly: Daily chart

I zoom out completely. Notice how the maximum value of the upper band is in the region of 180k.

Now I switch to the 4h timeframe and zoom out as much as I can: 4h chart

Notice how the maximum value of the upper band now is in the region of 260k.

Shouldn't they be the same regardless of the timeframe I'm using? What am I doing wrong?

Upvotes: 0

Views: 544

Answers (1)

vitruvius
vitruvius

Reputation: 21121

The length argument of the sma is number of bars on your chart.

You get the daily close price from the security call and then do y2Sma = ta.sma(dailyChart, 730).

This will calculate the sma using the daily close price over the last 730 bars. This will of course work on the daily chart because everything perfectly lines up.

However, on the 4h chart, 730 bars don't make two years.

You can actually calculate the sma as normal and use it as the expression to security().

//@version=5

// initialization
indicator(title="test", shorttitle="test", overlay=true)

// display 2-year MA multplier bands
y2Sma = ta.sma(close, 730)
dailyChart = request.security(syminfo.tickerid, "D", y2Sma)
y2SmaPlot = plot(dailyChart, linewidth = 3, color = color.white)

enter image description here

Upvotes: 1

Related Questions