The project for basic implementation of trade engine.
.
├── channel
│ └── order_channel.go // message queue for order
├── controller
| └── controller.go // to handle the http request
├── domain
| └── domain.go // define domain objects
├── engine
| ├── engine_test.go // for testing the trade engine
| └── engine.go // trade engine for matching the sell order and buy order
├── repository
| └── repository.go // repository for orders, trades
├── util
| └── util.go // utillities
├── main.go // for startup the application
...
- Client send a request to POST /api/orders
- Request handle by controller and controller will send a message to message queue
- The listner receive the message and call the trade engine to create an order
- Trade engine will try to match an order with sells or buys depends on order type
- A trade would be created when order matched.
The two scenarios here, 1) an buy order try to match the sells or, 2) a sell order try to match the buys.
- Request a buy order
- Loop over the sells
- Create a trade when both price and quantity matched on buy and sell
- Request a buy order price = 100, quantity = 4
- There are 3 sells with price = 100 in the sells list, and the quantities are 1, 1, 2
- Loop over the sells
- Three trade would be created for the buy order
- Request a buy order
- Loop over the sells
- No matched found then add the buy order to buy order list
Endpoint | Response | Request |
---|---|---|
POST /api/orders | order object | order object |
GET /api/buys | list of orders | N/A |
GET /api/sells | list of sells | N/A |
GET /api/trades | list of trades | N/A |
# create an order
curl --location --request POST 'http://localhost:8080/api/orders' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA==' \
--data-raw '{
"orderType": "sell",
"priceType": "limit",
"price": 20,
"quantity": 3
}'
# list buys
curl --location --request GET 'http://localhost:8080/api/buys' \
--header 'Accept: application/json' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA=='
# list sells
curl --location --request GET 'http://localhost:8080/api/sells' \
--header 'Accept: application/json' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA=='
# list trades
curl --location --request GET 'http://localhost:8080/api/trades' \
--header 'Accept: application/json' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA=='