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

时间:2019-07-08 13:03:01

标签: 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) 

我也尝试使用“ 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)

有什么建议吗?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

这将使编码更快。是在alexgrover之前并从Functions Allowing Series As Length - PineCoders FAQ脚本中提取出来的:

Sma(src,p) => a = cum(src), (a - a[max(p,0)])/max(p,0)