Class Project FINM32500 Matching engine
Description
A trade matching engine is the core software and hardware component of an electronic exchange. It matches up bids and offers to complete trades. Matching engines use one or several algorithms to allocate trades among competing bids and offers at the same price.
The most commonly used algorithm is time price priority, meaning those bids and offers entered into the match engine first have priority over similar bids or offers that were subsequently entered into the system. The principle of pricetime priority refers to how orders are prioritized for execution. Orders are first ranked according to their price; orders of the same price are then ranked depending on when they were entered.
Unlike the prorata model, in which orders go to specialists first, in a pricetime priority model, dealers have few special privileges
In your quanttrader life, you will need to learn pretty well how an electronic exchange works. The matching engine we are going to create in this project is basic.
Matching engines support different order types, such as a limit order or market order. Matching engines may have unique APIs, or use standard ones such as FIX APIs.
Part I: Conception 5pts
After some research, you will explain the way the matching engine will handle limit order, market order, IOC orders. You will give a writeup on the way you will conceive this matchingengine.
You will also explain how to represent the orders I would recommend a dictionary with the regular attributes of an order but do not forget the timestamp, it is important to know when orders arrive in the matching engine.
Part II: Creation of the class MatchingEngine 20pts
You will create the class MatchingEngine capable of handling incoming orders and matching orders. You will need to handle 3 types of orders:
Limit order
Market order
Immediate or Cancel
I would recommend writing the following functions:
def handleorderorder, function in charge of handling any types of orders in the matching engine
def handlelimitorderorder, def handlemarketorderorder, def handleiocorderorder
This class will have a container of your choice for bids and offers.
Please consider that a limit order A of a quantity QA can be matched with another order B of a quantity QB:
if QAQB, B will be partially filled, and the amount left for A will be QBQA
if QAQB, comparable to prior case
if QAQB, A and B will be fullyfilled
To succeed this part, you will need to show evidence that the different orders and different cases work. For that you are required to use unit testing.
Coming from this library:
https:docs.python.org3.6libraryunittest.html
Part III: Exchange Simulator 20pts
Once your MatchineEngine class is created, you will create another file that you will call ExchangeSimulator.py.
You will need to use your ExchangeSimulator. This simulation will have 100 threads representing traders. These traders will place randomly 3 types of orders IOC, Market Order, Limit Order. Every order should have a different order id.
A trader can:
place a new order
amend the volume of an existing order the amendment is always reducing the quantity
cancel an existing order
calculate the PL and the position each time they trade
The trader cannot create a new order if there is an existing order on the market.
This order will be sent to a deque
The communication between the thread and the matching engine will happen through a deque. This is an example how to use a deque, a client certainly a Trader and a server matching engine:
import time gbllockthreading.Lock
class clientthreading.Thread: def initself:
super.init def runself:
for i in range10:
while True:
printcheck if response from server
import threading
from collections import deque
clienttoserverdeque
servertoclientdeque
clienttoserver.appendleftincrement
clienttoserver.appendleftgetbalance
if not lenservertoclient: continue
responsefromserverservertoclient.pop printResult:dnresponsefromserver
class serverthreading.Thread: def initself:
super.init
self.balance0 def incrementself:
self.balance1 def decrementself:
self.balance1
def additionself,num1,num2:
return num1num2
def subtractionself, num1, num2:
return num1num2
def handlerequestfromclientself,request:
if requestincrement:
return self.increment elif request decrement: return self.decrement elif requestaddition:
return self.addition elif requestsubtraction:
return self.subtraction elif requestgetbalance:
return self.balance
def runself:first function to called once the thread is started
while True:
printcheck if question from client if not lenclienttoserver:
continue
requestfromclientclienttoserver.pop
returnfunctionself.
handlerequestfromclientrequestfromclient
if returnfunction is not None: servertoclient.appendleftreturnfunction
client1client
server1server
client1.start
server1.start
client1.join
server1.join
When a matching engine matches 2 orders bid and offer, the two traders will need to be informed of the trade.
Each trader will start with 1,000,000, you will draw the chart of how the cash evolves. A trader will stop trading when its cash is 0.
Part IV: Real Exchange 10 pts
You will now need to use a real TCP server for your matching engine, the traders will be TCP clients.
Reviews
There are no reviews yet.