英文:
Converting a linreg V1 Function to V5 (Tradingview - Pine Script)
问题
我尝试将pine v1 linreg函数转换为v3(然后自动转换为V5),但一直出现以下错误:
输入处的语法错误 'end of line without line continuation'
这是V1的代码:
val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)),
lengthKC,0)
所以我在pine脚本语言参考中查找了一下,函数应该是这样的:
linreg(source, length, offset) → series
如果我没弄错的话,source应该是这样的:
source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),
长度:
sma(close,lengthKC)),
和偏移:
lengthKC,0)
所以我在V3中写成了这样:
val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))
但它一直告诉我:
输入不匹配 ',' 期望 ')'
我尝试了一些其他解决方案,但无法解决这个问题。
编辑:
这是完整的代码:
study(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator [LazyBear]", overlay=false)
length = input(20, title="BB Length")
mult = input(2.0,title="BB MultFactor")
lengthKC=input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")
useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)
// Calculate BB
source = close
basis = sma(source, length)
dev = multKC * stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev
// Calculate KC
ma = sma(source, lengthKC)
range = useTrueRange ? tr : (high - low)
rangema = sma(range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC
sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz = (sqzOn == false) and (sqzOff == false)
val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))
bcolor = iff( val > 0,
iff( val > nz(val[1]), lime, green),
iff( val < nz(val[1]), red, maroon))
scolor = noSqz ? blue : sqzOn ? black : gray
plot(val, color=bcolor, style=histogram, linewidth=4)
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
val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)),
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:
study(shorttitle = "SQZMOM_LB", title="Squeeze Momentum Indicator [LazyBear]", overlay=false)
length = input(20, title="BB Length")
mult = input(2.0,title="BB MultFactor")
lengthKC=input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")
useTrueRange = input(true, title="Use TrueRange (KC)", type=bool)
// Calculate BB
source = close
basis = sma(source, length)
dev = multKC * stdev(source, length)
upperBB = basis + dev
lowerBB = basis - dev
// Calculate KC
ma = sma(source, lengthKC)
range = useTrueRange ? tr : (high - low)
rangema = sma(range, lengthKC)
upperKC = ma + rangema * multKC
lowerKC = ma - rangema * multKC
sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC)
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC)
noSqz = (sqzOn == false) and (sqzOff == false)
val = linreg((source - avg(highest(high, lengthKC)), avg(lowest(low, lengthKC))), (avg(sma(close,lengthKC))), (lengthKC,0))
bcolor = iff( val > 0,
iff( val > nz(val[1]), lime, green),
iff( val < nz(val[1]), red, maroon))
scolor = noSqz ? blue : sqzOn ? black : gray
plot(val, color=bcolor, style=histogram, linewidth=4)
plot(0, color=scolor, style=cross, linewidth=2)```
</details>
# 答案1
**得分**: 0
原始代码略有不同。
`val` 的定义如下:
val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)), sma(close, lengthKC)), lengthKC, 0)
如果您在指标列表中搜索 *Squeeze Momentum Indicator [LazyBear]*,您可以找到该指标。
[![enter image description here][1]][1]
<details>
<summary>英文:</summary>
The original code is slightly different.
The definition of `val` is as below:
val = linreg(source - avg(avg(highest(high, lengthKC), lowest(low, lengthKC)),sma(close,lengthKC)), lengthKC,0)
If you search for *Squeeze Momentum Indicator [LazyBear]* in the indicator list, you can find the indicator.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/FVVk8.png
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论