资源加载中... loading...

FMZ Backtest Mechanism Description

Author: Ninabadass, Created: 2022-03-23 10:06:22, Updated: 2022-03-28 13:42:35

Most strategies require backtests for verification before they are performed in bots. FMZ supports the spot, futures and perpetual contracts of some cryptocurrencies, as well as all varieties of commodity futures. However, the backtest mechanism of FMZ Quant platform is different from the common onbar backtest, which has caused confusions for many beginners. This article will detail and answer some common backtest questions.

How does a backtest system work?

img

s shown in the picture above, the time from the backtest start time to the end time can be regarded as a time axis. During backtest, the backtest time point moves from left to right along the axis to start backtest. At one time point, only the history data before this time point can be obtained. Based on the history data, the strategy makes purchases and sales, and finally forms the profit and loss. Obviously, the distribution of time points of backtest is discrete, and the density of the distribution represents the accuracy of backtest. Of course, considering that the denser the backtest time points, the longer the time required, the actual backtest system needs to make a choice between accuracy and efficiency.

Traditional onBar Backtest System

The onbar backtest mechanism is based on K-lines, that is, each K-line generates a backtest time point. At the time point, you can obtain the information, such as the current K-line highest, lowest, open and close prices, trading volume, etc., as well as the history K-line information before this time point.

The disadvantage of this mechanism is obvious: on a K-line, only one trade can be generated, and the price is usually based on the K-line close price. A K-line can only obtain four prices, namely open, close, highest and lowest prices, but cannot obtain the information, such as the price changes on one K-line, and whether the lowest price occurs first or the highest price first. Take the 1-hour K-line as an example. The market information must be obtained every few seconds in the bot, and the trading order will also be issued in the bot instead of waiting for the K-line to end. The advantage of the onbar backtest mechanism is that it is easy to understand and the backtest speed is extremely fast.

FMZ Quant onTick Backtest System

img

The above picture shows the FMZ backtest settings interface. The backtest mode is divided into two types, namely the simulation level and the real market level, which will be introduced separately down below:

What is tick?

Unlike K-line data, tick is the price at a specific time point. According to the K-line data, we actually only know when the open price and the close price occurred. It is not clear when the price reaches the highest value in the K-line period. In fact, the K-line data is also generated based on tick. According to the K-line data, it is also possible to simulate the changes of a specific tick in a K-line period. Although it is not a real tick, it can make our backtest more accurate. And the K-line period based on the simulation can be much shorter than the period used for backtest, so the accuracy is higher.

Simulation Level Backtest

For the simulation level backtest, you need to select the K-line period and the underlayer K-line period used for backtest. For example, if the strategy uses hourly line backtest, and the underlayer K-line is set to 5 minutes, then the interval of backtest time points will be based on the tick simulated by the 5-minute K-line, which is reflected in the continuous changes of the K-line close price in the latest 1 hour. The mechanism of generating ticks in the K-lines according to the K-lines is similar to that of MT4. There are detailed instructions in this post: https://www.fmz.com/bbs-topic/662

img
We use a simple strategy to demonstrate this mechanism; the strategy code:

function main() {
  while(true){
      var records = exchange.GetRecords() //GetRecords can fill the parameters, and obtain K-lines of different periods 
      var ticker = exchange.GetTicker()
      Log('K-line close price: ', records[records.length-1].Close, 'ticker Buy1 Sell1 prices: ', ticker.Buy, ticker.Sell)
      //js backtest does not use Sleep, and it will skip to the next tick; Python needs 1 hour of sleep time 
  }
}

Backtest result: img

Only the open and close ticks of each K-line are fixed, and the simulated 12 ticks are added in the middle, so that one K-line will form 14 backtest time points. If you backtest for one day with the underlayer K-line period of 5 minutes, a total of 24×12×14 = 4032 time points will be generated, while the traditional onBar backtest only has 24, so the accuracy is greatly improved. The open and close position operations can also be completed within one K-line period. Although the intermediate generated ticks are simulated, that does not affect a lot. In the backtest, as long as the buy order price is bigger than the sell one price, and the sell order price is smaller than the buy one price, the trading will be matched. This method of backtest takes into account the speed and accuracy of backtest and it is recommended for everyone.

Real Market Level Backtest

The real market level backtest uses the real tick, and the shortest interval between each time point is only 1s. This kind of backtest is accurate to the change per second, but due to the large amount of data, the backtest speed is slow and the backtest time cannot be too long. The picture below shows the real tick. The real market level backtest can be used to precisely verify strategies.

img

Differences between Backtest and Bot

Even the real market level backtest and the real bot trading still have obvious data deficiencies, such as the inability to obtain trading history trades, the inability to obtain actual depth changes, real network delays and so on. Even so, FMZ current backtest system is relatively complete, and there are many small functions, such as simulating network errors, which can be used to test the fault tolerance of strategies, simulate network delays, and draw market icons.

FAQ

Why only a few trading pairs and platforms are supported to backtest?

At present, there is only data of a few common trading pairs. In fact, the relationship between the strategy and the variety is not very close, so it is enough to verify the strategy.

Can it simulate BitMEX to charge the funding rate?

Sure, you can choose BitMEX backtest to open the log. img

Where the backtest is operated?

The backtest of JavaScript strategies is performed in the browser, while Python can choose FMZ server or its own docker to operate the backtest.

Can the backtest logs be downloaded?

Of course, you can. There is a download button on the right corner of the logs.

Can the backtest be operated locally?

FMZ opens the source of Python backtest engine. Reference: https://www.fmz.com/bbs-topic/1687


More