错误:出现了” ‘if’ 不能用作变量或函数名”。我不明白为什么。

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

Error: "'if' cannot be used as a variable or function name" appeared. I cant undarstand why

问题

以下是您提供的Pine脚本的翻译部分:

这是我在Pine中编写的一些脚本。它无法编译,因为出现了“'if'不能用作变量或函数名”的错误。

//@version=5
strategy
rsi = ta.rsi(price, ShortLength)
vrsi1 = ta.rsi(price, LongLength)
co = ta.crossover(vrsi, overSold)
co1 = vrsi1-vrsi<=betweenbuy
co2 = vrsi1<vrsi1BottomBorder
barsSinceLastEntry=1
barsSinceLastEntry := strategy.opentrades > 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na
cu = ta.crossunder(vrsi, overBought)
cu1 = vrsi-vrsi1<=betweensell
cu2 = vrsi-vrsi1<=betweensell+(betweensell*barsSinceLastEntry/barsbetween)
cu3 = vrsi1>vrsi1TopBorder
rco = ta.crossunder (vrsi, overSold)
rco1 = ta.crossunder (vrsi1, vrsi1BottomBorder)

rcu = ta.crossover (vrsi, overBought)
rcu1 = ta.crossunder(vrsi1, vrsi1TopBorder)

//创建买入/卖出逻辑
if (not na(vrsi))
    if (co and co1 and co2 and inTradeWindow)
        strategy.entry("Buy", strategy.long, comment="Buy")
     if (rco and rco1 and inTradeWindow)
          strategy.entry("Buy1", strategy.long, comment="Buy1")
    if (barsSinceLastEntry<barsbetween)
        if (inTradeWindow and cu and cu1 and cu3)
            strategy.exit("exit", "Buy", profit = profitnumber, loss = lossnumber)
    else if (barsSinceLastEntry>=barsbetween)
        if (inTradeWindow and cu and cu2 and cu3)
            strategy.exit("exit", "Buy", profit = profitnumber, loss = lossnumber)     
     if (rcu and rcu1 and inTradeWindow)
          strategy.exit("exit", "Buy1", profit = profitnumber, loss = lossnumber)

但是,这段代码似乎存在一些问题,导致无法编译。可能的问题包括变量未定义、未定义的函数或其他错误。为了修复问题,您需要仔细检查代码并确保所有变量和函数都已正确定义,并且没有语法错误。

此外,您还提供了两个已注释的代码块,这两个代码块中的逻辑似乎与未注释的代码块类似。如果您想切换注释/非注释代码块,您需要确保切换时注释代码块内的所有变量和函数引用都是有效的。

请仔细检查代码以解决问题,或提供更多关于错误消息的信息,以便我可以提供更具体的帮助。

英文:

Here's some script i did in Pine. It doesn't compile because of "'if' cannot be used as a variable or function name."


//@version=5
strategy
rsi = ta.rsi(price, ShortLength)
vrsi1 = ta.rsi(price, LongLength)
co = ta.crossover(vrsi, overSold)
co1 = vrsi1-vrsi&lt;=betweenbuy
co2 = vrsi1&lt;vrsi1BottomBorder
barsSinceLastEntry=1
barsSinceLastEntry := strategy.opentrades &gt; 0 ? bar_index - strategy.opentrades.entry_bar_index(strategy.opentrades - 1) : na
cu = ta.crossunder(vrsi, overBought)
cu1 = vrsi-vrsi1&lt;=betweensell
cu2 = vrsi-vrsi1&lt;=betweensell+(betweensell*barsSinceLastEntry/barsbetween)
cu3 = vrsi1&gt;vrsi1TopBorder
rco = ta.crossunder (vrsi, overSold)
rco1 = ta.crossunder (vrsi1, vrsi1BottomBorder)

rcu = ta.crossover (vrsi, overBought)
rcu1 = ta.crossunder(vrsi1, vrsi1TopBorder)

//Creating buy/sell logic
if (not na(vrsi))
	if (co and co1 and co2 and inTradeWindow)
		strategy.entry(&quot;Buy&quot;, strategy.long, comment=&quot;Buy&quot;)
     if (rco and rco1 and inTradeWindow)
          strategy.entry(&quot;Buy1&quot;, strategy.long, comment=&quot;Buy1&quot;)
	if (barsSinceLastEntry&lt;barsbetween)
		if (inTradeWindow and cu and cu1 and cu3)
			strategy.exit(&quot;exit&quot;, &quot;Buy&quot;,profit = profitnumber, loss = lossnumber)
	else if (barsSinceLastEntry&gt;=barsbetween)
		if (inTradeWindow and cu and cu2 and cu3)
			strategy.exit(&quot;exit&quot;, &quot;Buy&quot;,profit = profitnumber, loss = lossnumber)     
     if (rcu and rcu1 and inTradeWindow)
          strategy.exit(&quot;exit&quot;, &quot;Buy1&quot;,profit = profitnumber, loss = lossnumber)   

But it compiles perfectly with two commented blocks (As it was before my last changes).


/Creating buy/sell logic
if (not na(vrsi))
	if (co and co1 and co2 and inTradeWindow)
		strategy.entry(&quot;Buy&quot;, strategy.long, comment=&quot;Buy&quot;)
//     if (rco and rco1 and inTradeWindow)
//          strategy.entry(&quot;Buy1&quot;, strategy.long, comment=&quot;Buy1&quot;)
	if (barsSinceLastEntry&lt;barsbetween)
		if (inTradeWindow and cu and cu1 and cu3)
			strategy.exit(&quot;exit&quot;, &quot;Buy&quot;,profit = profitnumber, loss = lossnumber)
	else if (barsSinceLastEntry&gt;=barsbetween)
		if (inTradeWindow and cu and cu2 and cu3)
			strategy.exit(&quot;exit&quot;, &quot;Buy&quot;,profit = profitnumber, loss = lossnumber)     
//     if (rcu and rcu1 and inTradeWindow)
//          strategy.exit(&quot;exit&quot;, &quot;Buy1&quot;,profit = profitnumber, loss = lossnumber)   

Also i tried to compile it in reverse (like switch commented/non-commented). aaaand... it does not.

Please help. What's wrong here? And how to fix it?
When I started that it seemed to be super simple: a few variable and two logical blocks THE SAME DAMN STRUCTURE as blocks I used before, but this error ruins everything((

答案1

得分: 0

您的缩进有问题。您需要将缩进设置为4个空格,有些地方是5个。

我无法确定您的脚本是否能够编译,因为您提供的代码中存在未声明的标识符。无论如何,在这个部分,这是您的代码,具有正确的缩进:

if (not na(vrsi))
    if (co and co1 and co2 and inTradeWindow)
        strategy.entry("Buy", strategy.long, comment="Buy")
    if (rco and rco1 and inTradeWindow)
        strategy.entry("Buy1", strategy.long, comment="Buy1")
    if (barsSinceLastEntry < barsbetween)
        if (inTradeWindow and cu and cu1 and cu3)
            strategy.exit("exit", "Buy", profit=profitnumber, loss=lossnumber)
    else if (barsSinceLastEntry >= barsbetween)
        if (inTradeWindow and cu and cu2 and cu3)
            strategy.exit("exit", "Buy", profit=profitnumber, loss=lossnumber)
    if (rcu and rcu1 and inTradeWindow)
        strategy.exit("exit", "Buy1", profit=profitnumber, loss=lossnumber)

希望这对您有所帮助。

英文:

Your spacing is off. You need your indents to be 4 spaces, you had some random ones at 5.

I can't tell if your script will compile though because you have undeclared identifiers when I use the code you provided. Anyways here's your code with the correct spacing on this section.

if (not na(vrsi))
    if (co and co1 and co2 and inTradeWindow)
        strategy.entry(&quot;Buy&quot;, strategy.long, comment=&quot;Buy&quot;)
    if (rco and rco1 and inTradeWindow)
        strategy.entry(&quot;Buy1&quot;, strategy.long, comment=&quot;Buy1&quot;)
    if (barsSinceLastEntry&lt;barsbetween)
        if (inTradeWindow and cu and cu1 and cu3)
            strategy.exit(&quot;exit&quot;, &quot;Buy&quot;,profit = profitnumber, loss = lossnumber)
    else if (barsSinceLastEntry&gt;=barsbetween)
        if (inTradeWindow and cu and cu2 and cu3)
            strategy.exit(&quot;exit&quot;, &quot;Buy&quot;,profit = profitnumber, loss = lossnumber)     
    if (rcu and rcu1 and inTradeWindow)
        strategy.exit(&quot;exit&quot;, &quot;Buy1&quot;,profit = profitnumber, loss = lossnumber)  

huangapple
  • 本文由 发表于 2023年6月19日 03:42:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502269.html
匿名

发表评论

匿名网友

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

确定