A stock exchange, securities exchange, or bourse is an exchange where stockbrokers and traders can buy and sell securities, such as shares of stock, bonds and other financial instruments.
The basic function of an exchange is to facilitate the matching of buyers and sellers efficiently.
Tools used:
- JDK 8
- QuickFIX/J
- ActiveMQ
- MongoDB
- MySQL
- Maven
- JUnit 5, Mockito
- IntelliJ IDE
Here we are going to build a mini stock exchange simulator with the following components or services:
Random generation of market data and orders
- Market data feed: For a given stock, we will create random bid-ask prices and sizes and then publish
them onto a topic every
n
seconds. - Orders feed: For a given stock, we will create random market and limit orders and then publish them
onto a queue every
n
seconds.
Market orders are always filled because it will execute at whatever the ask/bid prices are available in market data.
Limit orders will be filled only if their limit price and quantity match the market data.
FIX engines using QuickFIX/J
The Financial Information eXchange (FIX) protocol is a messaging standard developed specifically for the real-time electronic exchange of securities transactions.
FIX is a public-domain specification owned and maintained by FIX Protocol Ltd.
We will be using open-source QuickFIX/J to build our FIX engines:
- FIX acceptor: listens on the market data and orders queues and provides order execution by a matching engine.
- FIX initiator: listens to the orders queue and forwards them to the FIX acceptor. If the acceptor replies with filled orders, then it publishes them on a topic.
Databases
We will be using MongoDB and MySQL databases to store executed and rejected orders.
Monitoring UI
Display live executions, rejections, order statistics and market data from the data stored in the MongoDB.
The whole simulator can be started in the following sequence:
ExchangeSimulatorServicesRunner
starts all the support services, namely:MarketDataProducer
: it builds random ask and bid prices for a selected range of symbols and sends them to a queue, every X seconds.OrdersProducer
: it builds random buy/sell market and limit orders and sends them to a queue, every X seconds.OrdersBackEndStore
: it subscribes to theExecutedOrdersTopic
to receive fully filled orders and stores them to MySQL and MongoDB databases.ExchangeSimulatorStatsRunner
: statistics service, which builds various statistics on aggregated data and prints
ExchangeSimulatorServer
starts the FIX acceptor and matching engine.OrderRouter
starts the OMS and FIX initiator.ExchangeSimulatorUI
starts the monitor UI.