C.W.K.
Stream
Lesson 05 of 08 · published

Trading & Market Data

~12 min · app, trading, binary

Level 0Poller
0 XP0/60 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

High frequency demands binary

Crypto exchange feeds tick at 100+ updates/second per symbol. JSON at that rate eats bandwidth and CPU. Binary protocols (MessagePack, custom DataView, Protobuf) reduce frame size 60-80% and parse time even more. For a public homepage ticker, JSON is fine; for a trading desk subscribing to 500 symbols, binary is non-negotiable.

The hybrid pattern

Trading platforms split concerns: WebSocket for the high-frequency price + book stream (binary), REST for low-frequency commands like place-order, cancel-order, and account info. The same auth token works for both. The user pushes orders rarely; the market pushes data constantly.

Code

Server: binary-encoded price stream·python
import struct
import asyncio

async def stream_prices(websocket):
    while True:
        prices = await get_latest_prices()
        # 18 bytes per record:
        # symbol_id(uint16) + price(float64) + volume(float64)
        if not prices:
            await asyncio.sleep(0.1)
            continue
        out = bytearray()
        for sym_id, price, vol in prices:
            out += struct.pack('<Hdd', sym_id, price, vol)
        await websocket.send_bytes(bytes(out))
        await asyncio.sleep(0.1)  # 10 Hz
Client: decode binary stream·javascript
ws.binaryType = 'arraybuffer';

ws.onmessage = (e) => {
  const view = new DataView(e.data);
  const recordSize = 18;
  const count = e.data.byteLength / recordSize;
  for (let i = 0; i < count; i++) {
    const o = i * recordSize;
    const sym    = view.getUint16( o,      true);
    const price  = view.getFloat64(o + 2,  true);
    const volume = view.getFloat64(o + 10, true);
    updateTicker(sym, price, volume);
  }
};

External links

Exercise

Implement the binary price stream above with three fake symbols. Compare bytes-on-the-wire and parse time vs. the equivalent JSON for 1000 ticks. Report ratios. Then add a REST /orders endpoint that publishes a fill notification through the same WebSocket — preserve REST as the source of truth for orders.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.