Trading View Alert Saving Failed Please Try Again

Nearly all TradingView scripts use custom, user-defined variables. A decent portion even uses custom functions. Those variables and functions prevent code repetition and make code easier to manage. But not so when they trigger the 'undeclared identifier' error. Let's fix those errors!

IN THIS ARTICLE:

# Exploring TradingView's 'undeclared identifier' error

TradingView Pine has a lot of built-in variables that return all kinds of information. And there are also plenty of functions that add all kinds of features to our indicator and strategy scripts. But we can also make our own variables and functions. That way we don't have to perform the same calculations over and over again.

But while custom variables and functions often make our TradingView programming easier, at times they can also give a small headache. That latter for instance happens when we run into the 'undeclared identifier' error. When this error happens and our script already runs on the chart, we see 'cannot compile script' appear there:

Example of the 'cannot compile script' TradingView error

This doesn't help much since plenty of TradingView errors use that same message. But for the actual error information we'll have to look in the Pine Editor's console window.

There something like the following shows:

          Processing script... line 5: Undeclared identifier `emavalue`; line 7: Undeclared identifier `plotColour` Script 'Error example' has been saved        

This has a lot more actionable information. Let's see how we use this error information to fix the code problem.

# Fixing TradingView's 'undeclared identifier' error

While the 'undeclared identifier' error message can be a bit annoying, the cause is often just a small mistake in our code. That makes fixing this error rather straightforward:

  1. Read the first line of the error message carefully. Note the line number as well as the incorrect variable or function name.
  2. Go to that line in the Pine Editor and locate the variable or function the error mentions.
  3. Look at the lines above the error line to see how you named that variable or function. Use that exact name to fix the error.
  4. Save the script. Repeat the steps if another 'undeclared identifier' error shows.

An identifier is the name we give a custom variable or function (TradingView Wiki, 2017). The identifier is whichever name appears to the left of = or => when we make our variable or function. That name can consists out of upper- and lowercase letters, an underscore (_), and numbers (although an identifier can't start with a number).

The 'undeclared identifier' TradingView error can create an avalanche of error messages. But don't feel intimidated by all that red in Pine Editor's console window.

It's just that when we make a mistake in one identifier, TradingView also triggers error for code that comes after it. So there might be 10 errors while in fact we've only made one mistake. So don't worry if you see multiple errors. Just fix the first one and then save the script to see if the others still show.

Now let's look at a few situations that trigger the 'undeclared identifier' error.

# Error example: incorrectly type a variable name

Likely the most common situation that gets us the 'undeclared identifier' TradingView error is when make a typing error with a variable name. This is also a bit annoying since we know what the variable name should be, it's just that we typed it incorrectly.

An example indicator with that mistake is:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      true)  emaValue                      =                      ema(close                      ,                      25) plotColour                      =                      (close                      >                      emaVlue)                      ?                      green                      :                      red                      plot(series=emaValue,                      color=plotColour,                      linewidth=                      2)                  
          Processing script... line 5: Undeclared identifier `emaVlue`; line 7: Undeclared identifier `plotColour` Script 'Error example' has been saved        

Here we got the 'undeclared identifier' error for the emaVlue variable name. That should of course be emaValue, the variable we made in line 4.

And so to fix the error we replace emaVlue with emaValue in the 5th line:

            plotColour              =              (close              >              emaValue)              ?              green              :              red                      

# Error example: incorrect capitalisation with a variable name

The name that we give our custom variable or function is case sensitive (TradingView Wiki, 2017). And so we need to refer to custom variables or functions with the exact same name as we used to create them.

But a capitalisation error is easily made. For instance:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      true)  emaValue                      =                      ema(close                      ,                      25) plotColour                      =                      (close                      >                      emavalue)                      ?                      green                      :                      red                      plot(series=emaValue,                      color=plotColour,                      linewidth=                      2)                  
          Processing script... line 5: Undeclared identifier `emavalue`; line 7: Undeclared identifier `plotColour` Script 'Error example' has been saved        

Here the script errors because we used the emavalue identifier in line 5. Since we gave that variable the name of emaValue when we made it (line 4), we should also refer to it with the proper capitalisation.

And so to fix the 'undeclared identifier' error here we change the fifth line to:

            plotColour              =              (close              >              emaValue)              ?              green              :              red                      

# Error example: executing a custom function without parentheses

When we make a TradingView function, we first declare its name, then list any parameters between parentheses (( and )), and end with the function's code. If our custom function doesn't use arguments, then that's fine too. But to correctly refer to the function we do need to use parentheses after its name. When we don't, we run into the 'undeclared identifier' TradingView error again.

Says we got an emaCustom() function but try to execute it as emaCustom:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                            8                                            9                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      false)  emaCustom()                      =                      >                      quickEMA                      =                      ema(close                      ,                      10)     slowEMA                      =                      ema(close                      ,                      40)                      ema(quickEMA                      -                      slowEMA,                      15)                      plot(series=emaCustom,                      color=                      orange                      ,                      linewidth=                      2)                  
          Processing script... line 9: Undeclared identifier `emaCustom` Script 'Error example' has been saved        

This example code runs into the 'undeclared identifier' error since we refer to the emaCustom() function as emaCustom. But without those missing parentheses (()) behind the function's name, TradingView thinks we mean to use a variable. And the example indicator above doesn't have an emaCustom variable.

To fix the problem we change line 9 to execute the function as emaCustom():

                          plot(series=emaCustom(),              color=              orange              ,              linewidth=              2)                      

# Error example: updating a variable before declaring it

There are two ways we can set our custom variable in TradingView. With = we make our variable and give it some value. Then if we later want to update its value, we use :=. But it has to be in that order – we first create a variable (with =), then we can update it (:=).

When we accidentally get the = and := order backwards, our code triggers the 'undeclared identifier' error message. For example:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                            8                                            9                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      false)  movAvg                      :                      =                      sma(close                      ,                      20)                      plot(series=movAvg,                      color=                      orange                      ,                      linewidth=                      2)  movAvg                      =                      ema(close                      ,                      10)                      plot(series=movAvg,                      color=                      teal                      ,                      linewidth=                      2)                  
          Processing script... line 4: Undeclared identifier `movAvg`; line 6: Undeclared identifier `movAvg` Script 'Error example' has been saved        

Here we first tried to update the movAvg variable (line 4). Then later we attempt to declare it (line 6). This got TradingView confused: the 'undeclared identifier' error shows it cannot find movAvg when we try to update its value.

To fix the error here, we'll have = and := swap places like so:

                          //@version=3              study(title=              "Error example"              ,              overlay=              false)  movAvg              =              sma(close              ,              20)              plot(series=movAvg,              color=              orange              ,              linewidth=              2)  movAvg              :              =              ema(close              ,              10)              plot(series=movAvg,              color=              teal              ,              linewidth=              2)                      

# Error example: using a self-referencing variable without :=

Things get more complex when we got the 'undeclared identifier' error with a self-referencing variable. When a variable references its own previous value (like profit := profit[1] + todayProfit), that variable is called a self-referencing variable.

We need to do two things before we can use a self-referencing variable. First we have to create that variable with the = operator and set it to some default value (profit = 1.0). But at that point we can't reference its own value yet! The second step is to update the variable to a new value with the := operator. At that point we can reference that variable's previous bar values (TradingView Wiki, 2018).

But if we only use = with a self-referencing variable, we'll run into the 'undeclared identifier' TradingView error. For example:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      false)  barNumber                      =                      nz(barNumber[                      1                      ])                      +                      1                      plot(series=barNumber,                      color=                      orange                      ,                      linewidth=                      2)                      plot(series=                      n                      ,                      color=                      purple                      ,                      linewidth=                      2)                  
          Processing script... line 4: Undeclared identifier `barNumber`; line 6: Undeclared identifier `barNumber` Script 'Error example' has been saved        

The problem here is that we try to do two things at once: create the barNumber variable but also update its value to include its previous bar value.

Instead we should first make barNumber with =. Only then can we update its value to include the previous bar value of barNumber. That means we fix the 'undeclared identifier' error when we change the code to:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                            8                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      false)  barNumber                      =                      1                      barNumber                      :                      =                      nz(barNumber[                      1                      ])                      +                      1                      plot(series=barNumber,                      color=                      orange                      ,                      linewidth=                      2)                      plot(series=                      n                      ,                      color=                      purple                      ,                      linewidth=                      2)                  

# Error example: using a forward-referencing variable without :=

Another abstract situation that gets us the 'undeclared identifier' error message is with a forward-referencing variable. When we references a variable before it gets updated to a value, that variable is a forward-referencing variable.

There are two steps to make a forward-referencing variable work. First we create that variable with = and set it to some default value (stopPrice = 0.0). That code has to come before any references to that variable. Then we update its value with := to the value we actually intended it to hold (stopPrice := emaValue - 0.0020).

If we overlook a step and forward reference a variable before it got made with =, we'll end up with the 'undeclared identifier' error. For instance:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      false)  plusOne                      =                      nz(currentValue[                      1                      ])                      +                      1                      currentValue                      =                      plusOne                      +                      close                      plot(series=currentValue,                      color=                      orange                      ,                      linewidth=                      2)                  
          Processing script... line 4: Undeclared identifier `currentValue`; line 5: Undeclared identifier `plusOne`; line 7: Undeclared identifier `currentValue` Script 'Error example' has been saved        

The problem here is that we update the plusOne variable to the previous bar value of currentValue. But that happens before we create the currentValue variable. As such TradingView doesn't understand what we're talking about, and errors.

To make the forward reference in line 4 work, we need to make the currentValue variable before that line. We do that with the = operator. We can then update currentValue later, for which we use :=.

And so to fix the 'undeclared identifier' error here the code becomes:

                                          1                                            2                                            3                                            4                                            5                                            6                                            7                                            8                                                            
                                          //@version=3                      study(title=                      "Error example"                      ,                      overlay=                      false)  currentValue                      =                      0.0                      plusOne                      =                      nz(currentValue[                      1                      ])                      +                      1                      currentValue                      :                      =                      plusOne                      +                      close                      plot(series=currentValue,                      color=                      orange                      ,                      linewidth=                      2)                  

# Summary

The 'undeclared identifier' error happens when TradingView cannot find the variable or function that we try to use. This happens when we mistype the name, including any capitalisation differences. The error also shows when we forget parentheses (( and )) with a function's name.

Those coding mistakes are luckily easy to find and fix. But the 'undeclared identifier' error also shows in more complex situations. Those happen when we make a mistake with TradingView's = and := operators. We can't for instance update a variable (:=) before we make it (=). If we don't follow that rule, we'll get the error.

We also run into 'undeclared identifier' if, when creating the variable, we set its value to some previous bar value. Or when we refer to a variable before our script has executed the = operator to make that variable. To fix the error in these two situations we first make the variable with =. Only then can we fetch its value with errors. And if we later want to update the variable, we'll use the := operator.

« All TradingView errors articles

whytehumened49.blogspot.com

Source: https://kodify.net/tradingview/errors/undeclared-identifier/

0 Response to "Trading View Alert Saving Failed Please Try Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel