Back to the future: checking the performance of a trading robot using historical data


    Earlier, we already considered the issue of the mandatory stages of developing a trading strategy for working in the stock market. One of the most important stages is testing the performance of a strategy on historical data - backtesting. Today we’ll talk about him.

    What is it

    In simple terms, backtesting involves launching a trading strategy algorithm using historical financial data. The algorithm, upon detecting certain exchange events (“signals”), will generate orders to buy or sell financial instruments - these operations will have a related profit or loss.

    The total profit or loss (profit and loss, P&L, PnL) for the time specified in the trading strategy will be an indicator of the success or failure of the algorithm.

    There are several goals that trading program developers achieve using backtesting:

    • Filtration - each strategy has certain indicators of productivity and work efficiency, which are embedded in it by the developer. Accordingly, any strategy that does not allow achieving the set goals should be “filtered out”.
    • Modeling - with the help of back testing, developers can test various market models (changing liquidity conditions, transaction costs, order processing speed, channel delays, etc.) without the risk of losing real money.
    • Optimization - using the “run” of a strategy on historical data, you can improve its performance in specific market situations.
    • Health check - with the help of testing, the developer can understand if there were any mistakes when describing the strategy in the program code.

    As you can see, backtesting is a useful tool for developers of financial systems, however, it is not always possible to correctly test on historical data. The more high-frequency strategy should be implemented, the more difficult it is to correctly model the impact of certain market situations and the parameters of a particular exchange platform on the overall system performance.


    Backtesting Mistakes

    Michael Halls-Moore, a well-known expert on exchange trading, quantum and the developer of exchange robots, is convinced that novice developers of exchange systems often make mistakes when creating them due to certain errors. In particular, the expert cites four such misconceptions:

    The expectation of equally good results in the future

    Often, the developer is tempted to make changes to the test parameters to obtain more convincing results.

    At the same time, if in the case of historical data there is an opportunity to change something and accurately predict the result, then in the "combat" mode the robot can work not so effectively. It is necessary to measure the performance of the strategy at different values ​​of the input parameters.

    Use of “future” data

    In some cases, the creators of trading strategies include assumptions about the future market situation in the data set. In case of errors in the code, incorrect calculation of the optimal parameters for the strategy, or incorrect use of extreme price values ​​(highs and lows), the launch of such a strategy in the real market may fail (this is one of the most common reasons why strategies work more efficiently on historical data, than in real time).

    Incorrect assessment of their psychological stability

    When conducting tests, the developer sees the final performance of his algorithm. If the system makes a profit in a certain time period (say, a year or five years), then the temptation is not to pay attention to deposit drawdowns (losses incurred) that occurred along the path to success. It seems to people that they can easily survive the loss of 25% of their money (after all, then the robot must recoup).

    In practice, not everyone has the strength to survive such moments without taking rash actions (and if the algorithm allows the loss of 25% of the money in history, then in reality this situation is very likely), which often lead to even greater losses.

    What parameters should be considered

    Developers of trading systems need to take into account many different parameters that can affect the final financial viability of a particular strategy.

    Transaction costs

    Beginner traders often pay attention only to the performance of their algorithm directly in the market, but forget to take into account the associated costs, which can level out all the income received. The most obvious costs in this case will be commissions for transactions levied by the exchange and the broker (for ITinvest, at some tariffs, the fees are approximately the same as exchange fees ).

    Slippage and delays

    Slippage refers to the difference in price between the one at which the trading robot intended to carry out the transaction, and the one at which it actually passed. It takes time to “deliver” the order to the core of the exchange trading system. In the case of high-speed trading robots (HFT), every millisecond on the account for which the price may change slightly makes the transaction not so profitable (or unprofitable at all).

    Some financial instruments have great volatility (their price changes often), so when working with them you need to make a discount on possible slippage.

    Liquidity effect

    When working with relatively illiquid instruments, the trader must keep in mind the possible impact that the actions of his trading system will have on the market. If not many people buy and sell a certain share, then an order to buy a significant number of such shares can greatly change their price. In order to avoid such a situation, it is necessary to teach the robot to break deals into a large number of small orders that cannot greatly affect the market.

    Types of Trade Orders

    The trading strategy is also influenced by the kind of trading orders its developer plans to use to complete transactions. Most often, traders resort to market orders and limit orders.

    The market order (“according to the market”) is executed immediately at the price of a financial instrument (stock, futures , option , etc.) that has formed on the market at the current moment . Accordingly, if it is necessary to complete a large transaction, for example, to purchase a large number of shares, the market order will result to the fact that there will be several transactions at different prices - the market may not have the right number of people who want to sell shares at one price, then having bought all their shares, the robot will go to the next offered price and so on.

    Market orders are an aggressive tool - they will always be executed, while the final transaction price remains unknown to the trader.

    Orders like Limit allow the robot to determine the worst price at which it makes sense to conduct a transaction. Such an order may remain unfulfilled (if no one was willing to sell or buy at the indicated price on the market) or partially executed (there weren’t enough willing people), which is why it is considered a more passive means of making transactions.


    Their advantage, of course, is the fact that the transaction price is predetermined. The list of currently placed orders of the Limit type is called the order book and is displayed in a separate window in the trading terminals.

    When testing a strategy, it is important to pay attention to its behavior when using market and limit orders. In the event that the order queue is modeled incorrectly, a trading strategy may show worse results when working in real time, compared to running on historical data.

    Backtesting Tools

    There are a fairly large number of publicly available systems that can be used to test financial strategies:

    • MS Excel - familiar to everyone and everyone, Microsoft Excel can be used to write mechanical trading systems. Most brokers allow you to associate this tool with their software products (uploading data and generating trading signals using VBA). The disadvantage of such a solution will be the low speed of work, and the plus is the free and quick implementation of simple strategies. Alternative - Open Office
    • Matlab is a software environment designed to perform complex calculations. There are plugins for use in stock trading. With its help, you can create small scripts that nonetheless describe fairly complex strategies. Minus - the system is paid and not cheap. Alternatives for the Russian market TSlab and StockSharp . Traders also use MetaStock , Wealth-Lab and Omegaproducts to create mechanical trading systems.
    • C ++ / C # are programming languages ​​that are widely distributed in the financial world. Java and Scala are gaining popularity.
    • Built-in tools of trading terminals in - some trading terminals have built-in tools for creating trading robots and backtesting strategies. The corresponding plug-in can be installed in the SmartX terminal . For writing robots, the scripting language TradeScript is used .


    Window for back testing of the plugin for creating robots on TradeScript in the SmartX terminal


    Backtesting is the most important stage in the development of a trading strategy, without which it is difficult to count on the adequate operation of the trading robot in the "combat" conditions of the real market. It is important to understand that the successful operation of the strategy on historical data does not guarantee an equally good result when used in real-time trading in real time.

    In addition to testing on historical data, developers should check the program in real time - this can be done using special test trading systems provided by exchanges and brokers. Using such risk-free systems with virtual money, you can debug the reaction of the robot to changing market conditions - usually the data in such cases is provided by exchange platforms (with a delay or “thinned out”).

    That's all for today, thanks for watching. We will be happy to answer questions in the comments.

    Attention! In ITinvest, a vacancy for the C # GUI developer has opened, the job is to implement front-end development of software products for trading on the exchange. Details are available at .


    PS If you notice a typo or mistake - write in a personal message, and we will quickly fix it.

    Related links and posts:

    Also popular now: