Quick Answer
A cBot that stops with "cBot crashed: Error #" followed by an eight-digit number, such as #16308494, #62082064, #62082701 or #18185479, tells you almost nothing by itself. The numbers in the forum threads are all different, and none of the threads gives a table that explains what a number means. So the first rule is: do not look the number up. Treat the message as "an exception was thrown that your code did not handle", and find that exception.
The cTrader forum threads on this message show a few real causes. One is access from another thread: a cTrader staff member wrote that this error usually appears when indicators are accessed from a different thread, and that the cAlgo API is not thread safe, so any object (including Bars) must be created in the thread that uses it. Another is a long backtest on tick data: one poster fixed the crash by changing the chart from the 1-tick timeframe to H1, and another by moving to 64-bit Windows after the crash at around a million bars. The practical method is to wrap each handler in a try/catch that prints the exception, so that the next crash tells you the cause. This post shows how, and how to use the troubleshooting information that cTrader staff ask for.
From the author: I build and convert cBots. A crash with a number and no stack trace is the most frustrating message in cTrader Automate, because the cBot just stops. The fix is almost never in the number, it is in adding the one line of logging that should have been there from the start.
Key Facts
| What you see | Cause reported in the forum | What to do |
|---|---|---|
| cBot crashed: Error #16308494 after hours of running | Indicators accessed from a different thread (cTrader staff reply) | Create indicators and Bars in the thread that uses them; do not share them |
| cBot crashed: Error #62082064 at the same bar in a tick-data export | The poster blamed memory on the 1-tick timeframe and fixed it by switching to H1 | Test on a higher timeframe; clear the backtest cache (a staff suggestion) |
| Error #62082701 and similar after switching timeframes in a long tick backtest | The poster blamed 32-bit Windows near about 1,024,000 bars and fixed it with 64-bit Windows | Use 64-bit Windows and enough RAM |
| Error #18185479 in a backtest, no log lines | A staff reply asked for the code; no cause was given in that thread | Add logging and try/catch, see below |
| The number changes between runs | It is not a fixed code | Do not search for the number |
The first two rows quote staff or poster statements from the threads. The third row is the poster's own conclusion, not an official statement.
Step 1: Catch the Exception and Print It
If an exception reaches the platform unhandled, the cBot stops and you get the number. If your code catches the exception first, you get the type, the message and the line. Put this in each handler, at least in OnBar and OnTick:
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class SafeHandlers : Robot
{
protected override void OnBar()
{
try
{
DoBarWork();
}
catch (Exception ex)
{
Print("OnBar failed: {0}", ex); // type, message and stack trace
}
}
private void DoBarWork()
{
Print("Bars: {0}", Bars.Count);
}
}
}
Print("{0}", ex) writes the exception type, the message and the stack trace. In a real cBot the stack trace names the method and line, which is the information that the number hides. Do not leave the catch block empty after the investigation: decide whether the cBot should stop, or skip this bar.
Step 2: Check the Usual Code Causes First
Many crashes are exceptions that you can read once you log them. Check these posts first:
| Exception | What it means | Guide |
|---|---|---|
| NullReferenceException | A variable is null, often result.Position after a failed order | Object reference not set |
| ArgumentOutOfRangeException, index out of range | An index beyond Bars.Count or a collection size | cBot index out of range |
| A refused order that is not read | TradeResult.IsSuccessful is not checked | TradeResult error codes |
| A volume that is not valid | Lots used as units | Lots vs units |
Step 3: Threads
The cTrader answer in the thread was short: cAlgo is not thread safe, so exceptions can occur when an object is used from a thread other than the one that made it. If your cBot starts its own Thread, Task or timer callback, and touches Bars, indicators, Positions or Symbol from there, move that work back to the cBot's own thread. In cTrader Automate the safe place is the event handlers (OnStart, OnBar, OnTick, OnTimer). If you use Timer, use the cBot's own timer (Timer.Start) and handle OnTimer.
Step 4: Backtests With Tick Data
Two threads connect the crash with a very large backtest:
- A poster exporting tick data got a crash at the same bar every time. A staff reply suggested clearing the backtesting cache and trying again, and the poster found that switching the chart timeframe from 1-tick to H1 worked.
- Another poster saw several error numbers after switching timeframes in a long backtest, and concluded that the cause was 32-bit Windows near about 1,024,000 bars. Moving to 64-bit Windows 10 fixed it, and more RAM made the backtest faster.
Neither thread is an official explanation. Treat them as things to try when the crash only happens on a very long tick backtest: shorter date range, higher timeframe, 64-bit system, more memory, cleared cache.
Step 5: Send the Troubleshooting Information
In one thread, a staff member could not reproduce the problem from the source code and asked the poster to send the troubleshooting information the next time it happened, with the keyboard shortcut Ctrl+Alt+Shift+T. If you ask the cTrader team for help, give them: the cBot code (or a reduced version), the parameters, the broker, the date range and timeframe of the backtest, and that troubleshooting file.
A Small Test cBot
This cBot does not trade. It throws an exception on purpose inside a try/catch, so you can see what the log shows when the exception is caught:
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
public class CatchTest : Robot
{
protected override void OnStart()
{
try
{
int[] values = new int[3];
Print("Reading index 5: {0}", values[5]); // out of range
}
catch (Exception ex)
{
Print("Caught: {0}", ex.GetType().Name);
}
Stop();
}
}
}
The log should show Caught: IndexOutOfRangeException. If you remove the try/catch, the cBot stops with a crash message instead. This is the difference that the rest of the post depends on.
Common Habits That Lead to Blind Crashes
| Habit | What goes wrong | Do this |
|---|---|---|
| No try/catch in handlers | The crash message has only a number | Catch and Print the exception |
| Using result.Position without a check | NullReferenceException | Check IsSuccessful first |
| Sharing indicators with a thread | Not thread safe | Use the handlers |
| Backtesting years of ticks on a small machine | Memory limits | Shorter range, higher timeframe |
| Searching the number | The numbers differ between runs | Log the exception |
Checklist: What to Check When a cBot Crashes With a Number
- Do not search the number.
- Wrap
OnStart,OnBarandOnTickintry/catchand print the exception. - Run again and read the type and the stack trace.
- Check the guides for that exception in the table above.
- Check for threads, tasks and timers that touch the API.
- If it only fails in long tick backtests, try a shorter range, a higher timeframe and 64-bit Windows.
- Clear the backtest cache and run again.
- Prepare the code, parameters, broker and troubleshooting file if you ask for help.
Common Mistakes
- Looking for a table of error numbers.
- Leaving a
catchblock empty. - Using the API from a background thread.
- Running a huge tick backtest on a 32-bit system.
- Ignoring the order result.
- Not saving the log before closing the window.
FAQ
Related Guides
- cTrader cBot crash: object reference not set: the most common exception behind a crash.
- cTrader cBot index out of range exception: a bar index beyond the data.
- cTrader order failed: TradeResult error codes: read the result of an order.
- cTrader volume is in units, not lots: a common first-cBot mistake.
- cTrader error CT0002: assembly must contain algo type: a build error that stops before the cBot runs.
- Pine Script to cTrader C# cBot: the full conversion process.
- What to check in a trading bot before going live: a pre-live checklist.
- cTrader error CS0535: does not implement interface member: a class that misses an interface member.
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.