6

Trying to plot values from an array but I find needing to plot each value separately. How do you create a series to plot this?

// © SKnight79

//@version=4
study("My Script")

pma1 = array.new_float(5,0)

array.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 1) + close * 1) / 10))
array.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 2) + close * 2) / 10))
array.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 3) + close * 3) / 10))
array.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 3) + close * 3) / 10))
array.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 5) + close * 5) / 10))

plot(array.get(pma1,0), title="MA 1 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=1, show_last=1)
plot(array.get(pma1,1), title="MA 2 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=2, show_last=1)
plot(array.get(pma1,2), title="MA 3 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=3, show_last=1)
plot(array.get(pma1,3), title="MA 4 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=4, show_last=1)
plot(array.get(pma1,4), title="MA 5 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=5, show_last=1)
SKnight79
  • 61
  • 1
  • 5
  • 3
    For the part that you already have, you can use a for-loop to set the values of the array. However, plotting them within the loop gives the error "Cannot use 'plot' in local scope."... I cannot find a good approach either, which is quite crucial if you want to have a dynamic number of elements in the array which you cannot hard code like you did. – Stormbringer Nov 22 '21 at 20:27

1 Answers1

0

In pinescript you can't use plot in a local scope, so it is impossible to use plot in a for loop.
A workaround is to record all you value in a matrix
and on the last bar, redraw those value with line.new which can be used in local scope. A working example with 5 values:

//@version=5
indicator("Line from Array", overlay = true, max_lines_count = 500)

var MyMatrix = matrix.new<float>(0, 6, na)
// Col 0 = bar_index        Col 1 = value type1     Col : value type2 ...

matrix.add_row(MyMatrix)

NumRows = matrix.rows(MyMatrix) -1
matrix.set(MyMatrix, NumRows, 0, bar_index)
matrix.set(MyMatrix, NumRows, 1, close*1.05)
matrix.set(MyMatrix, NumRows, 2, close*1.10)
matrix.set(MyMatrix, NumRows, 3, close*1.15)
matrix.set(MyMatrix, NumRows, 4, close*1.25)
matrix.set(MyMatrix, NumRows, 5, close*1.35)

// Limit the matrix size to 500 / 5 columns as we can only draw 500 lines
if matrix.rows(MyMatrix) > 100
    matrix.remove_row(MyMatrix, 0)

if barstate.isrealtime
    for lig = 1 to matrix.rows(MyMatrix)-1
        x1 = int(matrix.get(MyMatrix, lig-1, 0))
        x2 = int(matrix.get(MyMatrix, lig, 0))
        for col = 1 to 5
            y1 = matrix.get(MyMatrix, lig-1, col)
            y2 = matrix.get(MyMatrix, lig, col)
            line.new(x1=x1, y1=y1, x2=x2, y2=y2)

Will give this on your chart : enter image description here

G.Lebret
  • 2,826
  • 2
  • 16
  • 27