如何在Tradingview的Pine Script指标中添加多时间框架选项?

huangapple go评论138阅读模式
英文:

How do I add a multi time frame option in Pine Script indicator for Tradingview?

问题

我正在为TradingView创建一个脚本,使用了先前公开的指标。我添加了一些内容,但在尝试在图表上设置指标时,多时间框架输入不起作用。它仍然停留在同一个图表上(例如1分钟),但当我将其设置为其他时间框架(比如5分钟)时,它不会改变。

我已经添加了以下代码:

timeframe = input.timeframe(title="Time Frame", defval="1")

这部分保存和编译都没有问题,但在使用它时指标不会改变。所以我认为这可能是一个小错误。有人可以帮我调整一下吗?以下是完整的脚本:

//@version=5
indicator(title="Relative Strength Index x2", shorttitle="MTF RSIx2")

src = close
len_fast = input(14, title="Fast RSI Length")
len_slow = input(28, title="Slow RSI Length")
timeframe = input.timeframe(title="Time Frame", defval="1")

up_fast = ta.rma(math.max(ta.change(src), 0), len_fast)
down_fast = ta.rma(-math.min(ta.change(src), 0), len_fast)
rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast))

up_slow = ta.rma(math.max(ta.change(src), 0), len_slow)
down_slow = ta.rma(-math.min(ta.change(src), 0), len_slow)
rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow))

diff = rsi_fast - rsi_slow + 50
diff_color = diff > diff[1] ? color.new(color.green, 50) : color.new(color.red, 50)

diff_plot = plot(diff, title="Difference", color=diff_color, style=plot.style_columns, histbase=50)
rsi_slow_plot = plot(rsi_slow, title="RSI Slow", color=color.white)
rsi_fast_plot = plot(rsi_fast, title="RSI Fast", color=color.blue)

level1 = 70
level2 = 60
level3 = 40
level4 = 30

hline(level1, "Level 1", color.new(color.red, 50), linewidth=1)
hline(level2, "Level 2", color.new(color.white, 50), linewidth=1)
hline(level3, "Level 3", color.new(color.white, 50), linewidth=1)
hline(level4, "Level 4", color.new(color.lime, 50), linewidth=1)

我已经尝试添加了我提到的代码片段:

timeframe = input.timeframe(title="Time Frame", defval="1")

我期望这将允许我在1分钟图上使用指标,但可以将指标设置为显示5分钟的数据/绘图点,以获得更大的移动视图。

英文:

I'm creating a script for TradingView using a previous public indicator. I added a couple things, but having issues with the multi time frame input not working when I try setting the indicator on the chart. It just stays on the same chart (for instance, the 1m) but does not change when I set it to something else (let's say the 5m).

I already added the code:

timeframe = input.timeframe(title="Time Frame", defval="1")

This saves and compiles just fine but doesn't change the indicator when I'm using it. So I'm assuming it's a small error. Could anyone help me tweak this please? Here is the full script:

//@version=5
indicator(title="Relative Strength Index x2", shorttitle="MTF RSIx2")

src = close
len_fast = input(14, title="Fast RSI Length")
len_slow = input(28, title="Slow RSI Length")
timeframe = input.timeframe(title="Time Frame", defval="1")

up_fast = ta.rma(math.max(ta.change(src), 0), len_fast)
down_fast = ta.rma(-math.min(ta.change(src), 0), len_fast)
rsi_fast = down_fast == 0 ? 100 : up_fast == 0 ? 0 : 100 - (100 / (1 + up_fast / down_fast))

up_slow = ta.rma(math.max(ta.change(src), 0), len_slow)
down_slow = ta.rma(-math.min(ta.change(src), 0), len_slow)
rsi_slow = down_slow == 0 ? 100 : up_slow == 0 ? 0 : 100 - (100 / (1 + up_slow / down_slow))

diff = rsi_fast - rsi_slow + 50
diff_color = diff > diff[1] ? color.new(color.green, 50) : color.new(color.red, 50)

diff_plot = plot(diff, title="Difference", color=diff_color, style=plot.style_columns, histbase=50)
rsi_slow_plot = plot(rsi_slow, title="RSI Slow", color=color.white)
rsi_fast_plot = plot(rsi_fast, title="RSI Fast", color=color.blue)

level1 = 70
level2 = 60
level3 = 40
level4 = 30

hline(level1, "Level 1", color.new(color.red, 50), linewidth=1)
hline(level2, "Level 2", color.new(color.white, 50), linewidth=1)
hline(level3, "Level 3", color.new(color.white, 50), linewidth=1)
hline(level4, "Level 4", color.new(color.lime, 50), linewidth=1)

I already tried added the code snippet I mentioned...

timeframe = input.timeframe(title="Time Frame", defval="1")

I was expecting that would allow me to use the indicator on the 1m chart, but the indicator could be set to sow 5m data/plot points instead of 1m to get a larger view of the move.

答案1

得分: 0

input.timeframe() 仅仅是一个输入函数,用户可以选择要使用的时间框架。它不会自动使指标多时间框架。您仍然需要使用 security() 函数进行编码。

或者,您可以尝试在 indicator() 调用中添加 timeframe="", timeframe_gaps=true

英文:

input.timeframe() is just an input function so the user can select a timeframe to be used. It does not magically make the indicator multi timeframe. You still need to code for it using the security() function.

Alternatively, you can try adding timeframe="", timeframe_gaps=true to your indicator() call.

huangapple
  • 本文由 发表于 2023年6月19日 20:26:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506618.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定