Reputation: 534
I am trying to develop a multiple time-frame RSI using pine-script on trading view but I seem to have an issue with shorter term RSI in a longer term chart view.
For example, the following code will display 5-min RSI. It will display the RSI appropriately if I have the chart set at 5-min. But when I select a larger time-frame (e.g., 1 hour, etc) the value becomes incorrect.
study("Multi Time Frame RSI", "MTF RSI", overlay=false)
src = input(title="Source", type=source, defval=close)
_5min_rsi = security(tickerid, "5", rsi(src, 14))
plot(_5min_rsi, title="5min_RSI", color=purple, linewidth=1)
I believe the problem has to do with series data being operated on. For some reason when I use security with "5" as my resolution, its data gets lost in the higher time-frame charts and it uses the close of a different series for that time. At least that is my hypothesis. I believe I am using the "security" function wrong or possibly providing the wrong input "src" to the RSI function.
I have also tried switching RSI and security to see if I can fetch the 5 min series data and input that into my RSI function but that doesn't work any better. E.g.
_5min_rsi = rsi(security(tickerid, "5", src), 14)
Essentially, what I need to see is that no matter what time-frame I am on in trading view I should see 5 min RSI calculated correctly. In the current state, the code will only work in 1 min and 5 min time-frames which is obviously unacceptable.
Upvotes: 5
Views: 14737
Reputation: 684
According to documents:
security function was designed to request data of a timeframe higher than the current chart timeframe. On a 60 minutes chart, this would mean requesting 240, D, W, or any higher timeframe. It is not recommended to request data of a timeframe lower than the current chart timeframe, for example, 1-minute data from a 5 minutes chart. The main problem with such a case is that some part of a 1-minute data will be inevitably lost, as it’s impossible to display it on a 5 minutes chart and not to break the time axis. In such cases, the behavior of security can be rather unexpected.
Upvotes: 5
Reputation:
After playing a bit with the security()
function, I don't think the security()
function works this way.
If we set the resolution to "1"
(that is 1 minute) and go to the 1D
chart, we will get only values for the last minute bar per one daily bar.
out = security("AAPL", "1", close)
If we set them vice-versa ("D"
for resolution and 1m
chart), all of the minute bars will be identical - they are getting their value from the last daily bar.
out = security("AAPL", "D", close)
Upvotes: 5