Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pine Script - Both left and right scales in a single layout

Tags:

pine-script

I want to code RSI ADX and ATR together.

RSI and ADX are banded oscillators (0-100), whereas ATR is not. if ATR is significantly above 100 then the whole plot gets squeezed. I want to add ATR in the same code but want to use the left axis only for the ATR.

I have added the scale=scale.left on the plot code, but that doesn't seem to work and I get the following error: -

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

I have written the following code:-

//          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)

Please help me and tell me what to do.

like image 507
Kunthu Dhadda Avatar asked Sep 13 '25 23:09

Kunthu Dhadda


1 Answers

As @PineCoders-LucF said, "One script cannot use two different scales", BUT...

But you can try to custom the original mathematical formula to your needs, so it can get restricted to the the 0-100 range, while somewhat preserving the the information of the plotted line and original formula. This will require from you some research on how to adapt mathematically the values to the new scales (0 to 100% in this case). For example, the RSI value is manipulated in such a way, that is transformed into a 0-100 value. For ATR, it would mean that you will have to give your own interpretation to the 0-100 value it will get.

I took the time to do it for you:

// 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)

This script works perfectly in my TV.

Keep in mind that this transformation requires further research on you behalf over how this value is comparable to that of the original ATR. Please, consider doing that and adding your conclusions to this post.

enter image description here

There's a similarity between both ATRs.

enter image description here

This 3 lines are the best improvements I could come up with. Two new lines for better input:

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

And this replacement:

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

There's a way to move the centerness of the lines around which the ATR curve moves, from 50 to let's say 10, which would make it more similar to the original ATR curve, but I couldn't find it (again).

like image 50
carloswm85 Avatar answered Sep 17 '25 19:09

carloswm85