在 Tradingview 上,我如何在同一窗口中看到 2 个振荡器,但比例不同?

问题描述 投票:0回答:1

我正在编写简单的 pinescript 指标,在这种情况下,我想结合 ATR 和 RSI,两者都设置为 overlay=false(意思是,它们不在图表上方,而是在单独的振荡器窗口中)。

问题是,由于 ATR(可以从 0 到数千,具体取决于资产)和 RSI(0 到 100)的性质,当资产的 ATR 太低或太高时,RSI 可能不可见给它。

有办法解决这个问题吗?谢谢!

我在谷歌上寻找帮助,但没有找到任何帮助。此外,overlay=true 并没有像我想要的那样工作——它覆盖在图表本身上。

trading tradingview-api indicator
1个回答
0
投票

您的 RSI 在 0 到 100 之间。
您可以将您的 ATR 调整到这个比例(该值将不再是 ATR 值,但您会看到 ATR 的演变)

为此,您必须知道图表上 ATR 的最大值和最小值。
首先,您需要知道屏幕上的柱数,然后您才能在屏幕上找到 ATR 的最小值和最大值。
一旦你有了最小值和最大值,你就有了你的 ATR 值的范围,然后很容易在 0 到 100 之间进行调整。

这里是一个工作代码:

//@version=5
indicator(title="ATR & RSI", shorttitle="ATR & RSI", overlay=false, max_bars_back = 500)

// Find the number of bar on the screen
time_on_chart = (time - chart.left_visible_bar_time)/1000 // time in second
number_of_bar = int(time_on_chart / timeframe.in_seconds()) + 1

// ATR
length = input.int(title="Length", defval=14, minval=1)
smoothing = input.string(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
    switch smoothing
        "RMA" => ta.rma(source, length)
        "SMA" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        => ta.wma(source, length)
ATR = ma_function(ta.tr(true), length)

// Find the max value for ATR on screen
Max_Atr = ta.highest(ATR, number_of_bar > 0 ? number_of_bar : 1) 
Min_Atr = ta.lowest(ATR, number_of_bar > 0 ? number_of_bar : 1)


Range_ATR  = Max_Atr - Min_Atr
coef = 100/ Max_Atr

plot(ATR*coef - Min_Atr, title = "ATR", color=color.new(#B71C1C, 0))

// Calcul of thr RSI
ma(source, length, type) =>
    switch type
        "SMA" => ta.sma(source, length)
        "Bollinger Bands" => ta.sma(source, length)
        "EMA" => ta.ema(source, length)
        "SMMA (RMA)" => ta.rma(source, length)
        "WMA" => ta.wma(source, length)
        "VWMA" => ta.vwma(source, length)

rsiLengthInput = input.int(14, minval=1, title="RSI Length", group="RSI Settings")
rsiSourceInput = input.source(close, "Source", group="RSI Settings")
maTypeInput = input.string("SMA", title="MA Type", options=["SMA", "Bollinger Bands", "EMA", "SMMA (RMA)", "WMA", "VWMA"], group="MA Settings")
maLengthInput = input.int(14, title="MA Length", group="MA Settings")
bbMultInput = input.float(2.0, minval=0.001, maxval=50, title="BB StdDev", group="MA Settings")

up = ta.rma(math.max(ta.change(rsiSourceInput), 0), rsiLengthInput)
down = ta.rma(-math.min(ta.change(rsiSourceInput), 0), rsiLengthInput)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
rsiMA = ma(rsi, maLengthInput, maTypeInput)
isBB = maTypeInput == "Bollinger Bands"

plot(rsi, "RSI", color=#7E57C2)
//plot(rsiMA, "RSI-based MA", color=color.yellow)
rsiUpperBand = hline(70, "RSI Upper Band", color=#787B86)
hline(50, "RSI Middle Band", color=color.new(#787B86, 50))
rsiLowerBand = hline(30, "RSI Lower Band", color=#787B86)
fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background Fill")
bbUpperBand = plot(isBB ? rsiMA + ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Upper Bollinger Band", color=color.green)
bbLowerBand = plot(isBB ? rsiMA - ta.stdev(rsi, maLengthInput) * bbMultInput : na, title = "Lower Bollinger Band", color=color.green)
fill(bbUpperBand, bbLowerBand, color= isBB ? color.new(color.green, 90) : na, title="Bollinger Bands Background Fill")
© www.soinside.com 2019 - 2024. All rights reserved.