0% found this document useful (0 votes)
35 views9 pages

Python Order Book Simulation Framework

The document discusses the development of an Order Book simulator in Python, aimed at enabling practical experiments in market microstructure and algorithmic trading strategies. It highlights the challenges of obtaining quality order book data and proposes a customizable framework for simulating trader behavior and order flow. Additionally, the author shares insights on visualizing order flow and the impact of informed traders on market dynamics, with plans for future enhancements to the simulator.

Uploaded by

As Win
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views9 pages

Python Order Book Simulation Framework

The document discusses the development of an Order Book simulator in Python, aimed at enabling practical experiments in market microstructure and algorithmic trading strategies. It highlights the challenges of obtaining quality order book data and proposes a customizable framework for simulating trader behavior and order flow. Additionally, the author shares insights on visualizing order flow and the impact of informed traders on market dynamics, with plans for future enhancements to the simulator.

Uploaded by

As Win
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

[Link]

com/technological-singularity/distribution-of-the-order-flow-in-python-
d7ba059dbf13

An Order Book simulator in Python

Run experiments and test theories

Luigi Battistoni

Follow

4 min read

4 days ago

23

Introduction — My motivation

If you followed my last year’s work, you noticed that I covered a lot of topics regarding the
Order Book. In particular, I covered some Market Making topics taken from
Hasbrouck’s Empirical Market Microstructure. Moreover, I covered some parts of the trader’s
point of view getting inspiration from Cartea’s Algorithmic and High-Frequency Trading.

While I was studying the theory regarding market microstructure, I had the urge to run some
practical experiments. Getting quality data about the Order Book is not an easy task for an
individual: data can be really expensive, it contains a lot of information and has a huge size.
I found crypto exchanges to be the most useful resources for a student of this field, since they
provide the order book snapshots for free (both historical and in real time!). Please reach me
out here or on linkedin if you want to know more about where to download this kind of data,
I’ll be glad to help!

Free Order Book snapshots are really convenient, but two big issues arise:

1. data is fast and heavy: the granularity of the snapshots could be up to 10 milliseconds.
Cryptocurrencies are exchanged continuously during the day, this means 8,640,000
snapshots per day! Building an historical dataset can be computationally expensive;

2. as everything in finance, we are just looking at one realization of the world. We have to
infer statistical properties from a sequence of realized values. We can’t collect more
values, as we are working with timeseries.

These two issues motivated me to build a fast and reliable framework in which you can run
experiments. This framework is highly customizable and will allow you to make your own
hypothesis about the Traders, Market Makers and the general behavior of the Order Book, in
order to develop algorithmic trading strategies.

In this article, I will explain to you how to use the framework, what are its limitations and what
you can do to improve it. In the next months, I will implement new features to overcome the
limitations of the very first building blocks that I am proposing you. Moreover, I will myself run
experiments using this framework, getting inspiration from books and papers.

I will link the GitHub repository containing the framework’s code at the end of the article, please
feel free to give me a feedback about it!

The framework

The framework is quite easy to use. First of all, you have to instantiate the Order Book using
the OrderBook class. Then, you have to instantiate traders using the Trader class. Every trader
submits orders using the Order class, through the submit_order_to_order_book method.

Orders are then added to the book using the add_order_to_the_order_book method. If the
order results in a trade, then a Trade object is created. At every timestep you can retrieve the
status of the order book and other various quantities, like the last trade price, volumes,
etcetera.

An example of a simple experiment that you will find in my order_flow_simulation notebook is


the following. Notice that here I didn’t use any model, just a simple uniform distribution to
show how the code works:
Order Flow representation

To understand the meaning of this plot, refer to my article here.

In further iterations, I will implement some more features, for example:

 each trader will have an id

 each order will have an id

 each trader will know what orders he submitted to the book, in this way he can cancel or
modify them

 track the trader’s PnL

 each trader will have a systematical trading strategy or a utility function

Despite this being a basic framework, it is capable of running some pretty complex experiments.
In my next article, I will explore what happens when a Market Maker tries to keep a Bid-Ask
spread fixed and an informed Trader hides among uninformed ones.

You will find the notebook with the inputs and the code to run a simulation using this
framework here. You will find the repository with the whole code here. Feel free to reach me for
any doubt

A brilliant way to represent the Order Flow in Python

The complex made simple

Luigi Battistoni

Follow

5 min read
·

2 days ago

23

Introduction

In this article I will show you an insightful way to plot the Order Flow in Python. I learned about
this method while reading Algorithmic and High-Frequency Trading, Álvaro Cartea (2015) and I
was immediately fascinated by it. This representation provides an intuitive description of
the evolution of the Order Book and of the change in price.

I will provide the Python code used in this article. Moreover, at the end of the article, I will link
my GitHub repository and other articles that you might find interesting.

Before deep diving into the representation method, let us ask a simple question.

What is an Order Flow and why should you care about it?

An Order Book is a list of buy and sell orders for a specific financial instrument, organized by
price level. It is used in trading markets to keep track of the demand and supply of assets in real-
time. The most important information contained in the Book is the following:

 Bid Prices: prices buyers are willing to pay for the asset. Bid prices are listed in
descending order, with the highest bid (the best bid) at the top.

 Ask Prices: prices sellers are willing to accept for the asset. Ask prices are listed in
ascending order, with the lowest ask (the best ask) at the top.

 Order Sizes: for each bid and ask price, the Order Book shows the quantity of the asset
that buyers or sellers are willing to trade at that price.

As you can imagine, the Order Book provides insight into the current market state, allowing
participants to make informed trading decisions (i.e. the balance between the bid and ask sides
of the order book might indicate the market sentiment and liquidity of the asset).
The Order Flow is the sequence of Order Book snapshots. It can be used to understand how the
market dynamic changes in time and where the security price might be in the future. A deep
understanding of both the Order Book and the Order Flow is crucial to approach high frequency
trading.

The usual representation of the Order Book

If you ever stumbled upon an Order Book, this is the usual representation that brokers and
other sources use:

source: [Link]
trading/

You have the Buy orders (Bid) in green and the Sell orders (Ask) in red. The level prices are on
the x axis, while the volumes are on the y axis. The plot represents the cumulative volumes at
each price level for Bid and Ask.

The white space between the highest Bid and the lowest Ask is the Bid-Ask Spread. The dotted
line represents the Mid Price, or the middle of the Bid-Ask Spread. Some sources use this price
as the theoretical efficient price of the security.

This is actually a quite understandable representation, you can see that there is an imbalance
between demand and supply and you can use this information to form an idea on where the
future trades will be. The problem is that this is a snapshot of the Order Flow. This is a single
millisecond snapshot and the Book changes very fast. This information will be obsolete in a very
short time.

So what about the representation of the Order Flow? What are its usual representations in
common brokers and financial sites?

You can, in general, find two kind of representations. The order flow chart:

source: [Link]

As before, you have Bid and Ask orders, with Volumes. At each time step a new snapshot is
added to the graph. You maybe will find this plot useful, I don’t.

Another used plot is the heatmap:


source: [Link]
visualization/

You have Bid, Asks and their Volumes represented in balls of different sizes. This is less
confusing than the previous plot, but still quite complex to decode, expecially in real time
trading.

An alternative representation

Now let me show you an alternative representation (the one proposed by Cartea):

Here we have, as usual, the time on the x axis and the prices on the y. The time can represent
anything, we can assume that these are snapshots of the Order Book taken every millisecond.

In red we have the Bid, with each different price level. In blue we have the Ask. The bar plot in
each level represents the Volumes in each level. The yellow area between the highest Bid and
the lowest Ask is the Bid-Ask Spread.

The orange line represents the Price, that is the price at which the last transaction happened.
Each transaction is represented with a black circle, red if it is a sell transaction, green if it is a
buy. The wider the circle the greater the volumes in the transaction.

In my opinion, this representation is far more useful than the previous ones. You can visualize
how the spread changes in time, how the price evolves and where the buy or sell pressure is.

Imagine to look at the three different representations in real time, with the plot updating and
while new values are added. What representation would you find more useful? What do you
think about the third one? Do you have any suggestion to further improve this plot?

You will find the notebook with the inputs and the code to plot the graph here. Follow the
instructions in the notebook to put the Order Book snapshots in the right format, feel free to
reach me for any doubt!

Using the Order Book depth to unveil informed trading

A Python experimental approach using the CMSW Framework


Luigi Battistoni

Follow

5 min read

Aug 5, 2024

Introduction

In my latest article of my market microstucture¹ series, I introduced the CMSW model²: a


statistical framework useful to study the behavior of buyers and sellers in a Limit Order Book. In
particular, the model is capable of giving quantitative measures that support the decision of
whether to issue a limit order or a market order.

In this piece, I show the results of some experiments I ran using the Order Book simulator that I
built in this article. The assumptions for the simulations are that a fraction of the buyers is
informed about the future price of the security and that there is no market making.

How is the (uninformed) agents’ expectation about the future price of the security related to
the depth of the book (i.e. the total volumes of the limit orders)? What if we change the fraction
of informed traders?

In my latest article we already tested the theory that in absence of informed traders, the Bid-
Ask spread dynamics is determined by the agents’ expectation about the intraday volatility.
Moreover, we found that if we introduce informed traders, they completely dominate the
market in their favor. This article will support this theory through other simulations.

One of the conclusions of these tests is that we need a layer of market makers (or liquidity
providers) that try to contrast both the wild effect of intraday expected volatility and the
oligopoly of informed traders. I will expand on this over my next articles, extending my Order
Book simulator to support some agents that behave like market makers.

Analysis

You can find the notebooks with Python code used for this analysis here and here. Feel free to
reach me for any questions!

The goal of this section is to understand the relation between the fraction of informed traders,
the expectation on intraday volatility and the depth of the order book.

Remember that, in the CMSW framework, our agents can either be buyers or sellers. They
decide what to do (don’t trade, place a limit order or place a market order) by picking the action
that maximizes their expected utility function. We assumed that only buyers can be informed.

Each trader decision is guided by his expectation of the future volatility of the security, and
hence the uncertainty that they picked the right direction. Informed traders know in advance
the future value of the security, so their uncertainty is zero.

Let us first plot the average volume depth for different fractions of informed traders. Moreover,
we generate a scatter plot for four levels of expected payoff standard deviation:

I added an OLS regression line as well, in order to highlight the positive correlation between the
variables. In particular, we confirm our thesis that the presence of informed traders make the
order book deeper. Since we ran all the simulations for 1000 time steps, this means that all
things equal, the presence of more informed traders generate less transactions and volumes
stack up in the book, without any matching order. This is quite simple to imagine, as they are
not going to buy limit sells with a price higher than the final payoff of the security.

Let us now study the effect of payoff expected std dev on the order book depth. Here we have
correlations that are very weak and, depending on the initial conditions of the system, are
either not statistically significant or slightly significant. The interesting patterns here are the
different levels associated to different fractions of informed traders, i.e. the greater the fraction
the deeper the book (this is consistent with the previous result). Moreover, we see that the
higher the fraction of informed traders, the higher the order book depth variance. Again, this
happens because the informed traders are free to manipulate the prices and the volumes, in
absence of a liquidity provider.
Another interesting plot is the following: on the x axis we have the payoff expected std dev,
while on the y we have the average number of maximum price levels in the book, i.e. if we have
a limit order at 10, 10.01, 10.02 we have 3 price levels.

A quadratic fit seems to be more suitable than a linear regression. For extreme payoff std dev
values this is very noticeable, but the financial sense might get lost. A more interesting fact is
that the system behaves in a similar manner regardless of the fraction of informed traders. The
number of price levels depend just on the payoff expected std dev.

Conclusions and further steps

So, this is the analysis. A question might come naturally:

Is this useful in any real market situations?

I would say that the best insight that comes from this model is that we can identify the
presence of informed traders looking at both the sequence of the Bid-Ask Spread and at the
order book depth.

Moreover, we can identify the direction in which the informed agents are trading looking
at “liquidity walls” : price levels where a lot of market orders are filled.

In a further piece I will explore the behavior of the system when we add a market maker. I will
make the market maker guess the fraction (or the presence) of informed traders and
consequently react. Then the informed traders will change their strategy to hide from the
market makers and an equilibrium will be reached.

As usual, my final goal is to make a model where an individual can become aware of the
presence of informed trading, even when a market maker is acting, and understand the
direction of the trend.

If you are interested in this kind of topics, please consider checking my articles, as I am
constantly studying and covering ways to extract value and insights from the Order Book.

You might also like