0

I have noticed that after this simple indicator is first added to the chart, removing and adding it again (without any changes to the code) does not change the time of the last confirmed history bar. Is this supposed to be like this? (I am using Brave browser if that matters)

//@version=5
indicator("My script", overlay = true, max_labels_count = 500)
bgcolor(barstate.islastconfirmedhistory ? color.rgb(232, 196, 238) : na)
plotchar(na,"","",location =  location.top)
Moebius
  • 329
  • 1
  • 7
  • Does this answer your question? [How to use barstate.islastconfirmedhistory](https://stackoverflow.com/questions/70924747/how-to-use-barstate-islastconfirmedhistory) – elod008 Feb 01 '23 at 09:52
  • Thank you for the reference. No, it does not answer. This wording by @vitruvius is exactly the behavior I expect from `barstate.islastconfirmedhistory`: "If the instrument is trading and if new bars form, it will still point to the same bar as when you added the indicator to your chart. " – Moebius Feb 02 '23 at 22:45
  • The key words to my question are "same bar as when you added the indicator to your chart". Now, e.g. on 1m timeframe, if I add the indicator at 00:05:20 the `barstate.islastconfirmedhistory` will point to (or rather be true for) the last history bar at 00:04. Now when I remove the indicator and add it again at, e.g. 00:10:20 I would expect it to point to the bar at 00:09, but to my surprise it still highlights the bar at 00:04. – Moebius Feb 02 '23 at 22:46
  • Actually, I have run other tests, it looks that the indicator keeps running even after removal from the chart. And if you just add it again it will keep all its progress. (I had some indicators printing to a label from realtime bars, and all the lines printed before removal were still there with newly added lines after I removed and added the indicator again). – Moebius Feb 02 '23 at 22:46
  • I'm with you. Pine script is very much different from other "regular" programming languages. The way I see it, so that it also explains this behaviour, as you choose a timefram/ load a ticker, pine script makes a snapshot from the bar state. I conducted the same tests like you and that's the only explanation even if I'd expect this differently. So if islastconfirmedhistory is called, not your indicator is responsible for the evaluation but the execution model of pine script that has a snapshot to evaluate with. – elod008 Feb 03 '23 at 13:29
  • Yep, many strange things. E.g here we see that request.security() returns data for higher TF sometimes on the last bar of the HTF bar and sometimes at the bar following after the HTF bar, irregularly: ' //@version=5 indicator("My script", overlay = false, max_labels_count = 500) htfBi = request.security(syminfo.tickerid,"3M",bar_index, lookahead = barmerge.lookahead_off) bgcolor(ta.change(htfBi) ? color.teal : na) plot(htfBi) ` – Moebius Feb 07 '23 at 22:53

2 Answers2

0

What is lastconfirmedhistory at all?

Returns true if script is executing on the dataset's last bar when market is closed, or script is executing on the bar immediately preceding the real-time bar, if market is open. Returns false otherwise.

source
It is supposed to work like that. The determinant here is the data set of tradingview concerning historical- and real-time bars. Your browser does not matter at all. It's the data history or data change respectively and the execution model of pine script that is responsible for this.
Apperantly if you load a ticker with a timeframe, it sets the baseline for the lastconfirmed history, not the adding of the indicator.

@vitruvius had an excellent answer on this here

You could use barstate.iconfirmed for what you may need however note that bgcolor() is not dynamic meaning you cannot change it from a future point (eg. remove it). So if you use a label.new() instead you can achieve exactly what you want like this:

var label l = na
if barstate.isconfirmed and not barstate.ishistory
    label.delete(l)
    l := label.new(bar_index, high, "mark previous bar")

elod008
  • 1,227
  • 1
  • 5
  • 15
  • Try running it on 1S timeframe (or 1M if you do not have seconds). After some new (realtime) bars are plotted remove the indicator. Now the lastconfirmedhistory bar has changed. If you add the indicator again it should point to the current (new) last confirmed history bar (the dataset grew bigger, its last bar is not the same as before). The realtime bars are those that occur in realtime after the indicator is added, correct? – Moebius Feb 01 '23 at 00:37
  • Check out the reference from @vitruvius I also added in my edited answer. He explained it very well. – elod008 Feb 01 '23 at 09:55
0

When you delete and re-add the same script to the chart, it will use a cached version of the script instead of calculating it from scratch. Make any minor changes (e.g., add a space) to force it to recompile. This is a handy feature (especially for heavy scripts that take time to compile) that allows you to instantly add a script after deletion.

e2e4
  • 3,428
  • 6
  • 18
  • 30