Skip to content
Edition NigeriaGlobal
Country: Zimbabwe

You are in the Zimbabwe edition. Type your country to switch.

Set up your profile

You are learning in the Zimbabwe edition, with examples in zig and from Zimbabwe.

Financial Data Lab · Lesson 2 of 8

Lab 2: computing returns in code

This lab turns a column of prices into daily returns, cumulative returns, annualised volatility and maximum drawdown in Python, and shows how to check every figure against a hand calculation on a small invented sample. It also explains simple against log returns and why returns must be computed per ticker.

Advanced10 min read5 steps

This lesson is complete in writing. A video for it is on its way.

What you will learn

  • By the end you can compute daily simple returns and log returns for each share in a price table with pandas.
  • You can compute a cumulative return by compounding, and explain why adding returns gives the wrong answer.
  • You can compute daily and annualised volatility and the maximum drawdown in code.
  • You can check every figure against a hand calculation on a small invented sample.

Key terms in this lesson

Simple return
The change in price, plus any income, divided by the starting price over one period. Simple returns compound by multiplying, not by adding.
Log return
The natural logarithm of the end price divided by the start price. Log returns add up across periods, which is useful in statistics, but they differ slightly from the returns an investor actually ea…
Drawdown
The fall in value from a peak to a later low, usually shown as a percentage.

Step 1 of 5

Returns multiply, they do not add

Opening this step…

Step 2 of 5

The sample file and the return code

This step opens when you finish the one above.

Step 3 of 5

Cumulative return and volatility

This step opens when you finish the one above.

Step 4 of 5

Maximum drawdown in code

This step opens when you finish the one above.

Step 5 of 5

Testing the code before you trust it

This step opens when you finish the one above.

Key takeaways

The key takeaways open when you finish every step.

Lesson quiz

6 questions. You need 85% to pass.

The quiz opens when you finish every step.

Read first

Stockertim Learn is education. It explains how investing works and never recommends buying or selling anything.

Up next

Financial data & analyticsLab 3: plotting prices and returnsRead nowLab 3: plotting prices and returnsFinancial data & analytics · Advanced · 15 minFinancial data & analyticsLab 4: SQL on a price tableRead nowLab 4: SQL on a price tableFinancial data & analytics · Advanced · 15 minFinancial data & analyticsLab 5: cleaning corporate action…Read nowLab 5: cleaning corporate actions in codeFinancial data & analytics · Advanced · 15 minFinancial data & analyticsLab 6: a sentiment classifier on…Read nowLab 6: a sentiment classifier on sample headlinesFinancial data & analytics · Advanced · 15 minFinancial data & analyticsReturns, CAGR and volatility in …Read nowReturns, CAGR and volatility in a spreadsheetFinancial data & analytics · Intermediate · 15 minFinancial data & analyticsDrawdowns and correlation in a s…Read nowDrawdowns and correlation in a spreadsheetFinancial data & analytics · Intermediate · 15 minFinancial data & analyticsCompounding and CAGRRead nowCompounding and CAGRFinancial data & analytics · Beginner · 15 minFinancial data & analyticsLab 1: reading CSV files in Pyth…Read nowLab 1: reading CSV files in Python and SQLFinancial data & analytics · Advanced · 15 minFinancial data & analyticsSpread and volatility: how much …Read nowSpread and volatility: how much values varyFinancial data & analytics · Beginner · 15 minFinancial data & analyticsDistributions and fat tailsRead nowDistributions and fat tailsFinancial data & analytics · Intermediate · 15 minFinancial data & analyticsRegression and betaRead nowRegression and betaFinancial data & analytics · Intermediate · 15 min
English