【问题标题】:Pine Script - Both left and right scales in a single layoutPine Script - 在单一布局中左右缩放
【发布时间】:2020-09-05 07:40:45
【问题描述】:

我想同时编码 RSI ADX 和 ATR。

RSI 和 ADX 是带状振荡器 (0-100),而 ATR 不是。如果 ATR 明显高于 100,那么整个地块就会被挤压。我想在相同的代码中添加 ATR,但只想将左轴用于 ATR。

我在情节代码上添加了scale=scale.left,但这似乎不起作用,并且出现以下错误:-

Add to Chart operation failed, reason:
-line 79: Unknown argument 'scale' of type 'const integer';
-line 79: Cannot call 'plot' with arguments (type_unknown, title=literal string, color=literal color, transp=literal integer, scale=const integer); available overloads: plot(series[float], const string, series[color], input integer, input integer, input bool, input integer, input float, series[integer], input bool, series[float], const bool, input integer, const integer, string) => plot; plot(fun_arg__<arg_series_type>, const string, fun_arg__<arg_color_type>, input integer, input integer, input bool, input integer, input float, series[integer], input bool, series[float], const bool, input integer, const integer, string) => plot

我写了以下代码:-

//          RSI             //


len = input(14, minval=1, title="RSI Length")
rsisrc = input(close, "RSI Source", type = input.source)
up = rma(max(change(rsisrc), 0), len)
down = rma(-min(change(rsisrc), 0), len)
rsi = down == 0 ? 100 : up == 0 ? 0 : 100 - (100 / (1 + up / down))
plot(rsi, "RSI", color=#ffa726)
band1 = hline(60, "RSI Upper Band", color=#C0C0C0)
band2 = hline(50, "RSI Middle Band", color=#C0C0C0)
band0 = hline(40, "RSI Lower Band", color=#C0C0C0)
fill(band1, band0, color=#ffa726, transp=92, title="RSI Background")


//          ADX             //


adxlen = input(8, title="ADX Smoothing")
dilen = input(13, title="DI Length")
dirmov(len) =>
    up = change(high)
    down = -change(low)
    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)
    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)
    truerange = rma(tr, len)
    plus = fixnan(100 * rma(plusDM, len) / truerange)
    minus = fixnan(100 * rma(minusDM, len) / truerange)
    [plus, minus]

adx(dilen, adxlen) =>
    [plus, minus] = dirmov(dilen)
    sum = plus + minus
    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)

sig = adx(dilen, adxlen)

plot(sig, color=#e57373, title="ADX")
band3 = hline(25, "ADX Line", color=#C0C0C0)

//          ATR             //

length = input(title="Length", defval=14, minval=1)
smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
ma_function(source, length) =>
    if smoothing == "RMA"
        rma(source, length)
    else
        if smoothing == "SMA"
            sma(source, length)
        else
            if smoothing == "EMA"
                ema(source, length)
            else
                wma(source, length)
plot(ma_function(tr(true), length), title = "ATR", color=#991515, transp=0, scale=scale.left)

请帮助我并告诉我该怎么做。

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    正如@PineCoders-LucF 所说,“一个脚本不能使用两种不同的比例”,但是......

    但您可以尝试根据需要自定义原始数学公式,使其限制在 0-100 范围内,同时在一定程度上保留绘制线和原始公式的信息。这将需要您对如何在数学上使这些值适应新的比例(在这种情况下为 0 到 100%)进行一些研究。例如,RSI 值以这种方式进行操作,即转换为 0-100 值。对于 ATR,这意味着您必须对它将获得的 0-100 值给出自己的解释。

    我花时间为你做:

    // ATR for 0-100 range
    //@version=4
    enableATR = input(true, title="Enable ATR")
    length = input(title="ATR Length", defval=14, minval=1)
    smoothing = input(title="ATR Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
    ma_function(source, length) =>
        if smoothing == "RMA"
            up = rma(max(change(source), 0), length)
            down = rma(-min(change(source), 0), length)
            rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
        else
            if smoothing == "SMA"
                up = sma(max(change(source), 0), length)
                down = sma(-min(change(source), 0), length)
                rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
            else
                if smoothing == "EMA"
                    up = ema(max(change(source), 0), length)
                    down = ema(-min(change(source), 0), length)
                    rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
                else
                    up = wma(max(change(source), 0), length)
                    down = wma(-min(change(source), 0), length)
                    rma = (down == 0 ? 100 : (up == 0 ? 0 : 100) - (100 / (1 + up / down)))
    atrValue = ma_function(tr(true), length)
    stretchedAtrValue = atrValue < 50 ? atrValue - atrValue * 0.35 : atrValue + atrValue * 0.35
    atrSmoothed = sma(stretchedAtrValue, length)
    plot(enableATR and atrSmoothed ? atrSmoothed : na, title = "ATR", color=color.orange, transp=0)
    

    这个脚本在我的电视上完美运行。

    请记住,此转换需要您代表您进一步研究此值与原始 ATR 值的可比性。请考虑这样做并将您的结论添加到这篇文章中。

    两个 ATR 之间有相似之处。

    这 3 行是我能想到的最好的改进。两个新的行更好的输入:

    lowerATR = input(title="ATR Lower Extention", defval=0.35)
    upperATR = input(title="ATR Upper Extention", defval=0.35)
    

    还有这个替换:

    stretchedAtrValue = atrValue < 50 ? atrValue - atrValue * lowerATR : atrValue + atrValue * upperATR
    

    有一种方法可以移动 ATR 曲线移动所围绕的线的中心度,从 50 到假设为 10,这将使它更类似于原始 ATR 曲线,但我(再次)找不到它。

    【讨论】:

    • 稍后我会及时添加它的改进版本。
    【解决方案2】:

    一个脚本不能使用两种不同的比例。

    【讨论】:

    • 哦!如果您找到将 ATR 添加到同一脚本的任何方法,请告诉我。提前致谢
    【解决方案3】:

    为指标设置添加一个乘数。您必须为每只股票选择自己的乘数,这很不方便,但到目前为止我还没有找到其他选择。

            //          ATR             //
        
        length = input(title="Length", defval=14, minval=1)
        smoothing = input(title="Smoothing", defval="RMA", options=["RMA", "SMA", "EMA", "WMA"])
        multiplier = input.int(title="Multiplier for ATR",  minval = 1, defval = 1)
        ma_function(source, length) =>
            if smoothing == "RMA"
                rma(source, length)
            else
                if smoothing == "SMA"
                    sma(source, length)
                else
                    if smoothing == "EMA"
                        ema(source, length)
                    else
                        wma(source, length)
        plot(ma_function(source*multiplier, length), title = "ATR", color=#991515, transp=0)
    

    【讨论】:

    • 请格式化您的代码。
    猜你喜欢
    • 1970-01-01
    • 2019-03-13
    • 2022-11-28
    • 2021-06-09
    • 2018-05-17
    • 2016-11-05
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多