Introduction¶
Backtesting is a tool for evaluating cBots by allowing them to trade on historical market data under certain pre-defined conditions.
When backtesting, you can run a cBot instance on past market movements. Afterward, cTrader will generate a detailed report about the trading operations performed by your cBot and the final equity/balance statistics.
Note
Any trading actions performed when backtesting cBots will not affect any of your accounts. Backtesting is not intended to deal with any real funds. Instead, this feature only exists for analysis purposes.
To backtest a cBot, select one of its instances and switch to the 'Backtesting' tab.
Backtesting Settings¶
To set up how backtesting works, click on the 'cog' icon located in the upper-left corner. cTrader should open the following menu.
The options in this menu are defined as follows. All money-related parameters (e.g., the starting capital) are specified in USD.
Option Name | Action and Description |
---|---|
Starting Capital | Define the starting capital with which your cBot will operate when trading. |
Commission | Establish the commission rate per million traded units. |
Data | Choose which timeseries data that your cBot will be tested on. The available options are as follows.
|
Spread | Select the spread value(s) that will be applied during backtesting. The following options are available.
|
Sample CSV M1 Data¶
To source trading data from your local machine, make sure to create a comma-separated CSV file containing the following data.
- Date
- Time
- Open Price
- High Price
- Low Price
- Close Price
- Volume
Example
1 2 3 4 |
|
Backtesting Procedure and Modes¶
Specify the period for testing using the controls summarised below.
- Use the interactive slider at the top of the 'Backtesting' tab.
- Select the necessary values from the calendar menus to the left and right of the slider.
- Input custom values (using the
DD:MM:YYYY
format) into the fields on both sides of the slider.
You can run backtesting using the following modes.
- Non-real-time testing. Your bot performs all of the required operations in sequence; afterward, you only have access to the final results including trading statistics, the equity chart, etc.
- Real-time (visual mode) testing. The historical data you have provided is passed to your cBot at a certain playback speed that you can customise. As a result, you can see trading operations as they are placed. All trading statistics are also updated at the playback speed you have chosen.
Non-Real-Time Testing¶
After setup is finished, click on the 'Play' button to the right of the slider. Depending on the length of the specified period and your local machine specs, you may need to wait several minutes before cTrader generates the results.
Real-Time (Visual Mode) Backtesting¶
Specify the required settings including the testing period and enable the 'Visual Mode' flag located below the calendar slider.
Adjust the data playback speed by dragging the 'Speed' slider or using the menu to its right. After you are finished setting up, press the 'Play' button.
cTrader will then start displaying a trading chart for the chosen period in real-time accelerated using your playback speed setting. Note that you can adjust the playback speed dynamically as backtesting occurs.
Depending on its code, your cBot will also perform trading operations in real-time reacting to the technical analysis signals that materialise in the chart. The backtesting statistics will be automatically updated based on the actions taken by your cBot.
Depending on your chosen period and the playback speed, visual mode testing may be time-consuming. Nonetheless, if your cBot is not behaving as expected, you can test its reactions to various market conditions by using visual mode testing and custom trading data sourced from a CSV file.
Backtesting Using Renko Charts and Range Bars¶
cTrader supports backtesting cBots on Renko and range bar charts. To perform such a backtest, create a new cBot instance and select a Renko/range bar chart as the trading chart to which it is attached.
Alternatively, with a cBot instance already selected, use the chart selector menu at the top of the screen by clicking on the 'three dots' icon. In the menu that has appeared, select one of the available volume thresholds for generating Renko bricks or range bars.
After cTrader generates a Renko/range bar chart for the chosen symbol, switch to the 'Backtesting' tab. In it, configure the backtesting mode/settings as you normally would. Click on the 'Play' icon to perform a backtest on the chosen chart.
Backtesting Results¶
cTrader generates several trading statistics to showcase backtesting results. To view this data, proceed to the tabs located just below the trading chart.
The Equity Chart¶
When backtesting is performed (regardless of whether you use visual mode), cTrader generates an equity chart with the number of trades on the X-axis and the account balance on the Y-axis. It contains the following statistics.
- The yellow line showcases the account balance as it changes with each trade.
- The grey zone highlights how total equity increases or decreases depending on the trades made by your bot.
To get a better idea of what the equity chart does, consider the example above. It shows that our bot has performed more than 290 trades during backtesting. With each change in the account balance, we can also see a corresponding change in total equity.
Trade Statistics¶
The 'Trade Statistics' tab contains data on how exactly your cBot has performed throughout backtesting. It includes the following fields.
Field Name | Definition |
---|---|
Net Profit | The total net profit of all trades. |
Profit Factor | The ratio calculated by dividing the total net profit by the total net loss. |
Commission | The total amount of commissions paid for the trades made by your cBot. |
Max Balance Drawdown | The maximum amount of the balance drop-down. |
Max Equity Drawdown | The maximum amount of the equity drop-down. |
Total Trades | The total number of trades performed by your cBot. |
Winning Trades | The total number of trades that have resulted in realized profits. |
Max Consecutive Winning Trades | The largest number of consecutive trades that have generated realized profits. |
Largest Winning Trade | The largest realized profit generated by one trade. |
Losing Trades | The total number of trades that have generated realized losses. |
Max Consecutive Losing Trades | The largest number of consecutive trades that have generated realized losses. |
Largest Losing Trade | The large amount of realized losses generated by one trade. |
Average Trade | The average realized P&Ls generated by all trades. |
Positions¶
The 'Positions' tab showcases all positions that are currently open by your cBot. It is structured analogously to the 'Positions' tab in the regular 'TradeWatch' display and contains the same data.
Click on the 'cog' icon in the lower-right corner to customise what data is displayed in the central table.
Orders¶
The 'Orders' tab contains information about the orders that have been placed by your cBot. Its structure and appearance copy the 'Orders' tab in the main 'TradeWatch' display.
History¶
The 'History' tab displays statistics on all opening and closing deals that have been recorded during backtesting. Similarly to other tabs discussed above, it copies the 'History' tab in the 'TradeWatch' display.
Events¶
The 'Events' tab lists all server-related communications and displays various parameters of these events.
Log¶
Last but not least, the 'Log' tab displays your cBot output throughout backtesting. The list of messages is fully searchable and filterable by event type and instance.
Optimisation¶
It can be difficult to specify an optimal set of initial parameters for a cBot. Luckily, cTrader offers a built-in bot optimisation feature. Optimisation takes your code and runs it several times with each 'pass' being based on various parameter values. It then presents you with a set of customisable results which you can use to define the optimal parameter configuration.
To use this feature, select a cBot instance and switch to the 'Optimisation' tab.
Afterward, define the backtesting period for optimization by using the drop-down calendars or by dragging the slider.
Optimisation Settings¶
As optimisation is essentially just a series of backtests, you can specify backtesting settings analogously to how it is done in the 'Backtesting' tab.
Optimisation Parameters¶
Click on the 'Parameters' button to the left of the calendar slider. You should see the following window.
In it, check the flags next to the cBot parameters that you would like to optimise. The 'Timeframe' parameter is available for all cBots.
Optimisation Criteria¶
Press the 'Criteria' button located to the right of the 'Optimisation Parameters' button. cTrader will open the following tab.
Optimisation criteria define how the optimisation algorithm ranks your result after backtesting. You can choose between the following options.
- Standard. A series of predefined criteria that you can aim to either minimise or maximise. To do this, choose a criterion from a drop-down menu to the right and select the optimisation direction in its related menu to the left. To add a new criterion, click 'Add Criterion'.
- Custom. A custom criterion defined within your cBot code using the
GetFitness()
method as shown in the below examples.
1 2 3 4 5 |
|
1 2 3 4 5 6 |
|
Multi-Criteria Calculations¶
If multiple criteria are used to calculate parameter effectiveness, cTrader will use all of them equally to calculate the fitness value for an optimization pass.
Specifically, the platform multiplies the values of all maximising criteria and separately multiplies the values of all minimising criteria. Subsequently, it divides the absolute multiplication value for the maximising criteria by the same value for the minimising criteria.
The following pseudocode shows how exactly multi-criteria fitness values are calculated.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Method¶
Press the 'Method' button (represented either as GA
or #
depending on which option is chosen). In the below menu, choose which optimization method is used by the platform.
The following options are available.
- Genetic Algorithm. This algorithm emulates the biological evolution process. Please see a separate section for its detailed description.
- Grid (#). This algorithm sequentially backtests each possible set of parameters.
Note
In our estimation, the genetic algorithm is significantly faster for finding optimal parameter values.
Resources¶
Click the 'Resources' button to select which percentage of your CPU load is allocated for backtesting. Move the slider to set the CPU usage percentage.
The more resources you allocate, the faster the optimisation process will be completed. However, you may see a drop in performance when using other applications.
Note that the CPU resources can also be adjusted during optimisation itself.
Starting and Managing the Optimisation Procedure¶
To proceed with optimisation, click on the 'Play' button to the right of the calendar slider. Depending on your machine resources, the optimization settings, and cBot complexity, optimization may take some time.
The UI bar immediately below the calendar slider will provide information about the number of backtesting passes completed, the elapsed time, and the remaining time estimate.
The central screen in the 'Optimisation' tab, meanwhile, will provide a real-time grid containing information about all of the backtesting passes that the algorithm has completed.
To apply a set of parameters tested during a certain pass, press the 'Apply' button. It only becomes active if no optimisation procedures are currently running.
Alternatively, check the 'Autoselect the best pass' flag in the upper-left corner for cTrader to automatically select the pass (and the parameters) that has achieved the best possible result according to your specified optimization criteria.
Optimisation Results¶
After optimisation is completed, the central table will display the final list of all backtesting passes and their results.
This table has the following columns. As the grid is fully configurable, these columns can be dragged-and-dropped or disabled altogether after right-clicking on the table and deselecting them from the contextual menu.
Field | Definition |
---|---|
Pass | The pass number. |
Fitness | The value showcasing how well the pass fits the optimization criteria. |
Equity | Total equity at the end of the pass. |
Balance | Total balance at the end of the pass. |
Net Profit | The difference between the final balance and the starting balance. |
Trades | The total number of closed positions. |
Winning Trades | The total number of winning trades achieved during the pass. |
Losing Trades | The total number of losing trades achieved during the pass. |
Profit Factor | The total profit/total loss ratio. |
Max Equity Drawdown (%) | The maximum percentage of the equity drawdown. |
Max Balance Drawdown (%) | The maximum percentage of the balance drawdown. |
Max Equity Drawdown | The maximum equity drawdown specified in the account deposit currency. |
Max Balance Drawdown | The maximum balance drawdown specified in the account deposit currency. |
Average Trade | An average profit for all trades made during the pass. |
Pass Parameters | Click on the 'Apply' button in this column to apply the parameters from this pass to your cBot. |
Select a pass to view detailed statistics about it in the display below the central grid.
The first seven tabs in this display provide the same information as the same tabs in the 'Backtesting' window. The 'Pass Parameters' tab is unique to optimization.
Pass Parameters¶
The 'Pass Parameters' tab provides the following information.
cTrader will highlight all optimised parameters in green while any fixed parameters (not enabled in the 'Parameters' menu) will remain unhighlighted.
Saving and Loading Optimisation Results¶
cTrader also allows for saving and loading optimisation results to and from a locally stored .optres
file.
Note
A .optres
file is simply a collection of key-value pairs with keys representing various optimisation settings and metrics.
Tip
You can use .optres
files to continue refining your cBots across multiple local machines without losing any progress. You can also 'feed' data from this file to generative AI tools to try and detect any patterns in the passes that your cBot has completed.
To save optimisation results, wait until the process is concluded and click on the 'Save' icon. In the file explorer dialogue, type the file name and save the file.
To load optimisation results, click on the 'Load' icon and select the required file in the file explorer window.