'How to extract a single value from a series?
I want to create a series in which every value is normalized to the value of a particular element in the series, for example the latest value.
If you try syntax that would work in other common languages:
normalized_close = close / close[0]
the result will be an array of 1's because Pine is taking the ratio between the closing price relative to the closing price of the same bar (0 offset). I want to get the ratio between the closing price in each bar relative to the single closing price of the latest bar. In pseudocode, it would look something like this.
latest_close = get_value_at_index(close, 0)
normalized_close = close / latest_close
Where the function get_value_at_index(series, index)
is the function I can't seem to find.
Solution 1:[1]
You can create an array
, set its values with a for
loop and get the value at any index you want.
Here is an example with seeting an index with 1000 close
prices and getting the result from 3 bars ago:
//@version=5
indicator("My script")
// create new array
normalized_close_array = array.new_float(1000)
// append to that array
for i=1 to array.size(normalized_close_array) - 1
array.set(normalized_close_array, i, close[i] / close)
// get the value at any index you want
value_we_want = array.get(normalized_close_array, 3)
plot(value_we_want)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | mr_statler |