Quick Answer
CS0182: An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type appears on a line like [Parameter("Period", DefaultValue = SomeVariable)]. The values you write inside an attribute, such as DefaultValue, MinValue and MaxValue of a cBot [Parameter], are read while the code is compiled, before any cBot runs. So they must be constants: a number, a string, true or false, an enum value, or a const field. A normal variable, a static readonly field, a method call (Math.Max(...)) or a property of the platform (Symbol.PipSize) is not known at compile time.
The fix: write the number directly, use a const field, or leave the default simple and calculate the real value in OnStart. The sections below show each case.
From the author: I build cBots in cTrader Automate and convert strategies from Pine Script and MQL5 to C#. CS0182 is common in converted code because an MQL5
inputline can use a value from a#defineor a calculation, and a Pineinput.int(defval = ...)can be built from other values. In C# the parameter line is an attribute, and attributes are strict about what they accept.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| DefaultValue = DefaultPeriod | A static readonly or normal field | Make it const, or write the number |
| DefaultValue = Math.Max(10, 14) | A method call | Write the result: 14 |
| MaxValue = Period * 2 | A calculation with a non-constant | Use a const, or check the limit in OnStart |
| DefaultValue = 0.1m | A decimal literal | Use double: 0.1 |
| DefaultValue = Symbol.PipSize | A platform value | Calculate it in OnStart |
| DefaultValue = DateTime.Now | A value that changes | Not possible: use a fixed default |
What the Error Means
C# attributes are stored in the compiled file together with their arguments. The compiler writes the values into the file, so it must know them at compile time. That is why only a few kinds of values are allowed in an attribute: constants of simple types (numbers, bool, string, char), enum values, typeof(...) expressions and arrays of those. Some types cannot be used as attribute arguments at all; the C# documentation names decimal among them. Anything that needs the program to run to get a value is rejected with CS0182.
The Broken cBot
This cBot only prints. It has no order calls. Each marked line has a CS0182 error:
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BrokenBot : Robot
{
private static readonly int DefaultPeriod = 14;
[Parameter("Period", DefaultValue = DefaultPeriod, MinValue = 1)] // CS0182: static readonly is not a constant
public int Period { get; set; }
[Parameter("Fast", DefaultValue = Math.Max(10, 12), MinValue = 1)] // CS0182: a method call
public int Fast { get; set; }
[Parameter("Slow", DefaultValue = int.Parse("26"), MinValue = 1)] // CS0182: a method call
public int Slow { get; set; }
protected override void OnStart()
{
Print("Period {0}, fast {1}, slow {2}", Period, Fast, Slow);
}
}
}
The compiler reports the message at each argument. Check the exact text in your build.
Cause 1: A Field That Is Not const
A static readonly field looks constant, but its value is set when the program runs, so the compiler cannot use it in an attribute. A const field is replaced by its value at compile time and is allowed:
private const int DefaultPeriod = 14;
[Parameter("Period", DefaultValue = DefaultPeriod, MinValue = 1)]
public int Period { get; set; }
A const can be a number, a string, a bool or an enum value. It can also be a calculation of other constants (private const int MaxPeriod = DefaultPeriod * 5;).
Cause 2: A Method Call or a Calculation
Math.Max, int.Parse, Convert.ToInt32, DateTime.Now and similar calls are evaluated at run time. Write the result as a number, or a const that holds it:
[Parameter("Fast", DefaultValue = 12, MinValue = 1)]
public int Fast { get; set; }
Arithmetic with literals (DefaultValue = 2 * 7) and with const fields is fine, because the compiler can calculate it.
Cause 3: A Value From the Platform
Symbol, Account, Bars and the other platform objects exist only when the cBot runs, so they cannot appear in a parameter line. If you want "a stop of five pips" or "risk based on the symbol", keep the parameter simple (a number of pips, a percentage) and do the calculation in OnStart or in the handler:
[Parameter("Stop (pips)", DefaultValue = 20, MinValue = 1)]
public int StopPips { get; set; }
private double _stopDistance;
protected override void OnStart()
{
_stopDistance = StopPips * Symbol.PipSize;
}
The same applies to a limit that depends on another parameter. MaxValue cannot read Period. Check the relation in OnStart and stop the cBot with a clear message if it is wrong:
if (Fast >= Slow)
{
Print("Fast must be smaller than slow");
Stop();
}
(Stop() is a method of the Robot class that stops the cBot; check it in the reference for your version.)
Cause 4: A Type That Is Not Allowed
A decimal literal such as 0.1m cannot be an attribute argument, and prices in cTrader are double anyway:
[Parameter("Lots", DefaultValue = 0.1, MinValue = 0.01, Step = 0.01)]
public double Lots { get; set; }
An int literal for a double parameter also works: DefaultValue = 1 is converted. Do not use 1.5f or 1.5m unless you checked that the type of the parameter accepts it. Plain double and int are the safe choices.
The Fixed cBot
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FixedBot : Robot
{
private const int DefaultPeriod = 14;
[Parameter("Period", DefaultValue = DefaultPeriod, MinValue = 1)]
public int Period { get; set; }
[Parameter("Fast", DefaultValue = 12, MinValue = 1)]
public int Fast { get; set; }
[Parameter("Slow", DefaultValue = 26, MinValue = 1)]
public int Slow { get; set; }
protected override void OnStart()
{
if (Fast >= Slow)
Print("Warning: fast ({0}) should be smaller than slow ({1})", Fast, Slow);
Print("Period {0}, fast {1}, slow {2}", Period, Fast, Slow);
}
}
}
The bot only prints. Change the parameters in the cBot settings and check that the log shows your values. Test on a demo account before you add orders.
Similar Errors
| Error | Message | What it means | |---|---|---| | CS0182 | An attribute argument must be a constant expression | A value that is not known at compile time | | CS0120 | An object reference is required for the non-static member | An instance member used without an object | | CS0103 | The name 'x' does not exist in the current context | A name is unknown | | CS0029 | Cannot implicitly convert type | A value of the wrong type | | CS1002 | ; expected | A statement is not finished |
Checklist: What to Check When You See CS0182
- Look at the attribute line (
[Parameter(...)]) in the message. - Find the argument that is not a plain number, string,
boolor enum value. - Use
constfor a named default, notstatic readonly. - Write the result instead of a method call.
- Move platform values (
Symbol,Account) intoOnStart. - Check relations between parameters in
OnStart, not inMinValueorMaxValue. - Use
doubleandintand notdecimal. - Build again, and check the values in the log. Test on a demo account.
Common Mistakes
- Using
static readonlyinstead ofconst. - Calling
Mathmethods in a parameter line. - Reading
SymbolorAccountin an attribute. - Making
MaxValuedepend on another parameter. - Using
decimalfor prices. - Hiding a wrong relation between parameters instead of checking it at start.
FAQ
Related Guides
- cTrader C# error CS0120: a member that needs a cBot object is used without one.
- cTrader C# error CS0103: the name does not exist in the current context.
- cTrader C# error CS0029: a value of the wrong type.
- cTrader C# errors CS1002 and CS1513: a missing semicolon, brace or parenthesis.
- cTrader C# error CS0246: a type name the compiler does not know.
- Pine Script input.* in MQL5: inputs, groups and dropdowns on the MetaTrader side.
- Pine Script to cTrader C# cBot: the full conversion process.
- 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 CS0200: a read-only property such as
Position.StopLossthat is assigned 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.