Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the active time frame as a variable in a condition?

Tags:

pine-script

I want to write an indicator for tradingview that should draw a vertical line on a specific level depending on the active time frame, e.g. on the 5-minute-chart the indicator should draw the line at a different level than on the 60-minute-chart.

I have allready tried "resolution". Here is a snippet of the code:

x = (resolution == "5") ? 10 : (resolution == "60") ? 20 : 30

plot(x)

So this should draw a line at level 10, if the chart is on the 5-minute-time-frame, at level 20 on the 60-minute-time-frame and at level 30 for all other time frames.

But it allways draws at level 30, so the code has to be incorrect. I allready researched that "resolution" is a constant of the "input"-function, so it seems that it cannot be used outside of this function.

So my question is: What is the right code? Thank you!

like image 266
a kind person Avatar asked Dec 30 '18 20:12

a kind person


1 Answers

For Pine Script v4 the variable you are looking for is called timeframe.period.

E.g. '60' - 60 minutes, 'D' - daily, 'W' - weekly, 'M' - monthly, '5D' - 5 days, '12M' - one year, '3M' - one quarter

Pine reference

like image 73
Diego Ferri Avatar answered Jan 04 '23 04:01

Diego Ferri