Quick Answer
CS1002: ; expected and CS1513: } expected are syntax errors. The compiler cannot read the structure of your code, because a semicolon, a closing brace or a parenthesis is missing or in the wrong place. They are the most common errors when you write a cBot by hand or convert one from MQL5 or Pine Script, and they are also the easiest to fix once you know two rules.
Rule 1: the error is often reported on the line after the real mistake, because the compiler only notices the problem when it reads the next token. Rule 2: fix the first error in the list, then build again. One missing brace can create ten errors below it, and most of them disappear when the first one is fixed.
From the author: I build cBots in cTrader Automate and convert strategies from Pine Script and MQL5 to C#. Syntax errors in converted code come from habits of the other language: MQL5 allows some shortcuts that C# does not, and Pine Script has no semicolons or braces at all. This guide shows the usual cases and how to find the real position of the problem.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| CS1002: ; expected | A statement does not end with ; | Add the ;, look at the line above the reported one |
| CS1513: } expected | A block is not closed | Count the { and }, add the missing } |
| CS1514: { expected | A block is missing its opening brace | Add the { after the method or if line |
| CS1026: ) expected | A parenthesis is not closed | Count the ( and ) in the line |
| CS1003: Syntax error, ',' expected | A missing comma or a stray character in an argument list | Check the commas between arguments |
| CS1001: Identifier expected | A name is missing, or a reserved word is used as a name | Give it a valid name |
| Many errors at once | One early mistake | Fix the first one and build again |
Why the Line Number Is Often Wrong
C# does not care about line breaks. A statement ends at the semicolon, and not at the end of the line. When you forget a semicolon, the compiler keeps reading and only finds out that something is wrong at the first token of the next line. That is why CS1002 is usually reported on the line after the real mistake. If the reported line looks correct, look at the line above it.
A missing closing brace is worse: the compiler reads to the end of the file and then reports CS1513 at the last line. The mistake can be anywhere above. The editor helps here: put the cursor next to a brace and look at which brace is highlighted as its pair.
The Broken cBot
This cBot prints a value from the last bar. It does not place trades. It has three syntax mistakes:
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BrokenBot : Robot
{
[Parameter("Period", DefaultValue = 14, MinValue = 1)]
public int Period { get; set; }
protected override void OnBar()
{
double lastClose = Bars.ClosePrices.Last(1) // missing ;
double lastOpen = Bars.OpenPrices.Last(1);
if (lastClose > lastOpen
{ // missing )
Print("Up bar, close {0}", lastClose);
}
} // OnBar is closed, but see below
private double Average(int period)
{
double sum = 0;
for (int i = 1; i <= period; i++)
{
sum += Bars.ClosePrices.Last(i);
// missing } for the for loop
return sum / period;
}
}
}
The compiler reports the first error on the line double lastOpen = ... (after the missing semicolon), the second near the { after the if, and a } error at the end of the file. Fix them from the top.
Cause 1: A Missing Semicolon
Every statement in C# ends with ;: declarations, assignments, method calls and return. The blocks (if, for, while, methods, classes) do not:
double lastClose = Bars.ClosePrices.Last(1); // a statement: ends with ;
if (lastClose > 0) // not a statement: no ;
{
Print("ok"); // a statement: ends with ;
}
A semicolon directly after the if (...) is also a mistake of a different kind: if (x > 0); means "if x is positive, do nothing", and the block that follows always runs. The compiler may only give a warning for it, so look for it when a condition seems to be ignored.
Cause 2: A Missing or Extra Brace
Count the braces of the method. Each { needs its }, and the structure of a cBot has at least three levels: the namespace, the class and the method. A missing brace is easiest to see when the code is indented in the same way everywhere. Use the auto-format of the editor and look at where the indentation stops matching.
A common source is a block that was copied from another cBot, or an if that was changed to remove a condition and lost its brace. After such an edit, read the code from the top to the end of the method.
Cause 3: A Missing Parenthesis
CS1026 appears when a ( is not closed. Count them in long conditions. A good habit is to split a long condition into named parts:
bool isUp = lastClose > lastOpen;
bool aboveAverage = lastClose > average;
if (isUp && aboveAverage)
{
Print("Signal");
}
Short lines are easier to check, and they give you names to print when you test the cBot.
Cause 4: Habits From MQL5 and Pine Script
Some things that are fine in MQL5 or Pine Script are syntax errors in C#:
| Other language | In C# |
|---|---|
| MQL5 input double Lots = 0.1; | [Parameter("Lots", DefaultValue = 0.1)] public double Lots { get; set; } |
| MQL5 #property strict | No equivalent: remove it |
| MQL5 double values[]; | double[] values = new double[10]; or a List<double> |
| Pine Script if x followed by an indented block, no braces | Braces { } are required for a block of several lines |
| Pine Script x := 5 | x = 5; |
| Pine Script => function | A method with a return type and return |
A converted cBot that has many errors at the top usually has one of these patterns repeated many times. Fix one place, and search the file for the same pattern.
Cause 5: Something Is Written in the Wrong Place
Code must be inside a method. A statement directly in the class body, outside any method, gives a syntax error:
public class MyBot : Robot
{
Print("Hello"); // wrong: a statement in the class body
protected override void OnStart()
{
Print("Hello"); // right: inside a method
}
}
Also check that every method is inside the class, and that the class is inside the namespace. A closing brace in the wrong place can end the class early, and then the next method is "outside" of it.
The Fixed cBot
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FixedBot : Robot
{
[Parameter("Period", DefaultValue = 14, MinValue = 1)]
public int Period { get; set; }
protected override void OnBar()
{
if (Bars.Count <= Period + 2)
return;
double lastClose = Bars.ClosePrices.Last(1);
double lastOpen = Bars.OpenPrices.Last(1);
if (lastClose > lastOpen)
{
Print("Up bar, close {0}, average {1}", lastClose, Average(Period));
}
}
private double Average(int period)
{
double sum = 0;
for (int i = 1; i <= period; i++)
{
sum += Bars.ClosePrices.Last(i);
}
return sum / period;
}
}
}
The bot only prints, so you can compare the values with the chart. The Bars.Count check at the top avoids the runtime error described in cBot index out of range.
Similar Errors
| Error | Message | What it means | |---|---|---| | CS1002 | ; expected | A statement is not finished | | CS1513 | } expected | A block is not closed | | CS1514 | { expected | A block does not start | | CS1026 | ) expected | A parenthesis is not closed | | CS0103 | The name 'x' does not exist in the current context | A name is unknown | | CS0161 | Not all code paths return a value | A method does not return on every path |
Checklist: What to Check When You See CS1002 or CS1513
- Fix the first error in the list, then build again.
- Look at the line above the reported line for a missing
;. - Check the braces: auto-format the code and look at the indentation.
- Check the parentheses of long conditions, and split them into named parts.
- Look for MQL5 or Pine habits (
input,#property,:=, blocks without braces). - Check that all code is inside a method, inside the class, inside the namespace.
- Search the file for the same pattern after you fix one place.
- Test on a demo account before the cBot trades with real money.
Common Mistakes
- Fixing the last error first instead of the first.
- Trusting the line number when the mistake is on the line above.
- Forgetting
;after a declaration. - Pasting a block without checking its braces.
- Writing MQL5
inputlines in a C# class. - Putting a statement in the class body.
FAQ
Related Guides
- cTrader C# error CS0103: the name does not exist in the current context.
- cTrader C# error CS1061: the object is found, the member is not.
- cTrader C# error CS0161: a method that does not return on every path.
- cTrader C# error CS0136: a name that is already used in the method.
- cTrader C# error CS0246: a type name the compiler does not know.
- cTrader cBot crash: index was out of range: a runtime error after the cBot builds.
- Pine Script to cTrader C# cBot: the full conversion process.
- Pine Script end-of-line error: the matching syntax error in Pine Script.
- What to check in a trading bot before going live: a pre-live checklist for stops, lot size, spread and trade results.
- cTrader C# error CS0019: an operator used between two types that do not work with it.
- cTrader C# error CS0120: a member that needs a cBot object is used without one.
- cTrader C# error CS0165: a variable is read before it has a value.
Fix Compile Errors Without Guessing
If a cBot or an Expert Advisor does not build, paste the code and the error message into the CodeFlowOS Error Fixer. It suggests a minimal fix, explains each error and shows what changed. A fix uses 1 credit, with up to 3 attempts included, and the compiler check is optional. Outputs are not guaranteed, and code that compiles can still have wrong trading logic, so always review the result. Before a bot goes live, the Code Review is free during the early-bird period. CodeFlowOS also converts strategies between Pine Script, MQL5 and cTrader C#: try the converter page.
This article is educational. It does not describe trading performance and is not financial advice. Always build the code in cTrader Automate and backtest it before using it on a live account.