Objective: Build an efficient tool to compute aggregate market data from raw trades.

Background: Among many other things, Messari is tasked with keeping track of asset prices, volume figures, etc. for as many crypto assets and crypto markets as possible. Fundamentally, the tasks at hand require a high-performance codebase. This exercise is designed to assess how you handle a problem that makes you think about low-level performance optimizations in the context of crypto trade data.

Details: As part of this exercise prompt, you will be provided with a binary that writes ten million trade objects as JSON to stdout, and then exits. It writes one object per line, as quickly as possible. Your objective is to write a tool that parses each trade as it comes in and computes various aggregate metrics from the provided data, completing the set of ten million trades in as little time as possible.

Your tool should accept input from stdin so output from the provided binary can be piped into your tool using a terminal window.

As an example, three consecutive lines of output would be of the form below:

{"id":121509,"market":5773,"price":1.234,"volume":1234.56,"is_buy":true}
{"id":121510,"market":5774,"price":2.345,"volume":2345.67,"is_buy":false}
{"id":121511,"market":5775,"price":3.456,"volume":3456.78,"is_buy":true}

Where:

Your app should continuously compute and keep track of the following as new trades come in:

  1. Total volume per market
  2. Mean price per market
  3. Mean volume per market
  4. Volume-weighted average price per market
  5. Percentage buy orders per market