Stock Exchange

Examples

Practical examples of using the Stock Exchange API

Trading Examples

Learn how to interact with the Stock Exchange API through practical examples.

Example 1: Placing a Buy Order

Let's place a limit buy order for 10 shares at $1000.

Send the Request

curl -X POST http://localhost:3000/order \
  -H "Content-Type: application/json" \
  -d '{
    "userID": 6,
    "type": "LIMIT",
    "bid": true,
    "size": 10,
    "price": 1000,
    "market": "INN"
  }'

Response

{
  "OrderID": 60464691
}

Your order has been placed successfully with ID 60464691.

Example 2: Placing a Sell Order

Now let's place a limit sell order.

curl -X POST http://localhost:3000/order \
  -H "Content-Type: application/json" \
  -d '{
    "userID": 7,
    "type": "LIMIT",
    "bid": false,
    "size": 5,
    "price": 1010,
    "market": "INN"
  }'

Example 3: Checking Your Orders

Retrieve all orders for user 6:

curl http://localhost:3000/order/6
{
  "orders": [
    {
      "ID": 60464691,
      "UserID": 6,
      "Price": 1000,
      "Size": 10,
      "Bid": true,
      "Timestamp": 1699887123000
    },
    {
      "ID": 85937264,
      "UserID": 6,
      "Price": 999,
      "Size": 44,
      "Bid": true,
      "Timestamp": 1699887125000
    }
  ]
}

Example 4: Getting Market Data

Best Ask Price

Get the lowest ask (sell) price:

curl http://localhost:3000/book/INN/ask

Response:

{
  "Bid": false,
  "Price": 990,
  "UserID": 8
}

Best Bid Price

Get the highest bid (buy) price:

curl http://localhost:3000/book/INN/bid

Response:

{
  "Bid": true,
  "Price": 970,
  "UserID": 8
}

The spread is the difference between the best ask and best bid. In this example: 990 - 970 = 20.

Example 5: Viewing Trade History

Get all executed trades for the INN market:

curl http://localhost:3000/trades/INN

Response:

{
  "trades": [
    {
      "price": 1000,
      "size": 100,
      "timestamp": 1699887120000,
      "bid": true
    },
    {
      "price": 1001,
      "size": 50,
      "timestamp": 1699887125000,
      "bid": false
    },
    {
      "price": 999,
      "size": 75,
      "timestamp": 1699887130000,
      "bid": true
    }
  ]
}

Example 6: Market Order

Place a market order that executes immediately at the best available price:

curl -X POST http://localhost:3000/order \
  -H "Content-Type: application/json" \
  -d '{
    "userID": 9,
    "type": "MARKET",
    "bid": true,
    "size": 20,
    "market": "INN"
  }'

Market orders execute immediately at the current market price. They do not require a price field.

Testing Scenario

Here's a complete trading scenario to test the system:

Start with an Empty Book

Ensure the server is running fresh.

Market Maker Provides Liquidity

The market maker automatically places orders to provide liquidity.

Place Multiple Orders

# Buy order at 995
curl -X POST http://localhost:3000/order \
  -H "Content-Type: application/json" \
  -d '{"userID": 1, "type": "LIMIT", "bid": true, "size": 50, "price": 995, "market": "INN"}'

# Sell order at 1005
curl -X POST http://localhost:3000/order \
  -H "Content-Type: application/json" \
  -d '{"userID": 2, "type": "LIMIT", "bid": false, "size": 50, "price": 1005, "market": "INN"}'

Execute a Market Order

# This will match against the best ask (1005)
curl -X POST http://localhost:3000/order \
  -H "Content-Type: application/json" \
  -d '{"userID": 3, "type": "MARKET", "bid": true, "size": 25, "market": "INN"}'

Check the Results

View trades to see the execution:

curl http://localhost:3000/trades/INN

Next Steps

On this page