Quick Answer
CS0246: The type or namespace name 'X' could not be found (are you missing a using directive or an assembly reference?) means the compiler reached a type name (a class, struct or namespace) and does not know it. The usual causes in a cBot are a missing using line at the top of the file, a type name that is spelled wrong or does not exist, and a library that is used in the code but was never added as a reference.
The fastest check: read the name in the quotes, then ask two questions. Is there a using line for the namespace that contains it? Is the name spelled exactly as the real type, with the same capital letters? If both are yes, the type probably comes from a library that must be added to the cBot as a reference.
CS0246 is about type names. If the compiler cannot find a variable or a method name, the error is CS0103, which is explained in cTrader C# error CS0103. If the type is found but the member after the dot is not, it is CS1061: cTrader C# error CS1061.
From the author: I build cBots and indicators in cTrader Automate, and I convert strategies from Pine Script and MQL5 to C#. CS0246 shows up when code is pasted from another project, from an online example or from a converter: the code is fine, but the
usinglines stayed behind. This guide lists the causes in the order I would check them.
Key Facts
| What you see | Usual cause | Fix |
|---|---|---|
| 'ExponentialMovingAverage' could not be found | using cAlgo.API.Indicators; is missing | Add the using line |
| 'List<>' could not be found | using System.Collections.Generic; is missing | Add the using line |
| 'DateTime' could not be found | using System; is missing | Add the using line |
| 'RobotAttribute' could not be found | using cAlgo.API; is missing | Add the using line |
| 'ExponentialMovingAvarage' could not be found | The type name has a typo | Use the real name, with the right case |
| 'Newtonsoft' could not be found | A library is used but not referenced | Add the reference to the cBot |
What the Compiler Is Telling You
CS0246: The type or namespace name 'List<>' could not be found
(are you missing a using directive or an assembly reference?)
- The name in quotes is the type the compiler could not find. For a generic type it may show as
List<>. - The hint in brackets lists the two usual reasons: a missing
usingdirective or a missing assembly reference. - The line points to the place where the type name is used, not to the top of the file where the fix usually goes.
The Broken cBot
This cBot keeps a list of closes and prints the time of each new bar. It does not place trades. The using lines for the list and for the date are missing, and one type name has a typo:
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BrokenBot : Robot
{
[Parameter("EMA period", DefaultValue = 20, MinValue = 1)]
public int EmaPeriod { get; set; }
private ExponentialMovingAverage _ema; // CS0246
private List<double> _closes = new List<double>(); // CS0246
protected override void OnStart()
{
_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, EmaPeriod);
}
protected override void OnBar()
{
DateTime barTime = Bars.OpenTimes.Last(1); // CS0246
_closes.Add(Bars.ClosePrices.Last(1));
Print("Bar {0}, EMA {1}, closes stored {2}", barTime, _ema.Result.Last(1), _closes.Count);
}
}
}
The file has three problems. ExponentialMovingAverage lives in cAlgo.API.Indicators, List<T> in System.Collections.Generic, and DateTime in System. None of the three namespaces is imported.
Cause 1: A Missing using Line
A using line does not load code, it only lets you write the short name. Without it, the compiler looks for ExponentialMovingAverage in the namespaces you imported, does not find it, and reports CS0246. The cTrader Automate templates add the common lines, so this usually appears after you delete lines, paste code from another file, or copy a class between two projects.
The lines you will need most often:
using System; // DateTime, Math, TimeSpan
using System.Linq; // Where, Any, Sum on collections
using System.Collections.Generic; // List<T>, Dictionary<K,V>
using cAlgo.API; // Robot, Indicator, Parameter, TradeType
using cAlgo.API.Indicators; // ExponentialMovingAverage, RelativeStrengthIndex
If you do not know which namespace a type is in, look at where the example or the documentation page of that type puts it. In most editors, putting the cursor on the red name and pressing the quick-fix shortcut offers the matching using.
Cause 2: A Wrong or Misspelled Type Name
C# is case-sensitive, so exponentialmovingaverage and ExponentialMovingAverage are different names, and so is a typo:
private ExponentialMovingAvarage _ema; // CS0246: 'Avarage' is not 'Average'
private ExponentialMovingAverage _ema; // correct
The same happens when a type name is guessed from another platform. MQL5 has no type for an indicator object (it returns an int handle), so a name like iMA is a function in MQL5 and not a type in C#. In a cBot the indicator is an object of a type such as SimpleMovingAverage or ExponentialMovingAverage, created with Indicators.SimpleMovingAverage(...).
Cause 3: The Attribute Without Its Namespace
[Robot(...)] and [Parameter(...)] are attributes, and they are types too (RobotAttribute, ParameterAttribute). If using cAlgo.API; is missing, the compiler reports CS0246 for the attribute, and the message names the long form:
CS0246: The type or namespace name 'RobotAttribute' could not be found
The short name in the code and the long name in the message are the same thing. Add using cAlgo.API; and both lines are fixed.
Cause 4: A Library That Is Not Referenced
A using line works only if the library is already part of the project. For the .NET base classes this is the case. For a library you add yourself (a JSON library, a helper DLL, a NuGet package), the cBot also needs the library as a reference, and the way to add it is the references or packages dialog of cTrader Automate. If you write using Newtonsoft.Json; without the reference, the error is CS0246 on the namespace itself:
CS0246: The type or namespace name 'Newtonsoft' could not be found
(are you missing a using directive or an assembly reference?)
Adding another using line does not help here. The library must be added to the cBot first. Also check that the cBot has the access rights it needs for what the library does, for example network or file access.
Cause 5: A Type from MQL5 or Pine Script
When you convert a strategy by hand, the type names come from the source language. These do not exist in C#:
| In MQL5 or Pine Script | In cTrader C# |
|---|---|
| datetime (MQL5) | DateTime (needs using System;) |
| MqlRates, MqlTick (MQL5 structs) | Bars and Symbol members, no struct needed |
| CTrade (MQL5 class) | ExecuteMarketOrder(...) and ClosePosition(...) on the Robot |
| series float (Pine Script) | DataSeries or double |
The conversion of the entry and exit rules is covered in Pine Script strategy.entry to cTrader C# and Pine Script strategy.exit to cTrader C#.
The Fixed cBot
using System;
using System.Collections.Generic;
using cAlgo.API;
using cAlgo.API.Indicators;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class FixedBot : Robot
{
[Parameter("EMA period", DefaultValue = 20, MinValue = 1)]
public int EmaPeriod { get; set; }
private ExponentialMovingAverage _ema;
private List<double> _closes = new List<double>();
protected override void OnStart()
{
_ema = Indicators.ExponentialMovingAverage(Bars.ClosePrices, EmaPeriod);
}
protected override void OnBar()
{
DateTime barTime = Bars.OpenTimes.Last(1);
_closes.Add(Bars.ClosePrices.Last(1));
Print("Bar {0}, EMA {1}, closes stored {2}", barTime, _ema.Result.Last(1), _closes.Count);
}
}
}
The bot only prints, so you can compare the numbers with the chart before you add trading logic. Note that the list grows on every bar: keep only the values you need, or the memory use grows for as long as the bot runs.
CS0246 and Similar Errors
| Error | Message | What it means |
|---|---|---|
| CS0246 | The type or namespace name 'X' could not be found | A type or namespace is unknown (often a missing using) |
| CS0103 | The name 'x' does not exist in the current context | A variable or method name is unknown |
| CS1061 | 'X' does not contain a definition for 'Y' | The object is known, the member after the dot is not |
| CS0234 | The type or namespace name 'Y' does not exist in the namespace 'X' | The namespace is known, the name inside it is not |
Checklist: What to Check When You See CS0246
- Read the name in quotes. Is it a type (a class or a namespace) or a variable?
- Check the spelling and the capital letters.
- Compare the top of the file with the lines listed in this guide, and add the missing
using. - Attribute in the message?
RobotAttributeorParameterAttributemeansusing cAlgo.API;is missing. - Name from another platform? Replace it with the C# or cTrader type.
- Third-party namespace? Add the library to the cBot as a reference, then add the
using. - Fix the first error first and build again, because one unknown type can cause several errors below it.
- When it builds, print a few values and compare them with the chart before you trade.
Common Mistakes
- Pasting a class without its
usinglines. - Deleting "unused"
usinglines that the code still needs. - Guessing a type name from MQL5 or Pine Script.
- Typing a type name with the wrong case.
- Adding a
usingfor a library that is not referenced. - Fixing the last error first. Start at the first one.
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.
- Pine Script to cTrader C#: how a strategy becomes a cBot.
- Pine Script strategy.entry to cTrader C#: entry rules with a full cBot.
- MQL5 undeclared identifier error: the MetaEditor version of an unknown name.
- 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 CS1503: the method is found, but an argument has the wrong type.
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.