Quick Answer
In MQL5, you send a volume in lots: 0.10 is a tenth of a standard lot. In a cTrader cBot, the volume is in units. According to the cTrader documentation, cTrader algorithms calculate volume in units by default, and you can convert lots to units with Symbol.QuantityToVolumeInUnits(lots) and units back to lots with Symbol.VolumeInUnitsToQuantity(units).
A converted cBot that sends 0.10 as the volume asks for a tenth of one unit, which is far below the minimum of any symbol. The server refuses it, and the TradeResult carries the BadVolume error (see cTrader TradeResult error codes). The opposite mistake is also possible: a number that was meant as units but was copied from a lot size can be thousands of times too large. The fix is to decide once which unit your code uses, convert at the edge, and make sure the final volume respects the symbol's minimum, maximum and step.
From the author: I build cBots in cTrader Automate and convert strategies from Pine Script and MQL5 to C#. The lot-size line is the one that most often survives a conversion unchanged, and it compiles. The first sign of the problem is an order that is refused, or a position of a size nobody intended.
Key Facts
| Topic | MQL5 | cTrader cBot |
|---|---|---|
| Unit of the order volume | Lots | Units (by default) |
| Lots to the order volume | Use the number as it is | Symbol.QuantityToVolumeInUnits(lots) |
| Order volume back to lots | Use the number as it is | Symbol.VolumeInUnitsToQuantity(units) |
| Minimum, maximum, step | SYMBOL_VOLUME_MIN, SYMBOL_VOLUME_MAX, SYMBOL_VOLUME_STEP | Properties of the symbol in units (for example VolumeInUnitsMin) |
| A refused volume | Retcode 10014, invalid volume | ErrorCode.BadVolume |
The method names QuantityToVolumeInUnits and VolumeInUnitsToQuantity come from the cTrader documentation and the cTrader community. A page in the documentation also writes the first name in a shorter form in its text, while its code samples use QuantityToVolumeInUnits, so let the code completion of cTrader Automate confirm the exact spelling in your version.
What a Unit Is
A unit is one unit of the base currency of the pair. For EURUSD, 1 unit is 1 EUR, and a standard lot is 100,000 units on most brokers. This is a common setup, but the lot size can differ by symbol and by broker, so do not hard code 100000. Let the symbol convert for you:
double lots = 0.10;
double volumeInUnits = Symbol.QuantityToVolumeInUnits(lots);
On a symbol with a lot size of 100,000, this gives 10,000 units. Print it on your own account, because other symbols (metals, indices, crypto) have other sizes.
The Broken Conversion
This cBot only prints. It shows the mistake without any order calls:
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class BrokenVolumeBot : Robot
{
[Parameter("Volume (lots)", DefaultValue = 0.10, MinValue = 0.01, Step = 0.01)]
public double Lots { get; set; }
protected override void OnStart()
{
// MQL5 habit: the lot number is used as the volume
double volume = Lots; // 0.10 units, not 0.10 lots
Print("Would send volume {0}", volume);
// what was meant
Print("Units for {0} lots: {1}", Lots, Symbol.QuantityToVolumeInUnits(Lots));
Stop();
}
}
}
The Parameter line with lots is how many cBots expose the volume. That is fine: the parameter can be in lots, as long as the order call receives units.
Convert at the Edge
Keep the user setting in lots, and convert in one place, just before the order:
double volumeInUnits = Symbol.QuantityToVolumeInUnits(Lots);
var result = ExecuteMarketOrder(TradeType.Buy, SymbolName, volumeInUnits, "MyBot");
if (!result.IsSuccessful)
Print("Order failed: {0}", result.Error);
For a risk based volume, the idea from Pine Script strategy.entry to cTrader C# applies: calculate the volume in units from the risk and the stop distance, and then snap it to the symbol rules. The MQL5 side of the same calculation is in MQL5 lot size calculation.
Respect Minimum, Maximum and Step
Every symbol has a smallest allowed volume, a largest one, and a step. In the cTrader documentation, the example for a market order uses symbol.VolumeInUnitsMin as the volume, and the cTrader community says that each symbol has minimum and maximum volume properties that are given in units. A volume that is not a multiple of the step, or is outside the range, is a candidate for BadVolume.
A helper that snaps a volume to the rules keeps this in one place. It takes the limits as arguments, so you can test it without a symbol:
public static double SnapVolume(double units, double min, double max, double step)
{
if (units < min)
return 0.0; // too small: do not trade
if (units > max)
units = max;
double steps = Math.Floor(units / step + 1e-9);
double snapped = steps * step;
return snapped < min ? 0.0 : snapped;
}
The policy here is on purpose: a volume below the minimum returns 0, which means "do not trade". Rounding it up to the minimum would risk more than the rule allows. Choose the policy that fits your risk rule, and say it in a comment.
In the cBot, the call looks like this (check the property names VolumeInUnitsMin, VolumeInUnitsMax and VolumeInUnitsStep in the code completion of your version):
double units = SnapVolume(
Symbol.QuantityToVolumeInUnits(Lots),
Symbol.VolumeInUnitsMin,
Symbol.VolumeInUnitsMax,
Symbol.VolumeInUnitsStep);
if (units <= 0.0)
{
Print("Volume is below the minimum for {0}, no order.", SymbolName);
return;
}
A Full cBot That Tests the Helper
This cBot does not trade. It runs the helper on fixed numbers (minimum 1,000, maximum 10,000,000, step 1,000 units) and prints the result. It needs using System;:
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class VolumeHelperTest : Robot
{
protected override void OnStart()
{
double[] inputs = { 1500, 2999, 500, 25000000, 10000 };
foreach (double units in inputs)
Print("{0} -> {1}", units, SnapVolume(units, 1000, 10000000, 1000));
Stop();
}
private static double SnapVolume(double units, double min, double max, double step)
{
if (units < min)
return 0.0;
if (units > max)
units = max;
double steps = Math.Floor(units / step + 1e-9);
double snapped = steps * step;
return snapped < min ? 0.0 : snapped;
}
}
}
The log should show: 1500 -> 1000, 2999 -> 2000, 500 -> 0, 25000000 -> 10000000, 10000 -> 10000. If the numbers differ, check the rounding line.
How to Spot a Lots/Units Mix-Up
- Print the volume next to the symbol name before every order.
- An order that is refused with
BadVolume: compare the number with the minimum of the symbol. - A position that is much larger than expected: look for a lot number that was multiplied by a fixed
100000, then converted again. - A position that is much smaller: look for a lot number sent as units.
- A different broker or account: the lot size and limits can differ, so read them from the symbol each time.
Similar Problems
| Problem | What it means | Where to look |
|---|---|---|
| Order refused, no log line | The result is not read | TradeResult error codes |
| CS0266 | double assigned to int | CS0266 |
| Crash on result.Position | The order failed and there is no position | NullReferenceException |
| MQL5 invalid volume | The same idea in lots | MQL5 error 10014 |
Checklist: What to Check When a cBot Volume Is Wrong
- Decide the unit of every variable: lots or units.
- Convert once, with
Symbol.QuantityToVolumeInUnits. - Snap the volume to the symbol minimum, maximum and step.
- Choose the policy for a volume below the minimum.
- Print the volume and the symbol before the order.
- Check
IsSuccessfuland printError. - Read the limits from the symbol, not from a constant.
- Test on a demo account before the cBot trades with real money.
Common Mistakes
- Sending the lot number as the volume.
- Hard coding
100000as the lot size. - Rounding up to the minimum and risking more than planned.
- Ignoring the step of the symbol.
- Converting twice, so the volume is too large.
- Not reading the result of the order call.
FAQ
Related Guides
- cTrader order failed: TradeResult error codes: read
IsSuccessfulandError, includingBadVolume. - Pine Script strategy.entry to cTrader C#: orders, labels and risk based volume.
- cTrader C# error CS0266: a
doublevolume used where anintis expected. - cTrader cBot crash: object reference not set: a null variable stops the handler.
- MQL5 lot size calculation: the same calculation in lots.
- MQL5 invalid volume error 10014: the MetaTrader side of a refused volume.
- 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.
- MQL5 OrderSend failed with error 4756: read the retcode behind 4756, then fix the request.
Fix Compile Errors Without Guessing
If a cBot 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 test it on a demo account before using it on a live account.