What you need to know before developing a backtester for a trading strategy: typical problems, types of systems and their parameters



The editorial board of QuantStart wrote a material about what you should know when you start developing your own system for testing trading strategies. We discussed some of the issues raised in the article earlier on the blog, so this time we prepared an adapted retelling of theses on what problems developers face, what is the difference between back-testers of different types, and what are their pros and cons.

What is a backtester


A backtest is the application of the rules of a trading strategy to a set of historical data on the prices of financial instruments. The essence of the approach is that if we develop a mechanism for determining the moment for entry and exit from a position (buy / sell), for example, stocks from a certain portfolio, and apply the resulting rules to historical data, this will give an idea of ​​the effectiveness of the trading strategy “in the past ".

One day, someone said that "all models are wrong, but some are useful." This phrase is great for backtesting. Systems for historical testing of financial strategies help determine whether the existing set of rules should be applied to real trading. If we know how a strategy could lead in the past, it will help filter out bad strategies without the need for real financial losses.

The problem is that the result of backtesting has nothing to do with the results of real trading on the exchange. This is just a model of reality. A model that often contains many assumptions.

There are two main types of backtesters - cyclic (for-loop) and event-driven (event-driven).

When developing such systems, there always arises the need for a compromise between accuracy and implementation complexity. These two types of backtesters represent the whole range of options for such a compromise.

Backtesting challenges


Testing for historical data carries a lot of difficulties. All of them are related to the fact that the whole process is only a simulation of reality. Here are just some of them:


There are also other issues that are not so often discussed, but nevertheless crucial for creating a quality working back tester. Among them:


On this, with the problems of testing everything on history, we now turn to the description of the systems themselves for such tests.

Two kinds of backtesters


First, consider cyclic systems. This is the simplest type of backtester, which is most often described in various blog posts on finance.

Cyclic backtesters


They work like this - the system simply iteratively passes every trading day (or the OHLC bar), performs calculations related to the price of the desired asset (for example, calculates moving average closing prices), and then performs the corresponding operation (entering a long or short position). Further iterations continue. In the process, information about the performance is saved in order to build an equity curve (equity curve).
Here is the pseudo-code of this algorithm:

for each trading bar: do_something_with_prices(); buy_sell_or_hold_something(); next_bar();PythonCopy 

As you can see, the system is extremely simple, which makes such back-testers an excellent option for getting the first estimates about the prospects of the trading system.

pros


Cyclic backtester is very easy to implement using almost any programming language, and do it quickly. This is useful when you want to test the effect of many different parameters.

Minuses


The main disadvantage is unrealistic results. Often in cyclic back-testers there is not even basic functionality for accounting for transaction costs, it has to be implemented separately. Also usually used only MARKET-orders.

Also, the ability to reuse code for a test and productive system is minimal, so you have to write it again. This increases the likelihood of software errors.

Cyclic back testers are prone to prediction errors. They should be used solely as a filtering tool to discard obviously unsuccessful strategies. At the same time, it is important to maintain an extremely skeptical attitude towards strategies that have shown good results. It is important to remember that in real life strategies rarely show themselves better than during backtesting.

Event-oriented systems


Systems of this type are on the opposite side of the spectrum. They are much closer to reality. Usually they work in huge while loops, during which in a special “event queue” “events” are constantly searched for, including:


When a certain event is recognized, it is transmitted to the appropriate module in the infrastructure of the trading system for further processing and potentially generates new events that again fall into the queue.

The pseudocode of such a backtester looks like this:

 while event_queue_isnt_empty(): event = get_latest_event_from_queue(); if event.type == "tick": strategy.calculate_trading_signals(event); else if event.type == "signal": portfolio.handle_signal(event); else if event.type == "order": portfolio.handle_order(event); else if event.type == "fill": portfolio.handle_fill(event) sleep(600); # Sleep for, say, 10 minsPythonCopy 

As you can see, the system is extremely dependent on the processing module of the portfolio - this is the real heart in event-oriented systems.

pros


This type of back tester has many advantages:


Minuses


The advantages of event-oriented systems are understandable, but there are also certain disadvantages. Among them:


Other materials on finance and stock market from ITI Capital :


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


All Articles