Quantitative Trading · Level I · Elective
Python for Trading I
Python from zero for traders. This elective covers the language basics a trader needs, then pandas for price data, vectorized signals, plotting, a first honest backtest and reading data from a broker. No prior programming is assumed.
What you will be able to do
- Write basic Python: variables, lists, dictionaries, loops and functions.
- Load, clean and transform price data with pandas.
- Build a vectorized signal and a first backtest with the correct lag.
Section 1 of 2
Python basics for traders
Python programs are sequences of statements. Values are stored in variables (`price = 101.5`), grouped in lists (`prices = [100, 101.5, 99.8]`) and dictionaries (`position = {"AAPL": 100, "MSFT": 50}`). Indentation defines blocks, so a loop or function body is indented under its header.
Loops repeat work: `for p in prices:` visits each price in turn. Conditions choose between branches: `if p > 100:`. Functions package logic so it can be reused and tested: `def pct_change(a, b): return b / a - 1`.
For trading code, clarity beats cleverness. Name variables for what they hold, keep functions small, and test each function on a case where you know the answer before using it on real data.
Keep this in mind: Variables, lists, dictionaries, loops and small tested functions are enough to start.
Make the idea stick
Quick checks before you move on.
Quick check 1 of 2 · Python basics for traders
What does `simple_return(50, 45)` return?
Quick check 2 of 2 · Python basics for traders