How does the history reference (e.g. [1]) work on UDTs defined with the var
keyword in the new Pine Script version 6?
It works fine when it’s defined without the var
keyword, but I cannot get it to work with it. I’ve made a simple example script to demonstrate this. The history reference behaves as I expect if I remove the var
in the declaration, but not with it. Do you know what the problem is?
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © mickes
//@version=6
indicator("Test history", overlay = true)
type A
int B
var a = A.new()
a.B := bar_index
if not barstate.isfirst
label.new(bar_index, high, str.format("{0} == {1}n{2} != {3}", (a[1]).B, a.B, bar_index[1], bar_index))
1
In Pine v6, you can no longer use the history-referencing operator []
on the field of a user-defined type directly.Fix: Use the history-referencing operator on the UDT object instead,
then retrieve the field of the historic object. To do so, use the
syntax (myObject[10]).field – ensure the object’s historical reference
is wrapped in parentheses, otherwise it is invalid. Alternatively,
assign the UDT field to a variable first, and then use the
history-referencing operator [] on the variable to access its historic
value.
It is explained here.
1