Unwalled

Lesson 147 of 157 · Sales craft, communication and professional tools

Python and SQL for markets analysts

Learning objective

Structure market data, compute risk and backtest with reproducible Python/SQL workflows.

Normalize instrument-specific quantity, multiplier and price units before aggregating. Map every mark to its proper observation time, currency and source.

Desk mechanics

Python supports time-series analysis, numerical valuation, simulations and visualizations; SQL retrieves and aggregates structured records. Organize data in long format with instrument_id, observation_time, value, value_type, currency, source and version. Store raw data separately from normalized data. Preserve timestamps and release vintages. Use vectorized array operations for calculations where practical and explicit tests for edge cases. Never store client names, credentials, proprietary market data or live trading keys in public coursework. A typical analysis imports validated prices, sorts by instrument/time, handles missing values according to documented rules, computes returns or yield/spread changes, aligns factors, calculates sensitivities/P&L, and exports a reproducible result. For SQL, joins must use stable identifiers and appropriate time keys; many-to-many joins can duplicate exposures. Avoid forward-filling across long gaps or market closures without labeling stale values. Backtest features must be available before the simulated decision time. Version the code, dependencies and input data.

Hypothetical worked example

A synthetic position table has signed quantities and entry/mark prices. Per equity row, pnl=quantity*(mark-entry); for bond price per 100, pnl=face_signed*(mark-entry)/100; for futures, pnl=contracts_signed*multiplier*(mark-entry). SQL can aggregate SUM(pnl) GROUP BY desk, currency; translate currency separately with timestamped rates. Combining price units without instrument-specific normalization is incorrect.

Desk application

A long 1,000 shares marked $0.30 higher contributes $300. $1m face marked 0.10 clean points lower contributes −$1,000. Two shorts at $50 per point gain $400 if futures fall four points. Total −$300 only if all figures are the same currency, interval and convention. Reject duplicated mark rows and reconcile totals by instrument before combining.

Professional extension

Create functions for bond cash flows, finite-difference Greeks, scenario revaluation and risk aggregation with unit tests. Build tests for zero volatility/time, missing quotes, duplicate trades and hedge signs. Use a deterministic random seed for reproducible simulation, while testing sensitivity across seeds. Production models need independent approval and security review.

Practice

Why can a SQL join over instrument_id alone create wrong portfolio totals?

Reveal the explanation

Multiple observations per instrument can multiply rows; join on correct timestamp/version or select a defined as-of record.

Key takeaways

Code must be auditable and testable; instrument units and timestamps matter; do not confuse a learning script with a production trading system.

Related learning

Module 4.2 assessment