Backtesting and Optimizing cBots
Note that all the values you set when backtesting the cBots won’t affect any of your accounts as they are not intended to deal with the real funds and are used for the demonstration purpose only.
cTrader Automate advanced backtesting functionality allows testing cBots’ performance within a certain time period under a specific set of conditions.
One can define a testing time period, start a cBot, and watch the results for how a cBot instance would have performed.
To backtest a cBot, select a cBot instance that you would like to test and proceed to the Backtesting tab to the right.
Define a time period for the backtesting by using the drop-down calendars or typing in the dates, or by dragging the slider between them.
Use the cog icon to set the Backtesting Settings.
- Specify the Starting Capital that your cBot will deal with during the backtesting by typing the value in or using the drop-down.
- Specify the commision per million by typing in or using the drop-down.
- Select the Data source for the backtesting:
- Tick data from Server. The most accurate historical data source. The cBot will get all ticks by tick price changes from the server for the available period.
- m1 bars from Server. The cBot will use 1 minute bars from the server.
- m1 bars in CSV file. The cBot will use 1 minute bars from the CSV file.
- Selected timeframe from the Server. The fastest way of retrieving data for the visual testing. The cBot will retrieve data in the form of the bars with the timeframe value set in the cBot Timeframe section. The longer the timeframe is set in your cBot, the faster the backtesting will be performed.
- Specify the Spread that will be applied for your backtesting period:
- Set the spread to the Fixed value to disable its deviations when placing an order. The value you set for the fixed spread will remain of this value within the whole backtesting period. Click Copy Live Spread to automatically set the current spread value.
- Set the spread to Random to select any spread value between the minimum and maximum values that you set when placing an order.
Sample CSV M1 Data¶
In order to use trend bars from the file you need to have comma-separated CSV file with following columns:
- Open Price
- High Price
- Low Price
- Close Price
1 2 3 4
When done click the Play button to the upper right to start your backtesting.
cTrader Automate allows visually backtesting the cBots to track the cBot work in the real time with the possibility to regulate the backtesting speed.
Check the Visual Mode box under the Backtesting settings icon to enable the Visual Backtesting.
Set the Backtesting Settings and select the backtesting timeframe as described above in the Backtesting Settings section.
Set the speed of your cBot instance testing using the Speed slider or toggles to the right.
When done click the Play button to the upper right to start the backtesting in the Visual Mode.
The Backtesting will start with the options and speed you have set and you will be able to track it in real time using all the cTrader instruments including zooming, changing the chart type, adding the indicators and drawing the chart objects, etc.
Note that you can use the progress slider to move forward on the X-axis, during the backtesting process, but you can not move backwards.
Also, you can change the speed of the backtesting process by using the speed slider or the toggles to the upper right or pause backtesting to view the intermediate result or apply some changes to the chart (add indicators, draw objects etc.).
If you are using the Visual Mode for backtesting your cBot, then you can track your cBot instance testing performance in the real-time in the chart and in the progress bar.
If the Visual Mode is off, then you will only see the testing results and the progress bar. In both cases, the backtesting may take some time to complete, depending on the Timeframe, speed, and other settings you have set.
You can estimate the progress and time remaining by looking at the Loading message displayed when loading data for the backtesting, or at the Visual Mode progress bar if the Visual Mode is on.
Once completed, the full details of the testing results will be displayed in the tabs below, including the positions, orders, trade statistics, historical data, events, as well as the cBot log.
All the opened positions are displayed in the Positions tab. It includes all the standard cTrader Positions information like traded instrument, position volume, direction, commissions, Net profit, etc.
The complete list of all the cBot orders and order options are displayed in the Orders tab.
The Equity Chart tab shows the changes in the Trading Account equity in relation to account balance over the backtesting time period.
Click anywhere on the Equity chart to automatically track the respective time period in the backtesting chart above.
All the cBot backtesting trades information is displayed in the Trades Statistics tab. Here you can find the following Trades statistics with the division to All Trades, Long Trades, and Short Trades:
- Net Profit - the net profit of all trades.
- Profit Factor - the ratio of Total Net Profit divided by the Total Net Loss.
- Commision - the total amount of commission potentially paid to the broker.
- 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 amount of the trades taken.
- Winning Trades - the total amount of the winning trades.
- Max Consecutive Winning Trades - the maximum number of the consecutive winning trades.
- Largest Winning Trade - a closing deal with the largest positive result.
- Losing Trades - the total amount of the losing trades.
- Max Consecutive Losing Trades - the maximum number of the consecutive losing trades.
- Largest Losing Trade - a closing deal with the largest negative result.
- Average Trade - the average profit for all trades.
- Sharpe Ratio - a ratio to measure risk-adjusted performance. It is calculated by dividing the cBot return by the cBot standard deviation of returns.
- Sortino Ratio - is an alternative to the Sharpe ratio, using downward deviation in place of standard deviation.
The History tab lists all the Closing Deals of the cBot instance backtesting performance including all the details like entry and close time, trading symbols, their volume, directions, entry and close price, and profit.
All the created and closed positions during the cBot instance testing run are displayed in the Events tab.
The Log tab contains the list of all the cBot instance backtesting information.
You can search the required events by typing to the search box to the upper left or use the filter to display only cBot testing statistics info or trading events.
cTrader Automate advanced Optimization functionality allows determining and applying the optimal set of parameters for a cBot based on the backtesting results.
The Optimization runs multiple backtest procedures and compares their performance.
To optimize a cBot select your cBot instance and proceed to the Optimization tab to the right.
Define the backtesting time period for the optimization by using the drop-down calendars or by dragging the slider.
Click the Parameters button for setting the parameters to be optimized for your cBot.
Specify the required values for the parameter to be used during the optimization. Alternatively, uncheck the parameters that you are not going to optimize.
Click the Criteria button for setting the standard or custom Optimization Criteria.
The standard Optimization Criteria group provides an ability to minimize or maximize the required trading factors, to be used during the optimization.
Fitness value will be calculated for each pass based on the set of selected criteria. The higher fitness value, the better.
Click Add Criteria to add more criteria, or click a cross next to a criteria to remove it.
If the Custom criteria are selected, you need to override GetFitness method in your cBot and provide a fitness value based on GetFitnessArgs. The higher fitness value, the better.
1 2 3 4 5
1 2 3 4 5
Multi Criteria Calculation¶
If you select multiple criteria for optimization then optimizer will use all of them equally to calculate the fitness value for an optimization pass.
To calculate the fitness value when multiple criteria is selected optimizer first multiply all maximizing criteria and then the minimizing criteria, after that it divides the absolute multiplication value of maximizing criteria to minimizing criteria.
Pseudocode of fitness calculation for multiple criteria:
1 2 3 4 5 6 7 8 9 10 11 12
Click the Method button (GA or #, depending on which method is selected) to select the Optimization Method. Two options are available:
- Genetic Algorithm (GA), that finds the optimal parameters faster as it emulates the biological process of evolution.
- Grid (#) that simply backtests each possible set of parameters.
Click the Resources button to set the CPU consumption for the backtesting optimization. Move the slider to set the CPU usage percentage.
The more resources you allocate, the faster the optimization will be performed.
Note that the CPU resources can be adjusted during the optimization.
Note that the Backtesting Settings described above are also applied to the cBot Backtesting Optimization.
Start the Optimization¶
When done click the Play button to the upper right to start the Optimization. The Optimization process may take some time depending on the optimization settings and the complexity of a cBot.
You can view the number of passes, the elapsed time and the remaining time estimate to the top of the Optimization tab.
The optimization process runs backtesting multiple times. Once backtesting for a specific set of parameters passed, the new row is added to the Passes grid.
When the Optimization is successfully done, the detailed results of all the optimization sessions performed are displayed in the Passes grid below.
Note that the Pass grid is configurable. You can drag the columns to change their location, or enable or disable the required columns by right-clicking and checking the columns you need.
The Passes grid contains the following columns:
- Pass - total number of passes in a current Optimization session.
- Fitness - shows how well a pass fits the Optimization Criteria.
- Equity - the value of equity at the end of backtesting.
- Balance - the balance at the end of backtesting.
- Net Profit - the difference between ending balance and the starting balance.
- Trades - the number of closed positions.
- Profit Factor - a total profit divided by a total loss.
- Max Equity Drawdown (%) - the maximum percentage of the equity drawdown.
- Max Balance Drawdown - the maximum percentage of the balance drawdown.
- Max Equity Drawdown (in account currency) - the maximum drawdown of the equity in euros.
- Max Balance Drawdown (in account currency) - the maximum drawdown of the balance in euros.
- Winning Trades - a total number of the winning trades during the session.
- Losing Trades - a total number of the losing trades during the session.
- Sharpe Ratio - a ratio to measure risk-adjusted performance.
- Sortino Ratio - an alternative to the Sharpe ratio, using downward deviation in place of standard deviation.
- Average Trade - an average profit for all trades in the session.
- Parameters - by clicking Apply in the parameters column you can apply the parameters of the pass to the current cBot instance.
Select a pass to view the detailed information on it to the bottom of the optimization window. The following parameters are displayed:
- Equity chart - the changes in account equity in relation to account balance over the testing time period.
- Trade Statistics - all the cBot backtesting trades information.
- History - all the closing deals of the cBot instance backtesting performance.
- Log - all the cBot instance backtesting information.
- Parameters - all the optimized parameters.
Note that if you enable the Autoselect the best pass option before you start optimization, then the best fitting pass will be automatically highlighted in the optimization results.
Multi Symbol Backtest/Optimize¶
cTrader Automate back tester and optimizer allows you to use multiple symbols data not just the chart symbol, you can load dynamically any of your trading account symbols data like Bars or Symbol object instance inside your cBot code and the cTrader back tester / optimizer will automatically discover it and it will load that particular symbol historical data and properties.