Python for Investing

Learn Python from the beginning using financial examples. Every lesson works on prices, returns, positions or portfolios, and every code block on the page has been run.

38 lessons, about six hours in total. Start at Lesson 1 and work down. If you already write Python, start at the level that matches you.

By the end you can load a price file, build a signal from it, run a backtest that does not look ahead, and report what it earned after costs.

To check what stuck, the quiz draws ten questions at random from any level.

From Lesson 16 on, the lessons read a simulated prices.csv from disk. To fill the same table from a broker, connect Python to an Interactive Brokers paper account: the connection is read-only, and ib_async returns daily bars as a pandas DataFrame. How do I get market data into Python? covers the other sources and which one fits which test.

01

What is a variable?

Name a price, a ticker and a share count, then value a position and a three-stock portfolio.

Beginner 8 min
02

How do I do maths and print a result?

Do arithmetic in Python and print the result with an f-string.

Beginner 8 min
03

What is True and False?

Compare two numbers, store the answer, and build a buy rule out of it.

Beginner 8 min
04

What is a list?

Hold many values under one name, count them, pick them out by position, and add new ones.

Beginner 7 min
05

How do I slice a list?

Slice a list with x[start:stop], and turn the last few closes into a moving average.

Beginner 8 min
06

What is a for loop?

Run the same lines once for every item in a list, and add the results up as you go.

Beginner 8 min
07

What are if, elif and else?

Pick one outcome with if, elif and else, and see how the order of the tests decides which one you get.

Beginner 8 min
08

How do I turn prices into returns?

Build a list of returns in a loop, then compound them into the return for the whole stretch.

Beginner 9 min
09

What is a function?

Give a calculation a name with def, feed it inputs, and get an answer back.

Beginner 8 min
10

What is a dictionary?

Map a key to a value, look it up by name, and use two dictionaries keyed by ticker to value a portfolio.

Beginner 8 min
11

What is a class?

Write your own class, build two objects from it, and read what self means on every line.

Beginner 12 min
12

What is a numpy array?

Hold many numbers in one array and do maths on all of them at once, without writing a loop.

Intermediate 8 min
13

How do I filter with a condition?

Turn a comparison into an array of True and False, use it to select elements, count them, and switch it into a position of 1 or 0.

Intermediate 9 min
14

What is a pandas Series?

Build a Series of closes, select by label and by position, and see arithmetic line up two price series on their dates.

Intermediate 10 min
15

What is a DataFrame?

Build a table from a dictionary of lists, select columns from it, add a computed column, sort it, and filter its rows.

Intermediate 10 min
16

How do I load a CSV file?

Read a CSV into a DataFrame with pd.read_csv, parse the dates, and index the rows by date.

Intermediate 10 min
17

How do I compute returns in pandas?

Turn a column of prices into a column of returns with pct_change, and handle the NaN it leaves in the first row.

Intermediate 9 min
18

Why is an array operation faster than a loop?

Time a Python loop against the same calculation written as one array operation, and see where the gap comes from.

Intermediate 10 min
19

What is a moving average?

Take the mean of the last n values at every row with .rolling(n).mean(), and put a 5-day and a 10-day average on a price column.

Intermediate 11 min
20

How do I use yesterday's value?

Move a column down one row with .shift(1) so every row can read the row above it.

Intermediate 8 min
21

How do I build an equity curve?

Turn a column of returns into the growth of one unit of money with (1 + r).cumprod().

Intermediate 8 min
22

How do I do the same thing per ticker?

Split a stacked table into one group per ticker, run a calculation inside each group, and get the answers back.

Intermediate 10 min
23

How do I stack two tables?

Glue DataFrames together with pd.concat, top to bottom by default and side by side with axis=1.

Intermediate 7 min
24

How do I join two tables on a key?

Line two tables up on a shared column with pd.merge, and choose which rows survive with inner, left, right and outer.

Intermediate 10 min
25

What is a panel?

Put a date and a ticker in the index, pull out one date or one ticker, and switch between the long and wide shapes.

Intermediate 12 min
26

What is the difference between an SMA and an EMA?

Compute a simple and an exponential moving average, and see how each one weights the past.

Advanced 10 min
27

How do I compute RSI?

Build the relative strength index from daily changes, Wilder smoothing, and the 0 to 100 formula.

Advanced 11 min
28

What are Bollinger bands?

Draw a moving average with a band two standard deviations above and below it, and measure where the close sits inside that band.

Advanced 10 min
29

How do I turn an indicator into a position?

Read an indicator with a rule to get a signal, then lag the signal by one row to get the position you hold.

Advanced 11 min
30

Why do I lag the signal?

Shift a signal down one row so it sits in front of the return it earns, and see how much the total changes.

Advanced 10 min
31

How do I charge trading costs?

Charge a cost every time the position changes, using turnover = position.diff().abs() times a rate.

Advanced 10 min
32

What is a drawdown?

Measure how far an equity curve sits below its own running peak, and how long it stays there.

Advanced 10 min
33

How do I measure return and risk?

Compute CAGR, annualised volatility, Sharpe and Sortino on a return series, then put all of them in one function.

Advanced 12 min
34

How do I put a backtest in one function?

Wrap the lag, the costs, the compounding and the metrics into one function that takes prices and a signal and hands back a table.

Advanced 12 min
35

How do I test many parameters at once?

Run the backtest function once for every value in a list of parameters and collect the results in one table.

Advanced 11 min
36

How certain is a Sharpe ratio?

Resample a return series with replacement a few thousand times to see the range of Sharpe ratios the same data could have produced.

Advanced 12 min
37

How do I backtest with a package?

Run the Lesson 34 rule through the bt package and check its numbers against the hand built ones.

Advanced 11 min
38

How do I get the performance stats without writing them?

Call ffn.calc_stats on a price series to get CAGR, volatility, Sharpe, Sortino, drawdown and forty more, and check them against the ones you built by hand.

Advanced 11 min
No matching items