为什么在pinescript的for循环中sma与wma如此不同?

时间:2019-05-09 08:10:23

标签: pine-script tradingview-api

我正尝试将下面的MA与tradingview的pinescript相加。
但是结果却大不相同。
wma一个是正确的,但是sma一个是奇怪的..
任何想法?

sum_ma = 0
for i = 1 to 30
    sum_ma := sum_ma + i * sma(close[i], 1)
plot(sum_ma)

sum_ma := 0
for i = 1 to 30
    sum_ma := sum_ma + i * wma(close[i], 1)
plot(sum_ma)

1 个答案:

答案 0 :(得分:1)

sma有问题。改用松树写的sma

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

pine_sma(src, len) =>
    sum = 0.0
    for i = 0 to len - 1
        sum := sum + src[i] / len
    sum

sum_ma = 0.0
for i = 1 to 30
    sum_ma := sum_ma + i * pine_sma(close[i], 1)
plot(sum_ma, color=blue)

sum_ma := 0
for i = 1 to 30
    sum_ma := sum_ma + i * wma(close[i], 1)
plot(sum_ma, color=red)