mcpdata Request an invite

Answers / By industry

How do I let AI analyse trading positions and risk?

Recipe verified on Ran on a private test instance
Short answer

Load the positions into a database (one version per business day) and connect your AI to it. It can then total exposure by desk, find the largest positions, and compare days, with every figure computed over the full book instead of a pasted extract.

Steps

  1. Connect your assistant to your instance (this writes, so it needs your own instance with a ReadWrite key from the accounts portal; the public demo is read-only). In Claude Code:
    claude mcp add --transport http mcpdata https://[you].mcpdata.org/mcp --header "Authorization: Bearer $CDB_API_KEY"

    Replace [you] with your instance's name. Other apps: Connect your AI.

  2. Load the daily position files as a history. The sample folder positions/book holds three days of 200 synthetic positions and a .cdb/config.json that makes it a history folder:
    cdb-localdata add ./samples/positions --to https://[you].mcpdata.org --namespace risk --name risk
    cdb-localdata sync
  3. Totals by desk on the latest day. From Python or the workbench (your AI can save this as a view for a daily report):
    import cdb
    s = cdb.Session("https://[you].mcpdata.org")
    print(s.execute_sql("SELECT desk, COUNT(*) AS positions, SUM(market_value) AS market_value "
                        "FROM risk.book AS OF LASTDATA GROUP BY desk ORDER BY market_value DESC").to_dataframe().to_string(index=False))
  4. Ask your question. Give your AI this prompt:
    Using mcpdata, make a daily report of market value and number of positions by desk on the latest day of risk.book, largest exposure first.
What your AI does: the tool calls it makes for this prompt, and what came back
create_view(name="risk.by_desk_latest", select_sql="SELECT desk, COUNT(*) AS positions, SUM(market_value) AS market_value FROM risk.book AS OF LASTDATA GROUP BY desk ORDER BY market_value DESC") → created true, view risk.by_desk_latest
create_report(name="Book by desk, latest day", view="risk.by_desk_latest") → report_id eaafead5470647df933d11c5599164f5
run_report(report_id="eaafead5470647df933d11c5599164f5") → status ok, rows 4
Output of the steps above, from the last run
desk  positions  market_value
   Rates         41    58497837.1
Equities         61    -3574881.5
  Credit         49    -8727346.7
      FX         43   -11162703.9

The assistant needs mcpdata connected (step 1) to run the query.

Why this works

Position files are wide and long, and risk questions are aggregations (by desk, by instrument, by day) that need every row to be right. With each day kept as a version, "what did the book look like on Friday?" is a query, not a hunt for last week's file.

Related questions