简体   繁体   中英

Passing array's element values to lowest or highest functions in pine script

I am trying to pass dynamic values to lowest and highest built-in functions in pine script from arrays. Below is the code

int[] pArray = array.new_int(0)

pArray is populated with int elements. Here is the code below that works.

fromInd         =   array.min(pArray)
lowest          =   ta.lowest(MACDHist,array.get(pArray,0))
Debug = label.new(bar_index+1, close, 'pArray: ' + str.tostring(pArray) + str.tostring(fromInd))

The output of above code on the chart is

pArray: [2, 15, 40]2

2 is the 0th index value of pArray that is printed outside of the array in the end. However when I change the label to output 'lowest' value (shown below) nothing prints to chart but everything compiles well with no errors or warnings .

Debug = label.new(bar_index+1, close, 'pArray: ' + str.tostring(pArray) + str.tostring(lowest))

The output of 'array.get(id, index)' is series int because pArray is initialized to int. I also typecasted the 'array.get(id, index)' argument by enclosing it with 'int()' but nothing is working for the last line code except when hard coded.

'ta.lowest(source, length)' accepts series int arguments for length and it can also accept dynamic lengths as shown by tradingview https://www.tradingview.com/blog/en/pine-functions-support-dynamic-length-arguments-20554/

What am I doing wrong here? Thanks in advance.

It should be working, at a guess, normally I'd say there must be something going on with how the array is populated, but it doesn't seem to be the case here given the array output. It's unlikely, but possible you've found a bug, or an even possibly an exception that isn't being picked up by the compiler.

//@version=5
indicator("Dynamic INT array length", overlay = false, max_bars_back = 5000)


int[] lengths = array.new_int()

for i = 0 to 10
    array.unshift(lengths, int(math.random(30, 50)))


[a, b, hist] = ta.macd(close, 26, 12, 9)

lowest = ta.lowest(hist, array.min(lengths))

plot(lowest)
plot(hist, color = color.red)

debug = label.new(x = bar_index, y = 0, style = label.style_label_left, text = str.tostring(lowest) + "   " + str.tostring(lengths) + "   " + str.tostring(array.min(lengths)))
label.delete(debug[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM