Building a trading system in Python

In this section, we will describe how to create a trading system from scratch. We will use Python to code this trading system but the approach is general enough to be transferred to other languages. We will talk about the design and the best software engineering practice. The system we will create will have the bare minimum components to trade and you may want to extend it after this first initial implementation.

Python is an object-oriented language. We will encapsulate the main functionalities of the trading system into Python objects. We will have these components communicate through channels. We will simplify the functional components by limiting this first implementation to five main components. We will code these five components into five different files. We will associate unit tests to all these components:

  • 1-py: We will reproduce the behavior of liquidity providers. In this example, it sends price updates (orders).
  • 2-py: To simplify the design, we are removing the gateway and we will plug the liquidity provider directly to the order book manager. This component will be in charge of building a book.
  • 3-py: This file contains the trading strategy code.
  • 4-py: This contains the code for the order manager.
  • 5-py: This replicates the behavior of a market:

We observe from the preceding diagram that there are links between all the components. Every link is a unidirectional communication channel. In Python, the data structure we choose is a deque from the collections package.

We use two methods of the deque data structure:

  • - push: This inserts an element into the channel.
  • - popleft: This removes an element from the channel.

We will first describe the implementation of all these components one by one. We will describe the public methods that will be used to use them. When you start designing a class, you first need to know what this class is supposed to do. You will design the testing environment that will be able verify the component behavior.

The orders and the order updates will be represented by a simple Python dictionary. Let's have a look at the code:

ord = {
'id': self.order_id,
'price': price,
'quantity': quantity,
'side': side,
'action': action
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.252.8