如何在 TradingView 的 pinescript 中开发多个时间框架 RSI

How to develop multiple timeframe RSI in pinescript for TradingView

我正在尝试在交易视图中使用 pine-script 开发多时间框架 RSI,但我似乎对长期图表视图中的短期 RSI 有疑问。

例如,以下代码将显示 5 分钟 RSI。如果我将图表设置为 5 分钟,它将适当地显示 RSI。但是当我 select 更大的时间范围(例如,1 小时等)时,该值变得不正确。

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)

我认为问题与正在操作的系列数据有关。出于某种原因,当我使用带有“5”的安全性作为我的分辨率时,它的数据在更高的时间框架图表中丢失,并且它使用了那个时间不同系列的收盘价。至少那是我的假设。我相信我错误地使用了 "security" 函数,或者可能向 RSI 函数提供了错误的输入 "src"。

我也试过切换 RSI 和安全性,看看我是否可以获取 5 分钟系列数据并将其输入到我的 RSI 函数中,但效果并不好。例如。

_5min_rsi = rsi(security(tickerid, "5", src), 14)

基本上,我需要看到的是,无论我在交易视图中处于什么时间范围,我都应该看到正确计算的 5 分钟 RSI。在当前状态下,代码只能在 1 分钟和 5 分钟的时间范围内工作,这显然是不可接受的。

玩了一下 security() 函数后,我认为 security() 函数不是这样工作的。

如果我们将分辨率设置为 "1"(即 1 分钟)并转到 1D 图表,我们将仅获得每个每日柱的最后一分钟柱的值。

out = security("AAPL", "1", close)

如果我们将它们设置为相反("D" 分辨率和 1m 图表),所有分钟柱将相同 - 它们从最后一个 每日条。

out = security("AAPL", "D", close)

根据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.