Skip to content

Trade Ledger

Hydra includes a built-in trade ledger system that automatically logs every fill event to an append-only file. This provides a durable audit trail of all trading activity, separate from the high-volume event logs.

The Trade Ledger captures essential details for every trade, including execution price, size, fees, and the specific signal or reason that triggered the trade. It is designed for:

  • Auditability: Maintain a permanent record of all live and paper trades.
  • Performance Analysis: Calculate P&L, volume, and fee statistics.
  • Strategy Refinement: Analyze the “edge” and reference prices at the time of execution.

The ledger is integrated with the system’s EventBus. Whenever an OrderFill event occurs, the TradeSubscriber automatically enriches the data and appends it to the appropriate ledger file.

Ledger files are stored in JSON Lines (.jsonl) format in the data/ledger/ directory:

ModePath
Paperdata/ledger/trades-paper.jsonl
Livedata/ledger/trades-live.jsonl
Replaydata/ledger/trades-replay.jsonl

You can view and analyze your trade history using the hydra trades command.

To view all trades from the paper trading ledger:

Terminal window
hydra trades

To view live trades:

Terminal window
hydra trades --mode live

You can filter the output by market, side, or limit the number of results:

Terminal window
# Show only the last 10 trades
hydra trades -n 10
# Show trades for a specific market
hydra trades --market 0x...
# Show only sell orders
hydra trades --side SELL

To see aggregated performance metrics instead of individual trades, use the --summary flag:

Terminal window
hydra trades --summary

This provides:

  • Total trade count (buys vs. sells)
  • Total volume in USDC
  • Total fees paid
  • Net realized P&L
  • Breakdown by market (trade count, volume, net position)

Each entry in the .jsonl file is a TradeRecord object. If you need to perform custom analysis, you can export the ledger as JSON:

Terminal window
hydra trades --json > my_trades.json
FieldTypeDescription
tradeIdstringUnique identifier for the fill.
marketIdstringThe ID of the market traded.
sidestringBUY or SELL.
tokenstringUP or DOWN.
pricenumberThe execution price.
sizenumberQuantity of tokens filled.
valueUSDCnumberTotal value (price * size).
feePaidnumberFees incurred for the trade.
tsMsnumberUnix timestamp in milliseconds.
modestringpaper, live, or replay.
reasonstringThe signal/reason that triggered the trade.
refPricenumberThe reference price (e.g. Binance) at trade time.
edgenumberThe calculated edge/alpha at trade time.

You can also interact with the ledger programmatically using the internal ledger module:

import { loadAllTrades, computeTradeSummary } from "./core/ledger";
const trades = await loadAllTrades("./data/ledger/trades-live.jsonl");
const summary = computeTradeSummary(trades);
console.log(`Net P&L: $${summary.netPnL}`);