Skip to content

Fault Tolerance

The term ‘fault tolerance’ refers to the ability of an automated system to continue operating when encountering some sort of failure. In cTrader Automate, fault tolerance means that cBot instances can keep running even when facing errors.

This article explains how fault tolerance works and describes the key benefits this feature offers to algo traders. Below, we provide its one-minute summary.

Fault Tolerance in One Minute!

  • Under fault tolerance rules, when facing an exception, a cBot instance will continue running. This feature makes cBots even more stable and provides opportunities for refining how your code is executed.
  • cBots can now automatically output exceptions they encounter in the operations log, providing valuable information that you can use to refine your cBots.
  • You can use the OnException(Exception exception) method to customize how your cBots react to exceptions. Use this event handler to create reliable and effective extensions!

How Fault Tolerance Works in cTrader Automate

In brief, fault tolerance ensures that all cBots follow the rules detailed below.

  • When encountering an exception, a cBot instance must continue running.
  • Every exception that occurs while a cBot is operating must be logged.
  • Users must have the opportunity to introduce custom rules determining how cBots handle various exceptions.

Fault tolerance allows cBots to ignore all errors that do not threaten to crash their processes. While exceptions may still prevent code execution, fault tolerance greatly reduces the number of times a cBot may crash unexpectedly.

How the OnException() Method Works

Under fault tolerance rules, cBots have to ignore exceptions. However, you can also customize how your cBots react to certain exceptions, or overwrite the default fault tolerance behaviors entirely.

To do so, use the OnException() method. It acts as the dedicated handler for unhandled exceptions. Its default signature is as follows.

protected virtual void OnException(Exception exception)
    /// Introduce your custom exception handling logic here.

Consider the following example in which the OnException() method is used to change how a cBot reacts to the NullReferenceException exception.

namespace cAlgo.Robots
    [Robot(AccessRights = AccessRights.None)]
    public class SampleExceptionBot : Robot
        private const string label = "Sample Exception Bot";
        protected override void OnStart()
                var shortPosition = Positions.Find(label, SymbolName, TradeType.Sell);

        protected override void OnException(Exception exception)
            if (exception is NullReferenceException)
                MessageBox.Show("The position does not exist", "Error!");

To throw the NullReferenceException exception, we make our cBot close a short position that may not exist. When encountering the specified exception, our cBot is supposed to show a MessageBox displaying explanatory text. This allows us to quickly react to this exception and adjust the behaviors of our cBot.

Note that the OnException() handler is not supposed to handle exceptions that occur while this method is being invoked. Nonetheless, such exceptions are still recorded in the event log.

How Fault Tolerance Benefits Algo Traders

Fault tolerance is a crucial aspect of how cTrader Automate works by providing the following benefits to algo traders.

  • Fault tolerance greatly boosts the reliability of cBots and provides new opportunities for refining how you approach automated trading.
  • As cBots can display the exceptions they encounter in the event log, you can easily determine the conditions in which a cBot has encountered a particular error.
  • By allowing for handling exception events, the OnException() method can be used to perfect the behaviors of your cBots and improve their reliability.

What Are the Limitations of Fault Tolerance

As stated previously, several errors can crash entire cBot processes. They are as follows.

  • The OutOfMemoryException exception.
  • The StackOverflowException exception.
  • Any unhandled exception that occurs in a separate thread.
  • Any other errors that could threaten to crash the process that a cBot instance is running in.

When facing any of the above errors, your cBots will stop running.

Note that several different instances of the same cBot run in a single process. When at least one of these instances crash the entire process, all of these instances will stop working.

In summary, fault tolerance provides several valuable opportunities for making cBots even more stable and effective. The OnException() handles provides an additional layer of control over how your cBots react to exceptions.

Last update: January 23, 2023