📜 ⬆️ ⬇️

Back to the Future: Testing a Trading Robot with Historical Data

image

We have previously 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 the testing of the performance of the strategy on historical data — backtesting. Today we will talk about him.

What is it


In simple terms, backtesting is to run a trading strategy algorithm using historical financial data. The algorithm, when detecting certain stock exchange events (“signals”), will generate orders to buy or sell financial instruments — these operations will have an associated income or loss.
')
The total amount of income 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:


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

image

Misconceptions about backtesting


Michael Halls-Moore, a well-known expert in stock trading, Quant, and the developer of exchange robots, is convinced that novice developers of exchange systems often make mistakes when they are created due to certain misconceptions. In particular, the expert cites four such errors:

Waiting for equally high 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 may not work at all so effectively. It is necessary to measure the performance of the strategy for different values ​​of input parameters.

Use of "future" data

In some cases, creators of trading strategies include assumptions about the future state of affairs in the data set. In case of errors in the code, incorrect calculation of 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 frequent reasons why strategies work more effectively on historical data than in real time).

Invalid assessment of their psychological stability

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

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

What parameters need to be considered


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

Transaction costs

Novice traders often pay attention only to the performance of their algorithm directly on the market, but they forget to take into account the associated costs that can level all their income. In this case, the most obvious costs will be transaction fees charged by the exchange and the broker (in ITinvest, on some tariffs, fees are roughly equivalent to exchange fees ).

Slippage and delays

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

Some financial instruments have a high volatility (their price changes frequently), so when working with them you need to make allowances for possible slippage.

Liquidity impact

When dealing with relatively illiquid instruments, a trader must keep in mind the possible impact that the actions of his trading system will have on the market. If a certain share is bought and sold by not so many people, the order to purchase 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 trading orders

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

The market order (“by market”) is executed immediately at the price of a financial instrument (stock, futures , option , etc.) that has emerged on the market. Accordingly, if a major transaction is necessary, for example, buying a large number of shares, the market order will the fact that there will be several transactions at different prices - the market may not have the right amount of people willing to sell shares at the same price, then having bought all their shares, the robot will proceed to the next price offered and so on.

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

Limit orders allow the robot to determine the worst price at which it makes sense to conduct a transaction. Such an order may remain unfulfilled (if the market did not have anyone willing to sell or buy at a specified price) or partially executed (there were not enough volunteers), as a result of which it is considered a more passive means of making deals.

image

Their advantage is undoubtedly the fact that the price of the transaction is predetermined. The list of currently placed orders of the Limit type is called the order book (Order Book) and is displayed in the trading terminals as a separate window.

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 incorrectly modeled, the trading strategy may show the worst results when working in real time, compared to running on historical data.

Backtesting tools


There are quite a few publicly available systems that can be used to test financial strategies:


image

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

Conclusion


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 a trading robot in the “combat” conditions of the real market. At the same time, it is important to understand that successful work of a strategy on historical data does not guarantee an equally good result when used in real-time trading.

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

That's all, thank you for your attention. We will be happy to answer questions in the comments.

Attention! In ITinvest, a vacancy for a GUI C # developer has opened, the job is to implement front-end development of software products for trading on the exchange. Details on the link www.itinvest.ru/about/vacancies/programmer-gui-c .

image

PS If you notice a typo or mistake - write a personal message, and we will fix everything promptly.

Links and posts on the topic:

Source: https://habr.com/ru/post/238839/


All Articles