Reputation: 484
I have a situation in pine script where i want to achieve that log prints just once on the last bar, but it behaves incorrectly
When I use this code
var bool logged = false
if not loggeds
logged := true
log.warning("bar_index: {0}, logged: {1}", bar_index, logged)
It behaves as expected and log is outputed once on the first bar and then it stops
When I use this code
var bool logged = false
if not logged and barstate.islast
logged := true
log.warning("bar_index: {0}, logged: {1}", bar_index, logged)
Log continues to print even though expected result would be just once on the last bar.
I tried multiple approaches
var logged = false
if not logged and barstate.isrealtime
logged := true
log.warning("bar_index: {0}, logged: {1}", bar_index, logged)
var logged = false
if not logged[1] and barstate.islast
logged := true
log.warning("bar_index: {0}, logged: {1}", bar_index, logged)
var int last_logged_index = -1
if barstate.islast and bar_index != last_logged_index
last_logged_index := bar_index
log.warning("bar_index: {0}", bar_index)
var int last_logged_index = -1
if barstate.islast and barstate.isrealtime and bar_index != last_logged_index
last_logged_index := bar_index
log.warning("bar_index: {0}", bar_index)
but nothing seems to work. Log output is still there on every real time data update.
If you can give me a hint what is going on it would be much appreciated.
Update: Thank you for the reply, but still don't know how to solve the issue how to log just once on the last bar. This code still logs just once on the first historical bar
var logged = false
if not logged and barstate.isconfirmed
logged := true
log.warning("bar_index: {0}, logged: {1}", bar_index, logged)
and this one does not log at all
var logged = false
if not logged and barstate.isrealtime and barstate.isconfirmed
logged := true
log.warning("bar_index: {0}, logged: {1}", bar_index, logged)
Upvotes: 0
Views: 57
Reputation: 178
Pine script executes on each realtime update (tick change) with a rollback process. In the user manual, the execution model describes it as follows:
When the script arrives on the realtime bar it executes a first time. It uses the current values of the built-in variables to produce a set of results and plots them if required. Before the script executes another time when the next update happens, its user-defined variables are reset to a known state corresponding to that of the last commit at the close of the previous bar. If no commit was made on the variables because they are initialized every bar, then they are reinitialized. In both cases their last calculated state is lost. The state of plotted labels and lines is also reset. This resetting of the script’s user-defined variables and drawings prior to each new iteration of the script in the realtime bar is called rollback. Its effect is to reset the script to the same known state it was in when the realtime bar opened, so calculations in the realtime bar are always performed from a clean state.
However, Pine Logs do not roll back, which is what you've caught. The notes in Creating logs mention this:
Pine Logs do not roll back on each tick in an unconfirmed bar, meaning the results for those ticks show in the pane until the script restarts its execution. To only log messages on confirmed bars, use barstate.isconfirmed in the conditions that trigger a log.*() call.
Upvotes: 0