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.
Ta bort från favoritskript Lägg till som favoritskript

Kommentarer

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 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:

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.
+1 Svara
Hem Aktie-screener Forex-screener Krypto-screener Ekonomisk kalender Hur det fungerar Diagramfunktioner Priser Tipsa en vän Ordningsregler Hjälpcenter Webbsidor och mäklarlösningar Widgets Diagramlösningar Lightweight Charting Library Blogg och nyheter Twitter
Profil Profilinställningar Konto och fakturering Hänvisade vänner Mynt Mina kölappar Hjälpcenter Publicerade idéer Följare Följer Privata meddelanden Chatt Logga ut