# JSON Output (/json-output)



Market Lab has two priorities:

* good human terminal output
* compact machine-readable output

## Market Catalog JSON [#market-catalog-json]

The `markets` command uses a dedicated `--json` flag:

```bash
mlab markets --exchange binance --json
mlab markets --exchange binancef --json
mlab markets --exchange bulkf --json
mlab markets --exchange hyperliquidf --json
```

Exchange catalog JSON contains:

* `exchange`
* `name`
* `marketType` (`spot` or `futures`)
* `markets`

Requesting one symbol returns that market object directly:

```bash
mlab markets --exchange bulkf --symbol BTC --json
```

Each market contains symbol mapping, status, precision, tick and lot sizes, minimum notional, maximum leverage, order types, and time-in-force values. This output is the installed rules snapshot, not live market data.

## Default JSON [#default-json]

Study JSON is intentionally compact. Built-in strategy submission returns a persistent job record, while strategy worker activity is written as JSONL job output.

That means the default output includes only the fields an agent or script usually needs.

Script JSON follows the same rule: default output stays compact, while runtime reports can be inspected separately with `script runs list` and `script runs show`.

## Verbose JSON [#verbose-json]

Add `--verbose` to include expanded context.

Examples:

```bash
mlab study spread --provider mmt --exchange bybitf --symbol BTC --depth 20 --output json --verbose
```

## Stream JSONL [#stream-jsonl]

Use `jsonl` for stream-oriented commands.

Examples:

```bash
mlab source candles --provider mmt --exchange binancef --symbol BTC --timeframe 60 --stream --output jsonl
```

## Execution JSON [#execution-json]

Execution commands support `terminal`, `json`, and `jsonl`. CSV and Parquet are rejected.

A dry-run trade emits the normalized `TradePlan` directly:

```bash
mlab trade long BTC --margin 10 --dry-run --output json
```

The plan contains:

* creation timestamp and venue
* account, internal symbol, and venue symbol
* direction and order side
* order kind and time in force
* requested margin or exact size, plus normalized size
* limit price or market reference price
* estimated margin and exposure, leverage, and reduce-only state
* optional native stop-loss and take-profit prices
* projected liquidation price (`null` when the selected venue cannot provide a reliable pre-trade portfolio simulation)

A submitted trade emits both the reviewed plan and venue receipt:

```bash
mlab trade long BTC --margin 10 --yes --output json
```

```json
{
  "plan": {},
  "receipt": {
    "venue": "bulkf",
    "account": "...",
    "order_id": "...",
    "status": "...",
    "terminal": false,
    "submitted_at_ms": 1780000000000,
    "raw_status": {}
  },
  "post_trade_position": {}
}
```

The same plan/result behavior applies to `cancel`. `positions`, `orders`, and `fills` emit normalized account records.

Hyperliquid account snapshots keep outcome holdings separate from perpetual positions and ordinary spot balances. Each outcome holding includes its outcome ID, side, canonical `OUTCOME_ID:SIDE` symbol, Hyperliquid token identity, quantity, and readable question and outcome metadata.

Structured live execution cannot prompt, so it requires `--yes`. Use `--dry-run` first.

## Daemon JSON and Events [#daemon-json-and-events]

Inspect runtime state:

```bash
mlab daemon status --output json
```

Status contains:

* runtime protocol `version`
* `running` and `pid`
* `started_at_ms`
* `account_stream_connected`
* `last_account_event_ms` and `last_recovery_ms`
* `last_error`
* `tracked_orders`
* `script_jobs`
* `strategy_jobs`

Read the lifecycle journal as a JSON array or JSONL stream:

```bash
mlab daemon events --limit 20 --output json
mlab daemon events --limit 20 --output jsonl
```

Current event names are:

* `order_submitted`
* `order_cancelled`
* `order_tracking_started`
* `order_status`
* `account_ws`
* `account_recovery_fill`

Every event has `ts_ms` and `event`. Submission events add `plan` and `receipt`; tracking events add normalized order state; account events carry the selected venue's raw account payload.

## Study Shape [#study-shape]

Default compact study JSON contains:

* `type`
* `version`
* `provider`
* `exchange`
* `symbol`
* `ts_ms`
* `stream`
* `metrics`

Verbose study JSON also includes:

* `inputs`
* `meta`

## Strategy Shape [#strategy-shape]

`strategy run` deploys a detached native job. Structured live execution requires `--yes`:

```bash
mlab strategy run twap BTC \
  --side buy \
  --margin 100 \
  --duration 300 \
  --interval 60 \
  --yes \
  --output json
```

The returned record contains:

* job `id` and `status`
* worker `pid`
* the tagged strategy definition and immutable TWAP configuration
* symbol, side, target margin and exposure, normalized total size, duration, interval, leverage, and reduce-only state
* created, started, stopped, and heartbeat timestamps
* last worker error

List and inspect strategy jobs:

```bash
mlab strategy jobs --output json
mlab strategy status <JOB_ID> --output json
```

Worker output is stored as JSONL and includes `strategy.plan`, `strategy.child_order`, `strategy.run.finished`, and `strategy.run.failed` records:

```bash
mlab strategy logs <JOB_ID> --follow --output jsonl
```

Use `--dry-run --output json` to receive the normalized TWAP plan instead of creating a job.

## Script Backtest Shape [#script-backtest-shape]

Script hooks may return:

* `metrics`
* optional `meta`
* `null` when the hook only performs execution calls

Strategy behavior comes from `ctx.trade`, `ctx.order`, and `ctx.cancel`. The backtest runtime intercepts those calls with its simulator.

Default script backtest JSON contains:

* `type`
* `version`
* `provider`
* `exchange`
* `symbol`
* `ts_ms`
* `script`
* `summary`
* `performance`
* `params`

The summary includes submitted, pending, and cancelled simulated orders alongside closed trades and open positions.

Performance reports `capital_required` as the peak simulated margin in use. Leverage belongs to each `open-long` or `open-short` request rather than to the backtest command as a whole.

Verbose script backtest JSON also includes:

* `window` with the requested `from` and `to` timestamps
* `closed_trades`
* `open_positions`
* `latest_output` when the script explicitly returned non-empty diagnostics
* `meta`

Closed trades and open positions include their stable script `order_id` and `events_held` count when they came from `ctx.trade` or `ctx.order`. Open positions also include simulated `stop_loss_price` and `take_profit_price` when configured.

## Script Job Shape [#script-job-shape]

`script run` deploys a detached job and returns its job record rather than a stream result:

```bash
mlab script run ./strategy.js \
  --source btc@candles@bulkf:timeframe=5 \
  --venue bulkf \
  --output json
```

The record contains:

* job `id` and `status`
* worker `pid`
* immutable script definition and snapshot path
* provider, exchange, symbol, sources, params, and execution venue
* created, started, stopped, and heartbeat timestamps
* last worker error
* execution-event sequence and acknowledged cursor

List and inspect records:

```bash
mlab script jobs --output json
mlab script status <JOB_ID> --output json
```

Worker output is JSONL internally. Tail it directly:

```bash
mlab script logs <JOB_ID> --follow --output jsonl
```

An explicit non-empty `{ metrics, meta }` return from live `onData` is stored as `script.run.result`. Returning nothing, `null`, or `{}` stores no routine result. Execution delivery uses `script.execution.event`, optionally including non-empty hook diagnostics. A command-routing failure is appended as `script.execution.error`.

The `onExecution` event envelope uses camel-case fields:

* `seq`, `jobId`, `tsMs`, and `type`
* optional `orderId`, `key`, `venue`, `venueOrderId`, and `status`
* `terminal`
* provider payload in `data`

## Script Runtime Reports [#script-runtime-reports]

Script runtime reports are stored locally and can be viewed from the CLI.

```bash
mlab script runs list --output json
```

```bash
mlab script runs show <run-id> --output json
```

Runtime report JSON contains:

* `script`
* `command`
* `provider`
* `exchange`
* `symbol`
* `started_at_ms`
* `ended_at_ms`
* `duration_ms`
* `status`
* `limits`
* `runtime`
* `error`
