如何使用 pinescript 绘制数组中的值

SKn*_*t79 6 arrays plot tradingview-api

尝试绘制数组中的值,但我发现需要单独绘制每个值。你如何创建一个系列来描绘这个?

\n
// \xc2\xa9 SKnight79\n\n//@version=4\nstudy("My Script")\n\npma1 = array.new_float(5,0)\n\narray.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 1) + close * 1) / 10))\narray.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 2) + close * 2) / 10))\narray.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 3) + close * 3) / 10))\narray.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 3) + close * 3) / 10))\narray.push(pma1, security(syminfo.tickerid, "", (ema(close, 10 - 1) * (10 - 5) + close * 5) / 10))\n\nplot(array.get(pma1,0), title="MA 1 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=1, show_last=1)\nplot(array.get(pma1,1), title="MA 2 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=2, show_last=1)\nplot(array.get(pma1,2), title="MA 3 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=3, show_last=1)\nplot(array.get(pma1,3), title="MA 4 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=4, show_last=1)\nplot(array.get(pma1,4), title="MA 5 Forecast", color=color.red, linewidth=1, style=plot.style_circles, offset=5, show_last=1)\n
Run Code Online (Sandbox Code Playgroud)\n

G.L*_*ret 0

在 pinescript 中,您无法在本地范围内使用绘图,因此不可能在 for 循环中使用绘图。
解决方法是将所有值记录在矩阵中
,并在最后一个柱上,使用可在本地范围内使用的 line.new 重新绘制这些值。具有 5 个值的工作示例:

//@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)
Run Code Online (Sandbox Code Playgroud)

将在您的图表上给出此信息: 在此输入图像描述