Why their is an error "unexpected identifier"

I just want to write a simple AFL, just start on 2 lines, error "unexpected identifier" happens

MyBuy1 = 280
MyShort1 = 300

No matter what names i change, there is still this error on "MyShort1", but MyBuy1 is fine, why and how can i fix it, thanks for answer.

Are you missing your semicolons at the end of each line?

oh, thanks, it's my careless

In my experience, these types of errors are the result of a missing semicolon or a missing double quotation mark (i.e. "). The problems are particularly difficult to eyeball when #include statement(s) or lengthy AFL formulas (i.e. programs) are involved. Furthermore, and in particular, i sometimes forget to end some statements inside of an if function with a semicolon (e.g. IF(condition){trueValue = 1;} (sometimes I mistakenly put the semicolon after the end of the block brace (which is not required).

Here are my tips to check when you received an Unexpected Identifier:

Understand AFL Identifiers:
Identifiers are arbitrary names of any length given to functions and variables. Identifiers can contain the letters (a-z, A-Z), the underscore character ("_"), and the digits (0-9). The first character must be a letter.
AFL identifiers are NOT case sensitive.

  1. Check that you are editing the correct formula (perhaps you are editing an old copy by mistake).
  2. Check for a missing semicolon someplace BEFORE the point where the error is displayed.
  3. Check for a missing opening or closing double quotation mark.
  4. Check that you saved newly edited code.
  5. Check for erroneously entered text (sometimes you may heavy fingered on the keyboard).
  6. Rerun the AFL Editor's Verify Syntax (as you may have corrected the issue and saved but the error message continues to display

The AMIBroker syntax checker is very good at finding errors in the code. Occasionally, the messages seem generic but I find most of the time they make perfect sense once you understand that the inline error message may not be the shown at the location where the programmer's error has occurred. When the word "Unexpected" is part of the message, I have found the issue often occurs in a previous ares of code.

Perhaps others have similar or better instructions for finding programming issues.

2 Likes

you are so nice, thank a lot, it is very helpful for newbie like me.

Dear all,
I also am a new convert to Amibroker. Coming from Metastock there are similarities but still a steep learning curve. One of my formulas in a previous incarnation had no errors in the plot line. Now I have ended up with an Error 31 Identifier error. I have checked the line and all my other lines, all are terminated correctly but I can't seem to remove the error. Error_Code%2031. I have attached a screen shot. If you would be so kind to show me my newbie error that would be fantastic.
Regards
Neil D

Apart from posting the screenshot, don't you think it is necessary to post the code as well ?
Why do you expect someone else to type it at their end?

By the way, look at the SetOption("EveryBarNullCheck… Line and see the close double quote missing.

Thank you Travick,
Your observation skills are far greater than mine and you have solved the issue.
I think what fooled me was that the error appeared, not on the line where the error was but several lines below.
I will get better. Thank you for taking the time to answer.
Regards

Neil

Missing semicolons, missing closing braces, missing quotation marks may all lead to error messages that appear many lines later because parser would try hard to find balancing (missing) part before producing an error.

The manual clearly explains what is going on
http://www.amibroker.com/guide/errors/31.html

The syntax is incorrect because the parser expects specific tokens and finds something else.

Typically it occurs when something is missing BEFORE the error is produced.

In your case it found " character (opening double quote for "Exitstop") and treated it as CLOSING double quotation mark that was missing in string that you began in line 32. Subsequent letters Exitstop were then unexpected identifier that it complained about.

1 Like

It makes sense when you explain it. Practice will help, I have only been at it for around a month so I have plenty of learning ahead of me.
Such a step up from Metastock and so much more flexibility and the time it has saved from manual calculations and excel spreadsheets.
Regards
Neil