---
title: "Level 4: portfolios, risk, execution and live trading"
description: "Level 4 of the Python Trading Course: portfolio optimisation, risk management, microstructure and execution, brokers and APIs, and a live system."
url: "https://pythonparatrading.com/en/python-trading-course/level-4/"
lang: "en"
image: "https://pythonparatrading.com/og/en/python-trading-course/level-4.png"
---

# Level 4: Portfolios, risk, execution and live trading

> From backtest to a live account: build portfolios of strategies, size and control risk, execute with measured costs and set up a fault-tolerant live system.

- Hours: 90–100
- Prerequisite: Levels 2 and 3; basic linear algebra
- Profile: Manager or trader who wants to trade live
- Course: [Python Trading Course](https://pythonparatrading.com/en/python-trading-course/)

## Portfolio theory and optimisation

- `4.1.1` Markowitz mean-variance; efficient frontier; real-world constraints.
- `4.1.2` Covariance estimation: shrinkage (Ledoit-Wolf), factor models, denoising (random matrix theory).
- `4.1.3` Black-Litterman: incorporating views, including those from ML models.
- `4.1.4` Risk parity, maximum diversification, minimum variance; HRP and HERC (López de Prado).
- `4.1.5` Optimisation with CVaR, drawdown and turnover and cost constraints (cvxpy).
- `4.1.6` Factor models: CAPM, Fama-French, PCA; attribution.
- `4.1.7` Rebalancing (calendar or threshold-based) and portfolios of strategies.
- `4.1.8` Tools: Riskfolio-Lib, PyPortfolioOpt, skfolio, bt.

## Risk management and position sizing

- `4.2.1` Risk per trade, stops (fixed, ATR, time-based), risk of ruin.
- `4.2.2` The Kelly criterion (and fractional Kelly) and volatility targeting.
- `4.2.3` VaR (historical, parametric, Monte Carlo), CVaR or Expected Shortfall, VaR backtesting.
- `4.2.4` Stress testing with historical scenarios (1987, 2008, 2020, 2022); correlation, liquidity, gap and counterparty risk.
- `4.2.5` Drawdown control: exposure reduction, circuit breakers, kill switch.
- `4.2.6` Derivatives-specific risk: portfolio Greeks, margin, liquidations.

## Microstructure and execution

- `4.3.1` Implementation shortfall, VWAP and TWAP; impact models (square root, Almgren-Chriss).
- `4.3.2` Order flow imbalance and microstructure signals; latency (retail versus HFT).

## Brokers and APIs

- `4.4.1` Choosing a broker from Spain or the EU: regulation (CNMV, ESMA, passporting), assets, costs, API.
- `4.4.2` Interactive Brokers: TWS and IB Gateway, ib_async, contracts, data, orders.
- `4.4.3` Alpaca, Tradier; MetaTrader 5 with Python; OANDA v20.
- `4.4.4` Crypto: ccxt, native APIs (Binance, Bybit, Kraken, Coinbase), websockets.
- `4.4.5` Secure credentials: environment variables, secret managers, least privilege (no withdrawals).

## Live trading system

- `4.5.1` Architecture: data → signal → risk → OMS → broker → reconciliation.
- `4.5.2` Order management: states, partial fills, cancellations, reconnections.
- `4.5.3` Paper trading and shadow trading; backtest versus live comparison (slippage tracking).
- `4.5.4` Task scheduling (cron, APScheduler, Prefect) and alerts (Telegram, Slack, email).
- `4.5.5` Fault tolerance: idempotency, persistent state, reconciliation at startup.
- `4.5.6` NautilusTrader or LEAN live: from backtest to production with the same code.

## Level project

A multi-strategy portfolio (at least three strategies from levels 2 and 3) optimised with HRP or risk parity, with volatility targeting and a kill switch, paper trading for 4 to 8 weeks at IBKR or Alpaca. The deliverable includes a report on the deviations between backtest and live.

## Practice

Research on this site to practise this level

1. [Volatility Targeting in Python](https://pythonparatrading.com/en/volatility-targeting-2/)
2. [Price Discovery & Arbitrage Pricing Theory](https://pythonparatrading.com/en/arbitrage-pricing-theory-and-price-discovery-2/)
3. [Quantitative Models and Algorithmic Execution](https://pythonparatrading.com/en/quantitative-models-algorithmic-execution/)
4. [Finite-State Machines for Quantitative Trading Execution in Python: From Knight Capital to LangGraph](https://pythonparatrading.com/en/finite-state-machines-trading-python/)

Previous level: [Level 3: Derivatives: futures, spreads, forex and options](https://pythonparatrading.com/en/python-trading-course/level-3/)

Next level: [Level 5: Machine learning and deep learning (Keras/PyTorch)](https://pythonparatrading.com/en/python-trading-course/level-5/)
