仍然没有 Pinescript 4 中 int 类型转换的系列?

Don*_*der 6 pine-script

我正在尝试使用一个系列作为整数。Pinescript 4 出来了,但仍然没有办法做到这一点:

//@version=4 
study("Test Script", overlay=true) 

l = 1 
l := nz(l[1]) + 1 
l := l>20?1:l 
ma = sma(close, l) 
plot(ma, linewidth=4, color=color.black) 
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用“var”。这次没有错误但没有按预期工作

//@version=4 
study("Test Script", overlay=true) 

var l = 1 
l := l>=20?1:l+1 
ma = sma(close, l) 
plot(ma, linewidth=4, color=color.black)
Run Code Online (Sandbox Code Playgroud)

有什么建议?

小智 0

我仔细检查但无法找到将系列转换为整数的方法。

幸运的是,在您的情况下,您可以编写自定义 SMA 函数来解决标准函数的字面整数限制sma()

//@version=4 
study("Test Script", overlay=true) 
moving_sma(source_series, length) =>
    if length == 1.0 // if length is 1 we actually want the close instead of an average
        source_series
    else // otherwise we can take the close and loop length-1 previous values and divide them to get the moving average
        total = source_series
        for i = 1 to length - 1
            total := total + source_series[i]
        total / length

sma_length = 1.0
sma_length := nz(sma_length[1]) == 0.0 ? 1.0 : sma_length[1]
if sma_length < 20
    sma_length := sma_length + 1
else
    sma_length := 1

plot(moving_sma(close, sma_length), linewidth=4, color=color.yellow)
Run Code Online (Sandbox Code Playgroud)