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.
Open-source script

In true TradingView spirit, the author of this script has published it open-source, so traders can understand and verify it. Cheers to the author! You may use it for free, but reuse of this code in a publication is governed by House Rules. You can favorite it to use it on a chart.

Want to use this script on a chart?


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:
+2 Svara
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:

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 Svara
Hem Aktie-screener Forex-screener Krypto-screener Ekonomisk kalender Om Diagramfunktioner Priser Tipsa en vän Ordningsregler Hjälpcenter Webbsidor och mäklarlösningar Widgets Diagramlösningar Lightweight Charting Library Blogg och nyheter Twitter