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. For cTrader algos, fault tolerance means that processes hosting cBots and indicators 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, an algo will continue running. This feature makes cTrader algos even more stable and provides opportunities for refining how your code is executed.
  • Algos can now automatically output exceptions they encounter in the operations log, providing valuable information that you can use to refine your code.
  • You can use the OnException(Exception exception) method to customise how your algos react to exceptions. Use this event handler to create reliable and effective extensions!

Fault Tolerance Rules

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

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

Fault tolerance allows algos to essentially 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 an algo may crash unexpectedly.

The OnException() Method

Under fault tolerance rules, algos have to ignore exceptions. However, you can also customise how your algos 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 any unhandled exceptions. Its default signature is as follows.

1
2
3
4
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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
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);
                ClosePosition(shortPosition);
        }

        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.

Benefits of Fault Tolerance

Fault tolerance is a crucial aspect of how algo trading in cTrader works by providing the following benefits.

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

Handling Crashes

As stated previously, several errors can crash entire 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 an algo is running in.

When facing any of the above errors, an indicator instance will be stopped; it will not be restarted automatically. However, the exception will still be recorded in the log.

In contrast, when a process hosting a cBot instance crashes, it will be restarted automatically (with the exception being recorded in the log).

Multiple Instances in a Single Process

There are cases when a single process may host several cBot instances. If one of these instances encounter an exception that causes it to crash, the entire process will also crash. On restart, the process will restart all cBot instances it originally hosted.

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