AG23
AG23

Reputation: 15

Using bar_index as length for other highest()

i running into some issues while using bar_index stored in variable to calculate the highest() in a range.

In my study i store the bar_index into a global variable f_barindex_enter := bar_index when i enter a position. After leaving the position i'm trying to retrieve the highest price between enter and exit f_barindex_distance = bar_index[0] - f_barindex_enter for further use in my study.

What i found is that when using highest(f_barindex_distance) generates a error when i apply the study on a chart. Pinescript help says that bar_index returns a integer but doesn't seem to work with highest(). I also tried to 'force' convert the variable to a integer by using int() but also generates a error.

am i doing something wrong?

Upvotes: 0

Views: 1034

Answers (1)

PineCoders-LucF
PineCoders-LucF

Reputation: 8789

You're probably not protecting highest() against all eventualities. See this example taken from here:

//@version=4
study("Lowest low since condition", "", true)
cond = rising(close, 3)
lookback = int(max(1, nz(barssince(cond)) + 1))
lowestSinceCondition = lowest(lookback)
plot(lowestSinceCondition)
// Show when condition occurs.
plotchar(cond, "cond", "•", location.top, size = size.tiny)
// Display varying lookback period in Data Window.
plotchar(lookback, "lookback", "", location.top, size = size.tiny)

Disclosure: the link in this answer points to a PineCoders FAQ entry.
I am a member of the PineCoders community and I most probably wrote that FAQ entry. PineCoders is a TradingView-supported group of volunteer Pine coders and PineCoders' website is strictly educational. Neither TradingView nor PineCoders benefits financially from sending traffic to pinecoders.com, and the site contains no affiliate/referral links.

Upvotes: 0

Related Questions