Price Simulation

From FxCodeBaseWiki

Jump to: navigation, search


What is Price Simulator?

Why Can't I Use Live Prices?

At first glance, it is not a problem to test a strategy or indicator on the real market. Moreover, the final testing of strategies and indicators always has to be done on the real prices.

However, during debugging or backtesting, you may need to run an indicator or strategy over one and the same market pattern several times to tune the logic (during debugging) or parameters (during backtesting). But since the real market is constantly changing, it assumes a new pattern every time you run an indicator or strategy. On the contrary, a price simulation allows to apply an indicator or strategy to one and the same market pattern as many times as you may need.

The real market may change fast, but it cannot be faster than time. If you want to test a strategy over a whole year, you will have to wait the whole year before you get the testing results.

The problem is easily resolved with the help of price simulation. You can "replay" any market history (historical data range) as many times as you need and every time it is done much faster than on the real market.

How Does It Work?

The price simulator takes a currency history (for example, Price Data files) and simulates a certain number of ticks for every bar. The number is determined by the timeframe sizes of bars. For a one-minute timeframe, eight ticks per bar are simulated. The number of ticks for other timeframes must be multiple of the number of ticks per one-minute bar. For example, for a five-minute bar, 40 ticks are simulated, for a one-hour bar ̶ 480, for a one-day bar ̶ 11,520, for a one-week bar ̶ 57,600 (five trading days), and so on. The distribution of ticks along the span of a bar depends on whether it is a descending or ascending bar. In the case of descending bars, the tick curve first goes from the Open price to the High price, then it goes to the Low price, and finally, to the Close price. In the case of ascending bars, the tick curve first goes from the Open price to the Low price, then it goes to the High price, and finally, to the Close price.

Pricesim curves.png

Why Can't I Use Tick Histories?

You can, theoretically. In fact, up to 20,000 ticks per instrument are generated during a trading hour, and during one year ̶ 124,800,000 ticks per instrument. This causes a number of problems:

  • Even if you use the most compact storage format, a one-year history of every instrument will be 1.5Gb in size.
  • The simulation process will be 41 times longer. Testing of a typical strategy on a one-year data range will take approximately 30 minutes, which makes such tasks as optimization almost useless.
  • If you take a look at the movement of real prices within a one-minute timeframe, you will see that, in most of the cases, the market follows the patterns shown below:
Pricesim real.png

As you see, most of the fluctuations are as small as one-tenth of a pip and fit the spread size. Thus, it is impossible to use stop and limit orders set one pip off an open position or track the equity and balance values with the precision of 1/10th of a pip. Moreover, most of the clients do not receive all ticks anyway. In the worst case, when the Internet connection is very bad, clients may get just four ticks per minute: open, highest, lowest, and close ones.

We have tested a number of strategies a on a one-minute tick simulation and on a tick history. The difference of strategy testing results is absolutely negligible.

I can see only one case when a strategy testing on pseudo-random tick movements would be better than a strategy testing on a simulated curve. A self-educating prediction algorithm, for example, a neural network, may be clever enough to recognize these curves and be able to "predict" the price direction after the first three simulated ticks. But, once again, this is a pretty rare case. Moreover, the market simulation is an open API, thus, for such a tricky case, you can easily implement custom simulation and backtesting routines.

This Article in Other Languages

Language: English  • Español • Français • Русский • 中文 • ‪中文(繁體)‬
Personal tools