Lydo
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 5th, 2020
Contact: TradingView Profile

Issue with resolution/security functions

Hi Guys,

I need some help please. I'm trying to integrate data from the Aroon indicator into another indicator I'm working on.

Problem:
I started off with the built in Aroon that uses the resolution function within Study. When using it on say 1min and I switch timeframe to 2min every second bar displays the data as "n/a". Basically I need it to to display the value for the previous bar in this case.

//@version=4
study(title="Aroon", shorttitle="Aroon", overlay=false, format=format.percent, precision=2, resolution="")
length = input(14, minval=1)
upper = 100 * (highestbars(high, length+1) + length)/length
lower = 100 * (lowestbars(low, length+1) + length)/length
plot(upper, "Aroon Up", color=#FF6A00)
plot(lower, "Aroon Down", color=#0094FF)

Investigation:
1) Firstly, I tried to just put in a simple if statement, which worked fine in previous indicators I've use but no luck here:
if upper == na
upper := upper[1]

2) I thought it was potentially an issue with the resolution function so I re-wrote it using security():
//@version=4
study(title="Aroon Test", shorttitle="Aroon Test", overlay=false, format=format.percent, precision=2)

aroonResolution = input(title="Aroon Resolution", type=input.resolution, defval="2")
length = input(14, minval=1)
aroonLength = security(syminfo.tickerid, aroonResolution, length)
aroonHigh = security(syminfo.tickerid, aroonResolution, high)
aroonLow = security(syminfo.tickerid, aroonResolution, low)

plot(length, color=color.yellow)
plot(aroonHigh, color=color.blue)
plot(aroonLow, color=color.purple)

upper = 100 * (highestbars(aroonHigh, aroonLength+1) + aroonLength)/aroonLength

if upper == na
upper := upper[1]

lower = 100 * (lowestbars(aroonLow, length+1) + length)/length

if lower == na
lower := lower[1]


colorGreen = #00ff22
colorRed = #ff1500
plot(upper, "Aroon Up", color=colorGreen)
plot(lower, "Aroon Down", color=colorRed)

This works fine when using the current timeframe but once you switch to 2min the calculations for upper and lower are now off. I'm pretty sure it's the highestbars() function that's now the issue.

Other related resources I've been reading up:
https://www.tradingview.com/support/sol ... plays-n-a/
https://www.tradingview.com/chart/TLT/g ... #tc4114362

I feel like I've gone down the rabbit hole with something that should be very straight forward. Any help would be appreciated, thanks

Lydo
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 5th, 2020
Contact: TradingView Profile

Re: Issue with resolution/security functions

I figured out the maths so it's ok, problem solved. I just needed to divide the current interval into the higher interval timeframe and multiply that by the length. Set that as the length of the lower timeframe and the lines drew as expected.

User avatar
Matthew
Site Admin
Site Admin
Posts: 92
Joined: July 1st, 2020
Location: Australia
Contact: Website Facebook Twitter TradingView Profile

Re: Issue with resolution/security functions

That's awesome Lydo! Well done solving this complex issue on your own :)

Lydo
Pine Script Rookie
Pine Script Rookie
Posts: 3
Joined: September 5th, 2020
Contact: TradingView Profile

Re: Issue with resolution/security functions

Thanks Matt, it certainly was a head scratcher!

Return to “Pine Script Q&A”