将linreg V1函数转换为V5(Tradingview – Pine Script)

huangapple go评论96阅读模式
英文:

Converting a linreg V1 Function to V5 (Tradingview - Pine Script)

问题

我尝试将pine v1 linreg函数转换为v3(然后自动转换为V5),但一直出现以下错误:

输入处的语法错误 'end of line without line continuation'

这是V1的代码:

  1. val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)),
  2. lengthKC,0)

所以我在pine脚本语言参考中查找了一下,函数应该是这样的:

linreg(source, length, offset) → series

如果我没弄错的话,source应该是这样的:

source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),

长度:

sma(close,lengthKC)),

和偏移:

lengthKC,0)

所以我在V3中写成了这样:

  1. val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))

但它一直告诉我:

输入不匹配 ',' 期望 ')'

我尝试了一些其他解决方案,但无法解决这个问题。

编辑:
这是完整的代码:

  1. study(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator [LazyBear]", overlay=false)
  2. length = input(20, title="BB Length")
  3. mult = input(2.0,title="BB MultFactor")
  4. lengthKC=input(20, title="KC Length")
  5. multKC = input(1.5, title="KC MultFactor")
  6. useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)
  7. // Calculate BB
  8. source = close
  9. basis = sma(source, length)
  10. dev = multKC * stdev(source, length)
  11. upperBB = basis + dev
  12. lowerBB = basis - dev
  13. // Calculate KC
  14. ma = sma(source, lengthKC)
  15. range = useTrueRange ? tr : (high - low)
  16. rangema = sma(range, lengthKC)
  17. upperKC = ma + rangema * multKC
  18. lowerKC = ma - rangema * multKC
  19. sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC)
  20. sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
  21. noSqz = (sqzOn == false) and (sqzOff == false)
  22. val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))
  23. bcolor = iff( val > 0,
  24. iff( val > nz(val[1]), lime, green),
  25. iff( val < nz(val[1]), red, maroon))
  26. scolor = noSqz ? blue : sqzOn ? black : gray
  27. plot(val, color=bcolor, style=histogram, linewidth=4)
  28. plot(0, color=scolor, style=cross, linewidth=2)
英文:

I tried to convert pine v1 linreg function to v3 (and afterwards convert it automatically to V5) but I kept getting this error:

> syntax error at input 'end of line without line continuation'

This is the V1 code

  1. val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)),
  2. lengthKC,0)

So I looked it up in the pine script language reference and the function should look like this:

> linreg(source, length, offset) → series

If I'm correct, the source should be this:

> source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),

Length:

> sma(close,lengthKC)),

And Offset:

> lengthKC,0)

So I wrote it like this in V3:

val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))

But it keeps telling me

> mismatched input ',' expecting ')'

I played around with some other solutions but couldnt figure out how to solve the problem.

EDIT:
This is the full code:

  1. study(shorttitle = &quot;SQZMOM_LB&quot;, title=&quot;Squeeze Momentum Indicator [LazyBear]&quot;, overlay=false)
  2. length = input(20, title=&quot;BB Length&quot;)
  3. mult = input(2.0,title=&quot;BB MultFactor&quot;)
  4. lengthKC=input(20, title=&quot;KC Length&quot;)
  5. multKC = input(1.5, title=&quot;KC MultFactor&quot;)
  6. useTrueRange = input(true, title=&quot;Use TrueRange (KC)&quot;, type=bool)
  7. // Calculate BB
  8. source = close
  9. basis = sma(source, length)
  10. dev = multKC * stdev(source, length)
  11. upperBB = basis + dev
  12. lowerBB = basis - dev
  13. // Calculate KC
  14. ma = sma(source, lengthKC)
  15. range = useTrueRange ? tr : (high - low)
  16. rangema = sma(range, lengthKC)
  17. upperKC = ma + rangema * multKC
  18. lowerKC = ma - rangema * multKC
  19. sqzOn = (lowerBB &gt; lowerKC) and (upperBB &lt; upperKC)
  20. sqzOff = (lowerBB &lt; lowerKC) and (upperBB &gt; upperKC)
  21. noSqz = (sqzOn == false) and (sqzOff == false)
  22. val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))
  23. bcolor = iff( val &gt; 0,
  24. iff( val &gt; nz(val[1]), lime, green),
  25. iff( val &lt; nz(val[1]), red, maroon))
  26. scolor = noSqz ? blue : sqzOn ? black : gray
  27. plot(val, color=bcolor, style=histogram, linewidth=4)
  28. plot(0, color=scolor, style=cross, linewidth=2)```
  29. </details>
  30. # 答案1
  31. **得分**: 0
  32. 原始代码略有不同。
  33. `val` 的定义如下:

val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)), sma(close, lengthKC)), lengthKC, 0)

  1. 如果您在指标列表中搜索 *Squeeze Momentum Indicator [LazyBear]*,您可以找到该指标。
  2. [![enter image description here][1]][1]
  3. <details>
  4. <summary>英文:</summary>
  5. The original code is slightly different.
  6. The definition of `val` is as below:
  7. val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)), lengthKC,0)
  8. If you search for *Squeeze Momentum Indicator [LazyBear]* in the indicator list, you can find the indicator.
  9. [![enter image description here][1]][1]
  10. [1]: https://i.stack.imgur.com/FVVk8.png
  11. </details>

huangapple
  • 本文由 发表于 2023年6月12日 17:21:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/76455215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定