One Way to Trading over Orderbook Analisys

Sergey Tyurin
8 min readMar 11, 2020

Hi, everyone! 😃 Enjoy the article from practitioner quantitative researcher&trader.

How can a price predict itself? (screenshot taken from tradingview.com)

Is it possible to predict a future market price?

The statistics says that it becomes at least possible if analyzed data has non-zero correlation with price series. Primitive examples: bad changes in board of directors make stock price go down, the poor health of state leader negatively mirrors on state’s economics.

The predictions we make on events of this kind are reliable for days and weeks, maybe months. Not suitable for intraday trading, isn’t?

So, the short answer is: Yes. Let’s go further to understand long answer.

Some thoughts on technical analysis

Consider of technical analysis. The TA adherents use historical price movements wrapped in different graphical figures to predict future price movements. The overall technique is the same — do observations on one data to make predictions on another data.

😑Look, I`m not a TA adherent because I don’t know the real relations between historical and future price. Do they really take place?

The technical analysis graphical figure depends on many parameters, so it is not possible to definitely say “the figure works” only by naming it. Quantitative researcher should accept the statement “the figure works” only if all the parameters were accurately defined and statistically much experiments were provided.

When to enter?

We get used to consider signals as the trigger events to open position. This is straightforward and clear.

But when to quit?

There are different ways, but not really much. Among them are: close by time/price/profit/loss/figure etc.

Here I don’t want to talk about closing positions by stop loss or take profit events because they are definitely have no any direct or indirect relation to future price movements!

A signal has its own reliability, which is maximal when signal emerges and goes to zero with the time

But what should we do when signal becomes zero-reliable? Should we close-by-time or maybe reverse-by-time?

Look, we didn’t get any new signal 😐 so why should we do anything at all even if current signal became doubtful, why?

Let total reliability time be short, say minutes, but if predictions come frequently enough, minutes too, we can just relax and repose our trust on statistics. Here I’m saying about reversal trading system.

Reversal trading system. The algorithm

  1. When we get an initial signal we open position.
  2. Loop: when we get an opposite signal we reverse the position.

Signals must be of the same nature but opposite to each other. For example if you open position on cat goes left, you close the position when cat goes right.

Trading system backtesting

Let’s assume we have unprofitable trading system. It has worked for some time and has many trades closed. What if we will consider all Buy signals as Sell signals and vice versa? The answer: unprofitable system can be transformed to profitable one. But why I used “can be transformed”? Because of we have an opposite force called spread!

Paying spread and commissions are such as energy dissipation in thermodynamics. On every our trade we must spend a little portion of reserves. The profit you could get by mirroring unprofitable system can be easily leveled by them. It is really boring 😞, but we keep fighting 👊.

If you mirror your profitable system you’ll definitely get unprofitable one! Opposite statement doesn’t work by default because of commissions and spread.

The statement seems to be obvious, isn’t it? So why do we focusing on it? Because it is the simplest way of how to test signal generators and get rid of those which are unprofitable by the origin.

The simplest way to test signal generator

Orderbook, Level II, Market depth

We still didn’t answer on the question:

Where to get fast-changing data with non-zero correlation to price?

Of course you have already realized that this data is just a market depth that brokers usually provide you along with market price. An orderbook is built with limit orders of real people, companies etc. and changes really fast.

No matter how these orders are settled, manually or with help of robot, it is important that all of these participants are currently watching the market and making decisions on current situation, redistributing market depth shape.

Price drives by fundamental causes. But definitely the actual market depth shape is involved in process of future market price chart formation

I cannot forbid you to use technical analysis figures. Do it if you know what you do. Additionally I suggest you to:

✔️ use actual market depth shape patterns to predict future price movements

How to define shape?

Take a brief look at technical analysis figures, there are a lot of approaches of doing this.

The shape function should’t have more than two or maximum three parameters. Otherwise you won’t be able to test your system properly.

😮? Yes, and below I am going to show you why.

Example is needed: the simplest indicator on orderbook analysis

I won’t be amazing you with my creativity and will give you the simplest indicator I have. Let’s consider orderbook as array of Bids and array of Asks as follows. And let’s calculate the total sum of volumes:

The indicator G is just a ratio of difference between total ask volumes and total bid volumes to the whole orderbook volume.

If we build a graph of G(t) indicator using any available trading instrument with orderbook data it may look like:

The gaps and peaks of orange line curiously synchronized with price (blue line). How can we use this phenomenon?

If we integrate indicator with time we will get cumulative indicator that looks nearly like price.

Orange line looks like a modified price (blue line). But we definitely know that orange line hadn’t built suchlike

Hey! We have recreated a price with use of data that knows nothing about this price! 😉

It seems like the distribution of limit orders in orderbook influences the price somehow. Wait.. or price influences the market depth shape? Let theorists answer this question, we want to get benefit on implementing it in real life!

If we look at the indicator G(t) precisely we can notice that it strictly bound to range [-1;1]. The absolute value of numerator is always less than denominator while the expected value is zero. Ain’t it a best fit function for trading signal generator for reversal system?

So, the direct trading signal can be computed as follows:

Mirrored signal can be achieved by swapping Buy and Sell

What it T?

Just a parameter in our model. Remember, the model cannot have more then 2 (3 max) parameters. Single parameter — is ideal.

Seems to be to simple to be applicable in real life 😕! But we can easily verify it by backtesting 😃!

Brute force backtesting of parametrized models

The common plan:

  1. Collect orderbook market data. You need at least three months or about 60 days for Law of large numbers to start working.
  2. Create your own backtest model with help of highly efficient programming language. Model usually includes market emulator responsible for data delivery and portfolio manager responsible for accounting. Consider using parallel computing, because it give you n*CPU increase of performance.
  3. Define a parametrized signal generator. Define mirrored generator as well. Generator should not have more than two (three max.) parameters.
  4. Define ranges for all parameters in your model. Split up the ranges with 10–1000 steps depending on the accuracy you want to achieve.
  5. Provide backtests on every day on every parameter on every step of the parameter.
  6. Collect result data and provide additional custom analysis.

The full backtest I have provided while I was writing this article took about 1 hour on my 16CPU PC. The dataset included 62 days, model has one generator with single parameter and parameter range was split up with 100 steps. The analysis of results was trivial. I just watched the dependency of total result with parameter.

Two-parameter models are analyzed with 3d graphs instead of simple flat analysis. There is a big chance to get stuck with all that data, so.. 😉 just consider use of single parameter in your model.

Here is graphical representation of results. X-axis represents range of parameter used in brute force test. Green and red lines are the corresponding average day profits in percents. Yellow line is average day trades number. Remark: the percent was taken on a margin requirements of used instrument, so it is just a qualifier but not the prospective profit. However, the prospective day profit can be estimated by dividing source percent to 3á6.

Redline generator should be gone to trash, while greenline should be additionally analyzed. Parameter=0.52 seems to be the best

The graph clearly shows mirroring of two generators. The blue line, acting as mirror is just an average day spread we have paid by opening and closing positions.

Below is trading on 62 days length history with parameter=0.52. Monthly percent is estimated about 6%÷12% that is very very good! 😎

Day profit histogram shows wide distribution

While average day profit is 1.9% the distribution seems to be too wide to use single signal generator in real trading. You can easily get tens percent drawdown in several days.

Your investors should have nerves of steel to continue using that generator 😕. How could we calm them down?

The power of diversification

Consider of two independent events with the some probabilities. The probability of occurrence of these two events can be obtained by finding the product of their probabilities.

Normal distribution with different distribution width

Let’s assume we have several independent generators with same wide distribution. The superpositioned distribution of sum of them will not be the same as source but will getting narrower with every new generator. Big losses, as well as big profits, will occur rarely, making your stats more predictable.

This is why diversification is so widely used in trading and this is the way how to build you perfect flat fence with small but steady day profits columns.

✨Hope this reading was interesting for you! Contact me for going deeper.✨

--

--

Sergey Tyurin

Project manager and programmer, trader and quantitative researcher, AI enthusiast 🔥