【问题标题】:How to get an array with a timeseries slice in PineScript?How to get an array with a timeseries slice in PineScript?
【发布时间】:2022-12-02 10:25:33
【问题描述】:

I have the following code in Pinescript where I am basically trying to slice a timeseries into an array to be used within NextFunction.

When using an array instantiated directly with array.from, NextFunction works correctly. But when instantiated by LoadArray, it does not work. Why would be it so? How to turn LoadArray into a function compatible with NextFunction?

Ps.:NextFunction uses matrices to perform its computations.

LoadArray(series, length) =>
    res = array.new_float(length)
    for i = length - 1 to 0
        array.set(res, i, series[i])
    res := res

NextFunction(y) =>
    ...
    ...

y = LoadArray(close, 5)
y2 = array.from(1212.3, 1211.6, 1212.7, 1214.8, 1216.1)

res1 = NextFunction(y2) // works alright
res2 = NextFunction(y) // does not work

I expected that the resulting arrays y and y2 would be equal and behave in the very same way within NextFunction. But they, for any reason, do not.

I tried to use History Referencing without success.

【问题讨论】:

    标签: pine-script


    【解决方案1】:

    The two arrays don't contain the same type of variable.
    y2 (array.from) contains float type
    y (LoadArray) contains a serie float type, as [x] return a serie of values (see https://www.tradingview.com/pine-script-reference/v5/#op_[])
    It seems that your NextFunction does work with float only.

    Switching to the following LoadArray implementation should fix it.

    LoadArray(series, length) =>
        res = array.new<float>(length, series)
        res := res
    

    【讨论】:

    • Can you suggest a solution?
    猜你喜欢
    • 2022-12-02
    • 2022-12-02
    • 2022-12-01
    • 2022-12-01
    • 2022-12-01
    • 2022-12-01
    • 2022-12-02
    • 2022-12-26
    • 2022-12-01
    相关资源
    最近更新 更多