Search code examples
pine-scriptpine-script-v5tradingview-api

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


Here's some scripting I did in PineScript. 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<=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)

//Creating buy/sell logic
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)   

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("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)   

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

What's wrong here? And how to fix it?

When I started, it seemed to be super simple: a few variables and two logical blocks, the same structure as blocks I used before.


Solution

  • 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("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)