CS2301
warning name is the same object on every bar, so name[n] returns the same object.
#What it means
This is a warning. A state collection (array, map or matrix) is one object that lives across bars, so name[1] gives you the same object, not a copy of how it looked on the previous bar.
#Typical cause
- History (
[n]) on a collection declared withstateorlive state.
#How to fix it
Read the past element from the collection itself (for example closes.get(closes.size() - 2)), or keep a separate series of the number you need and use history on that.
#Example
//@chartscript 1
indicator("Stored closes")
state closes: float[] = array.new<float>()
closes.push(close)
const prev = closes[1]
plot(prev.size())Fixed
//@chartscript 1
indicator("Stored closes")
state closes: float[] = array.new<float>()
closes.push(close)
plot(closes.size() - 1)