ismailfer / exchange-simulator

Exchange Simulator using Spring Boot, RestAPI, and Websockets for Order Placement, Order and Trade events, and Market Data events

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exchange Simulator - Spring Boot, RestAPI, Websockets Stream

github.com/ismailfer/exchange-simulator

This is a simulated exchange that accepts market and limit order types.

RestAPI is used to submit order requests, get order status, get static data, and market data.

Ů‹Websocket stream is used to submit order requests, receive order and trade events. This is usually much faster than using a RestAPI

Market data is also fed via websockets

Objectives

  • Used for testing purposes of algorithmic trading systems

Main features:

  • Rest API for order placement, status request, market data requests
  • Native websockets API using Jetty Server
  • Orders are placed via a websocket connection
  • Order and trade events are fed through the websocket connection
  • Market data is fed via websockets
  • Market order type
  • Limit order type
  • Any instrument is supported
  • Order book can be cleared via a command in RestAPI or websocket

Potential Improvements

  • Simulate a real exchange by subscribing to market data from a production environment (i.e. Binance)

    • Order book will be built based on the market data events
    • Matching will occur on this order book
    • This is useful to test a trading strategy on a real market data environment
  • Persistence of orders

  • FIX protocol support

Architecture

  • Spring Boot framework
  • Rest API
  • Native WebSockets, using Jetty Server embedded in the spring boot app
  • JSP/web3 to show market data, and orders in the simulator
  • Jackson json marshalling/unmarshalling
  • Reactive programming architecture
  • Concurrent / Multi-threading environment
  • Zero Garbage Collection
  • Java 17. However the code base can also run in Java 11 and above
  • Maven build

RestAPI and Websockets API - Postman Collections

API URLs

URL for Order Placement Rest API:

URL for Order Cancellation Rest API:

URL for Order Stream Websocket:

  • ws://localhost:9081/api/v1/order

URL for Top Of Book Stream Websocket:

  • ws://localhost:9081/api/v1/tob

URL for Order Book Websocket:

  • ws://localhost:9081/api/v1/orderBook

API Test Cases

Test cases of RestAPIs and Websocket APIs are found in the following Postman collection

Building the project

The build process will produce a war file (instead of a jar file); because there is an embedded jetty server; and JSP pages within the project.

To build the project; run the command:

mvn clean package

Running the Application

To run the application from the war file:

java -jar target\exchange-simulator-1.0.war

To run the application with ZeroG garbage collector

java -XX:+UseZGC -Xmx2g -jar target\exchange-simulator-1.0.war

The app doesn't require much memory; unless you want to load it with thousands of orders

Deployments

  • Docker
  • Kubernetes

Docker

This application is dockerized with Dockerfile

Latest docker image is available at my dockerhub; built on Alpine linux distribution; and open jdk17, running on port 9080 and 9081

To pull docker iamge from docker hub:

docker pull ismailfer/exchange-simulator

To build a fresh docker image:

docker build --tag exchange-simulator:1.0 .

To run the docker image:

docker run --name exchange-simulator -d -p 9080:9080 -p 9081:9081 exchange-simulator:1.0

To tail the logs on the running docker image:

docker logs -f exchange-simulator

About

Exchange Simulator using Spring Boot, RestAPI, and Websockets for Order Placement, Order and Trade events, and Market Data events


Languages

Language:Java 87.2%Language:CSS 10.0%Language:JavaScript 2.6%Language:Dockerfile 0.1%