Lemrin

Barssince Context Test

This is just published for visibility as a public service until the Pine devs are able to fix or document this behavior. The barssince() function returns different values when inside a conditional context. As long as it can be documented (and relied upon), this could be a pretty cool feature, but right now this is now how I read the help documentation to describe the function's intended behavior.

Anyway, in the script you'll see that test and test2 agree on red bars, but on green bars test gets a much lower value, which was pretty shocking to me until I traced down this particular cause within my larger script.
Remove from Favorite Scripts Add to Favorite Scripts

Comments

This is due to a change introduced in Pine v4. The change and how to avoid its pitfalls are explained here in our FAQ. The FAQ entry also links to the relevant User Manual section:
https://www.pinecoders.com/faq_and_code/#why-do-some-functions-and-built-ins-evaluate-incorrectly-in-if-or-ternary--blocks
+2 Reply
Lemrin PineCoders
@PineCoders, Thank you. Agreed, this can be worked around, but I hope there are plans to update the help documentation to reflect this new behavior for all affected functions. Like I said, it could be a very cool feature if it is intended, but as an easter egg I think it tends to go against the principle of least astonishment.

Edit referring to this one: https://www.tradingview.com/pine-script-reference/v4/#fun_barssince , specifically the description:

RETURNS
Number of bars since condition was true.

Perhaps rewrite to say something like it returns the number of bars since condition was true, ignoring those bars for which the function was not in context to execute.
+2 Reply
Home Stock Screener Forex Screener Crypto Screener Economic Calendar How It Works Chart Features Pricing Refer a friend House Rules Help Center Website & Broker Solutions Widgets Charting Solutions Lightweight Charting Library Blog & News Twitter
Profile Profile Settings Account and Billing Referred friends Coins My Support Tickets Help Center Ideas Published Followers Following Private Messages Chat Sign Out