100% found this document useful (2 votes)
2K views

Python For Financial Analysis Ebook 2021

Uploaded by

Tee Shi Feng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
2K views

Python For Financial Analysis Ebook 2021

Uploaded by

Tee Shi Feng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 82

PYTHON FOR FINANCIAL ANALYSIS

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Python for Financial Analysis


This eBook will teach you all you need for backtesting investment strategies using Python
libraries Pandas, NumPy, and Matplotlib in Jupyter Notebook.

What will this eBook teach you?


Great question.
This eBook assumes that you are familiar with Python. If not,
feel free to follow along.
The main focus of this book is to use the Python libraries
Pandas, NumPy and Matplotlib for backtesting investment
strategies.
According to the 2020 official annual Python Developers Survey
these are the most commonly used libraries by Data Scientist.
By the time you finish this eBook, you will have covered the
following with practical code examples (available on GitHub):
• How to work with financial time series data.
• Read data from CSV files and directly from API.
• Visualize financial time series data with Matplotlib.
• Calculate return (CAGR), maximum drawdown, and volatility (backtesting performance).
• How to work with investment strategies.
• Backtesting investment strategies.
Simply, master all you need to know for backtesting investment strategies with Python.

How to get the most out of this eBook


This book is accompanied with Jupyter Notebooks available from this GitHub repository.
Using the Notebooks along with the book will improve your learning journey.
There are two ways to move forward.
• Go to this GitHub repository and download all Notebooks and work with them in your own
Jupyter Notebook environment (you can download as Zip-file).
• Use the links to Colab given in the GitHub description and work with the Notebooks
interactively in Colab (no need for installation of setup).

Install your own setup?


Anaconda comes with Jupyter Notebook and Python, which is all you need. Install Anaconda
Individual Edition and Launch Jupyter Notebook. See the documentation for troubleshooting.

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Table of Contents

00 – Pandas DataFrames _________________________________________________________ 7


Learning objectives __________________________________________________________________________ 7
What is Pandas? _____________________________________________________________________ 7
A brief introduction to Jupyter Notebook _________________________________________________ 7
What is Jupyter Notebook? ___________________________________________________________________ 8
How to open the Jupyter Notebooks from this eBook_______________________________________________ 8
How to execute the code in the Jupyter Notebook _________________________________________________ 9
The code explained __________________________________________________________________ 10
What is a CSV file? _________________________________________________________________________ 11
Back on track _____________________________________________________________________________ 11
Learn about DataFrames ____________________________________________________________________ 12
Get the data as we want_____________________________________________________________________ 13
How to use the index of a DataFrame ___________________________________________________ 14
Our first project – Simple Moving Average _______________________________________________ 15
Step 1 ___________________________________________________________________________________ 15
Step 2 ___________________________________________________________________________________ 16
Step 3 ___________________________________________________________________________________ 17
Summary __________________________________________________________________________ 17
01 – Read from API _____________________________________________________________ 18
Learning objectives _________________________________________________________________________ 18
What is an API ______________________________________________________________________ 18
How to read data from Yahoo! Finance API _______________________________________________ 18
How to specify an end date ___________________________________________________________ 19
How to read data from multiple tickers at once ___________________________________________ 20
Project – Normalize and Calculate Return of Portfolio ______________________________________ 21
Step 1 ___________________________________________________________________________________ 21
Step 2 ___________________________________________________________________________________ 21
Step 3 ___________________________________________________________________________________ 22
Step 4 ___________________________________________________________________________________ 22
Step 5 ___________________________________________________________________________________ 23
Summary __________________________________________________________________________ 24
02 – Visualize DataFrames _______________________________________________________ 25
Learning objectives _________________________________________________________________________ 25
What you need to know about Matplotlib _______________________________________________ 25
Your first chart with Matplotlib ________________________________________________________ 25
Multi-line chart _____________________________________________________________________ 27
Limit the time period of the chart ______________________________________________________ 28

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Multiple charts in one figure __________________________________________________________ 29


Project – Compare two investments ____________________________________________________ 30
Step 1 ___________________________________________________________________________________ 30
Step 2 ___________________________________________________________________________________ 31
Step 3 ___________________________________________________________________________________ 31
Step 4 ___________________________________________________________________________________ 31
Step 5 ___________________________________________________________________________________ 32
Summary __________________________________________________________________________ 34
03 – Buy and Hold ______________________________________________________________ 35
Learning objectives _________________________________________________________________________ 35
How to approach this ________________________________________________________________ 35
Read the data ______________________________________________________________________ 35
The lost decade _____________________________________________________________________ 37
The CAGR vs AAGR __________________________________________________________________ 38
Calculate the CAGR for S&P 500 ________________________________________________________ 39
Maximum Drawdown ________________________________________________________________ 40
Project – Calculate the CAGR and Maximum Drawdown of S&P 500 from 2010 to 2020 ___________ 41
Step 1 ___________________________________________________________________________________ 41
Step 2 ___________________________________________________________________________________ 41
Step 3 ___________________________________________________________________________________ 41
Summary __________________________________________________________________________ 41
04 – Volatility__________________________________________________________________ 42
Learning objectives _________________________________________________________________________ 42
Volatility – not just one definition ______________________________________________________ 42
Log returns _________________________________________________________________________ 42
Log returns calculations with Pandas and NumPy __________________________________________ 43
Normalized data and log returns _______________________________________________________ 44
Volatility calculation _________________________________________________________________ 45
Project – Visualize the volatility of the S&P 500 index ______________________________________ 46
Step 1 ___________________________________________________________________________________ 46
Step 2 ___________________________________________________________________________________ 46
Step 3 ___________________________________________________________________________________ 46
Summary __________________________________________________________________________ 47
05 – Correlation ________________________________________________________________ 48
Learning objectives _________________________________________________________________________ 48
What correlation tells us ______________________________________________________________ 48
Why do we care about correlation? _____________________________________________________ 48
Project – Calculate the return (CAGR), maximal drawdown and volatility of SPY and TLT __________ 51
Step 1 ___________________________________________________________________________________ 51
Step 2 ___________________________________________________________________________________ 52
Step 3 ___________________________________________________________________________________ 52

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Summary __________________________________________________________________________ 52
06 – A Simple Portfolio __________________________________________________________ 53
Learning objectives _________________________________________________________________________ 53
The simple portfolio _________________________________________________________________ 53
50-50 split without rebalance __________________________________________________________ 53
Project – An annual rebalance _________________________________________________________ 56
Step 1 ___________________________________________________________________________________ 56
Step 2 ___________________________________________________________________________________ 56
Step 3 ___________________________________________________________________________________ 57
Step 4 ___________________________________________________________________________________ 57
Step 5 ___________________________________________________________________________________ 57
A word of warning and how to avoid it __________________________________________________ 58
Summary __________________________________________________________________________ 59
07 – The Asset/Hedge Split _______________________________________________________ 60
Learning objectives _________________________________________________________________________ 60
The calculations _____________________________________________________________________ 60
Project – Sharpe Ratio ________________________________________________________________ 63
Step 1 ___________________________________________________________________________________ 64
Step 2 ___________________________________________________________________________________ 64
Step 3 ___________________________________________________________________________________ 64
Summary __________________________________________________________________________ 65
08 – Backtesting an Active Strategy ________________________________________________ 66
Learning objectives _________________________________________________________________________ 66
Getting started with an active strategy __________________________________________________ 66
The strategy explained ______________________________________________________________________ 66
Getting started ____________________________________________________________________________ 66
The signal line _____________________________________________________________________________ 67
Log returns of the strategy ___________________________________________________________________ 67
The cumulative return of the strategy __________________________________________________________ 68
Project – Backtesting different periods and visualize results _________________________________ 69
Step 1 ___________________________________________________________________________________ 69
Step 2 ___________________________________________________________________________________ 70
Step 3 ___________________________________________________________________________________ 70
The learning from 2008 backtesting _____________________________________________________ 71
Summary __________________________________________________________________________ 73
09 – Backtesting Another Strategy _________________________________________________ 74
Learning objectives _________________________________________________________________________ 74
The 12% solution described ___________________________________________________________ 74
Implementing the 12% solution ________________________________________________________ 74
Project – Backtesting the 12% solution __________________________________________________ 75
Step 1 ___________________________________________________________________________________ 76
Step 2 ___________________________________________________________________________________ 76
Step 3 ___________________________________________________________________________________ 76

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Summary __________________________________________________________________________ 78
Next Steps – Free Video Courses ___________________________________________________ 79
Free Online video courses _____________________________________________________________ 79
Python for Finance: Risk and Return ___________________________________________________________ 79
Financial Data Analysis with Python ____________________________________________________________ 80
A 21-hour course Python for Finance ____________________________________________________ 81
Feedback _____________________________________________________________________ 82
Disclaimer ________________________________________________________________________________ 82

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

00 – Pandas DataFrames
In this chapter we will get acquainted with the main library when working with financial data in
Python: Pandas.
Note Pandas is part of the Anaconda distribution and is not needed to be installed separately.
Learning objectives
• Open the project in Jupyter Notebook.
• How to execute the code.
• Load financial data from a CSV file.
• Learn about the main data type in Pandas, DataFrame.
• Make your first project where you calculate the Simple Moving Average of the Close price.

What is Pandas?
Pandas (derived from panel and data) contains powerful and easy-to-use tools for doing practical,
real world data analysis in Python.
The best way to learn about new things is to relate it to something similar.
Let’s try to load some data into Pandas and see how similar it is to an Excel spreadsheet.

Note The above code snippets are available directly on GitHub. You can either download the
Notebooks or run them directly in Colab from the links provided (see GitHub).

A brief introduction to Jupyter Notebook


The above code snippets are taken directly from a Jupyter Notebook.

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

If you are new to Jupyter Notebook – this section is for you. Otherwise, you can skip to the next
section, where we will learn about the code above.
What is Jupyter Notebook?
Jupyter Notebook is an open-source web application that allows you to create and share
documents that contain live code, equations, visualizations and narrative text.
Said differently.
• It works in your browser.
• You can write your Python code in it.
• It can execute the Python code for you and show the results.
• You can write text in it.
• It can create visualizations with charts.
Jupyter Notebooks are very popular among beginners and Data Scientists.
• It is easy to learn programming in a Notebook.
• The needs of a Data Scientist are well served in a Notebook.
• Notebooks are good to explore data and programming in an easy way.
Financial analysis can be seen as a Data Science task.
• You work with financial data.
• You explore the data and make calculations.
• You visualize the data.
Jupyter Notebook seems to be a perfect match for financial analysis.
How to open the Jupyter Notebooks from this eBook
How to get started.
• Install Anaconda Individual Edition.
• Download the zip file from the GitHub.
• Unzip the content of the zip-file in a location you can find.
• Launch Anaconda Navigator from your main menu (Launchpad or Windows menu).
• Inside Anaconda Navigator launch Jupyter Notebook.

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

This should bring you into your main browser with the Jupyter Notebook Dashboard (see below).

• Navigate to the extracted files from the downloaded zip file.


• Click on the first Notebook 00 – Pandas DataFrames.ipynb
This will open the Notebook in a new browser tab.
How to execute the code in the Jupyter Notebook
A Notebook is divided into cells. A cell is a box like this one.

The above cell contains 1 line of code. The import statement of the main library in this case.
A cell has been executed if it has a number, like the above In [1]:
If the cell hasn’t been executed it would not contain a number.

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

To execute a cell can be done in various ways. The normal way is to mark the cell (can be done
with mouse or keys and press enter).

Then press Shift + Enter to execute it.

Alternatively, you can press Run with your mouse


The cell will execute and when done finish with a number.

To edit a cell, just mark it and press enter. Then you enter edit-mode.
This is all you need to know to follow along this eBook.
If you would like to learn more about the specific elements within the Notebook Editor, you can go
through the user interface tour by selecting Help in the menu bar then selecting User Interface
Tour.

The code explained


Let’s get back to the code and break it down.

10

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The first code cell, import pandas as pd, can look intimidating at first.
This ensures access to a library called pandas (the main library we use – read more about Pandas
here). You use the as pd for easy access, as we see in the next cell.
The real magic happens in the next cell. We define the location to the CSV file we want to read
(csv_file = “…”).
This CSV file was downloaded from Yahoo! Finance. It can be downloaded by finding Apple stock
and go to Historic Data and use the Download.
What is a CSV file?
A CSV file is a Comma Separated Values file.
The one we use here looks like this (or the beginning of it).

As you can see, the first row contains the names of each column. Then each line is separated with
commas for each value corresponding to the name in the first row.
Back on track
The second cell.

11

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Here we define where the CSV file is located. Now, this is set to be the location in the repository. If
you execute it locally, you can change the cvs_file to the following.
csv_file = "AAPL.csv"
This will load it from your local computer and not from the repository.
Note If you run the code in Colab you need to load it from the repository.
Finally, the following line.

It shows the 5 first lines of the content we loaded.


The content is referenced by the variable aapl. To shows the first 5 lines of content we use
aapl.head(). This calls the function head() on the object aapl.
The object aapl is a DataFrame.
As you see, the DataFrame looks similar to an Excel sheet.
Learn about DataFrames
A DataFrame as rows and columns.
Each column has a data type.

Here we see the that column Date has data type object, while Open, High, Low, Close, Adj Close
has data type float64, while Volume has data type int64.
A DataFrame has an index.

Here the index is a range from 0 to 1258 with step 1.


Basically, that means the numbers 0, 1, 2, 3, …, 1257.

12

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Get the data as we want


When working with financial data there are two things that would make the above data more
convenient.
First, if the Date columns was interpreted as dates. As it is now, it is just a string stored with some
numbers. If the DataFrame knows it is dates, we can do a lot of nice manipulations with the data.
This will be handy later.
Second, we want to use the Date column as index. This is makes it easy to see data in a specific
date time interval.
Luckily, this is straight forward to do.

The index_col=”Date” sets the index to be the Date column. And parse_dates=True ensures that
the dates (in this case in the Date column) are parsed as dates.

As the above shows, we now have the Date column as the index.

The data types are looking similar, except we do not have the Date column there anymore. This is
because it is the index now.

We see that the index is now of type DatetimeIndex.

13

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

How to use the index of a DataFrame


The DateFrame has two main ways to access rows with the index.
The loc and iloc. We will explore both here.

The loc can access a row in our DataFrame by using the “index name”. Here, it is a date, hence, we
can write the date to access it.
Notice, that the output if formatted a bit different than when we used head().
Here the data is simply put in a column like fashion. Also, notice, that it only has one data type
(float64).
This is because it converts the row to a Series (the other main data type from the Pandas library).
Don’t worry too much about that now.

You can access a date interval by using the slicing technique above.
Notice that because we use DatetimeIndex, it can actually figure out what the date range is, even
though we have used dates with no data (it was weekend the 27th and 28th).

The iloc is an integer location. The iloc[0] will access the first row.
Similarly, iloc[1] the second row and so forth.

14

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

We can access the data from the back with negative indexing. Just like Python lists.

Finally, we can make slicing from row 0 to 4 (where row 4 is excluded). Hence, we see the rows of
index 0, 1, 2, and 3.

Our first project – Simple Moving Average


We will learn how to calculate a simple moving average.
In finance, a simple moving average (SMA) is a stock indicator that is commonly used in technical
analysis.
“A simple moving average (SMA) is a calculation that takes the arithmetic mean of a given set of
prices over the specific number of days in the past; for example, over the previous 15, 30, 100, or
200 days.” – Investopedia.org.
That is, for every day, we look at the last days prices and take the average of them.
It will be clear in a moment.
Let’s calculate the SMA of the Close price.
Step 1
Then we need to access the Close prices.

15

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

As you see, this can be done quite easy.


Step 2
We need to understand the method rolling(.), which provides a rolling window of calculation.
Let’s explore it.

We apply rolling with argument 2, rolling(2).


The mean() calculates the average of the rolling window.

If we take the average of the Close price of the 9th and 10th of March, then we get the value given
for the rolling(2).mean() the 10th of March.
Similarly, for 10th and 11th of March, as the above example demonstrates.
Hence, the rolling(2).mean() applies the mean() function on the window of the last 2 rows.

Similarly, the rolling(5).mean() applies the mean() function on the window of the last 5 rows.

16

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Step 3
Now we can calculate the simple moving average of any days.
In this example we will calculate the simple moving average of 10 days.
Often, the simple moving average is called the moving average (MA). On financial pages, like
Yahoo! Finance, they use the abbreviation MA for the simple moving average.

Now that was nice.


Notice that we added a new column to our DataFrame simply by specifying it.
Also, notice that we use aapl.tail() to get the last 5 rows of our dataset.

Summary
Now we have learnt what a DataFrame is. How to read data properly from financial pages like
Yahoo! Finance into our DataFrame. How to access data rows based on a date index and integer
value index. Also, how to slice data, i.e., showing subsets of data rows.
Finally, we made a project where we calculated the simple moving average of the Close price and
added it to our DataFrame.

17

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

01 – Read from API


In the last chapter we learned about DataFrames and how to operate time series data from
financial pages like Yahoo! Finance. This required a manual step where we download a CSV file
from the page.
In this chapter we will learn how to automatically read from the Yahoo! Finance API using the
Pandas Datareader library.
Learning objectives
• What is an API.
• How to read historic stock prices directly from an API.
• Specify the time interval you want prices from.
• How to read historic stock prices for multiple tickers at the same time.

What is an API
“An application programming interface (API) is a computing interface that defines interactions
between multiple software or mixed hardware-software intermediaries.” – Wikipedia.org
For our purpose, it helps us read historic stock prices directly from Jupyter Notebook without
manually downloading a CSV file from a page like Yahoo! Finance.

How to read data from Yahoo! Finance API


To achieve that we will use another library.
Pandas Datareader, an up to date remote data access for pandas, works for multiple versions of
pandas.

The library is imported together with another library, the datetime.


The datetime library is a standard library used to specify dates, which we need to specify a start
date and potentially an end date.
This already sound complex.
Luckily it is not. The Pandas Datareader will do all the hard work for us.
You only need to specify a ticker and start date.
“A stock symbol is a unique series of letters assigned to a security for trading purposes. Symbols
are just a shorthand way of describing a company's stock, so there is no significant difference
between those that have three letters and those that have four or five. Stock symbols are also

18

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

known as ticker symbols.” – Investopedia.org

As you see, the start date is defined by the dt.datetime(2010, 1, 1). This creates a datetime object
representing the year 2020, month 1 (January), and day 1.
Then we call the Pandas Datareader with pdr.get_data_yahoo(…), and specify the ticker AAPL,
which is the ticker (stock symbol) for Apple.
You can find any available ticker on Yahoo! Finance by searching the name of the company you
wish to have historic stock prices from.

The Pandas Datareader returns a DataFrame as we know it from last chapter.

The good thing is, that it already has the Date as the index and it is a DatetimeIndex.
All ready to use.

How to specify an end date


This is probably just as you would guess.

In this case we want all the data from January 1st, 2010 until January 1st, 2020.
We can investigate the start and end data as follows.

19

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Notice, that the output of the DataFrame (aapl) is by default shortened with … in the middle, and
not displaying the full set of 2,516 rows.

How to read data from multiple tickers at once


You can read historic stock prices from multiple tickers simultaneously.

This is convenient for speed and to lower the number of requests you make to the API.
The data is structured as follows.

20

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Later we will learn how to access the data in a convenient way, when we work with data from
multiple tickers.

Project – Normalize and Calculate Return of Portfolio


In this project we will learn how to normalize data and calculate the return of a portfolio using
Pandas DataFrames.
“In statistics and applications of statistics, normalization can have a range of meanings. In the
simplest cases, normalization of ratings means adjusting values measured on different scales to a
notionally common scale, often prior to averaging.” – Wikipedia.org
As you will see in this project it is easy to normalize data with DataFrames and it is a great tool to
master when working with financial data.
Step 1
We continue with the data we have prepared in this chapter.
The first step is to get the Adj Close values for all the tickers.

This simple statement takes all the Adj Close prices from the DataFrame.

As you see the advantage of the two-layer column names, which the Pandas Datareader returns.
It makes it easy to get the Adj Close from all tickers.
If you are unfamiliar with adjusted close (Adj Close):
“The adjusted closing price amends a stock's closing price to reflect that stock's value after
accounting for any corporate actions. It is often used when examining historical returns or doing a
detailed analysis of past performance.” – Investopedia.org
Step 2
Now we need to normalize the data.

This looks too easy, right?


What happens there? Well, it takes the entries of the first row and divides it with all the rows.

21

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The result?

As you see, all the start prices are adjusted to 1. This is the normalization.
Then the relative change can be seen from day 2010-01-05 and forward.
Step 3
Let’s assume that our portfolio consists of 25% of each ticker.
How can we model that?

Create a list with four 0.25 values.

Then we multiply that list to the normalized data.

Then we get a weighted distribution of our portfolio. Starting with 25% for each ticker.
Step 4
If we calculate the sum of that and add that to the DataFrame. Notice, that we sum axis=1, which
means along the rows.

This is convenient as we will see in a moment.

22

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Then we have the sum of our weighted sum of how the portfolio evolves in the Total column.
Step 5
Let’s assume we invested 100,000 USD. This can be modeled quite easy as you see here.

Our portfolio looks like this in the end (tail() shows the last 5 rows by default).

We have that 2021-03-11 we will have 2,977,541 USD in total.


This value can also be obtained directly as follows.

Hence, this shows that if we invested 100,000 USD the opening day of 2010 in a portfolio
consisting of 25% of each ticker, then we would have 2,977,540 USD on March 11th, 2021.

23

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Summary
In this chapter we learned how to read historic stock prices directly from the Yahoo! Finance API
using Pandas Datareader. How to set the start and end date. Also, how to read multiple tickers at
the same time.
In the project we learned how to normalize data to calculate the return of a portfolio.

24

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

02 – Visualize DataFrames
In this chapter we will learn how to visualize historic stock prices on a chart using Matplotlib, as
well as values calculated and added to the DataFrames.
“Matplotlib is a comprehensive library for creating static, animated, and interactive visualizations
in Python.” – matplotlib.org
Learning objectives
• Learn how to make a chart of historic stock prices.
• Different modes to visualize charts in Jupyer Notebook.
• Have multiple charts on the same axis.
• Have multiple axis in one figure.

What you need to know about Matplotlib


Matplotlib is an easy to use library to create charts from Pandas DataFrames and Series.
With a few lines of code, you are up and running.
There are two different ways to use Matplotlib, which often make users more confused than good
it.
• The object-oriented way, and
• The functional way.
By experience, I found that it is easier to understand the object-oriented way and it leads to less
confusion.
It is true, that it needs a bit more code than with the functional way.
But used right, you only need to add one extra line of code and pass one argument.
This gives you more flexibility and less confusion.
Therefore, this eBook will only use the object-oriented way with Matplotlib.
Let’s get started and see how easy it is.

Your first chart with Matplotlib


We need to import Matplotlib and set the mode.

The first line (import pandas as pd) is familiar and imports the Pandas library.
Then the second line imports the Matplotlib library, where we choose the pyplot module and
want access to with the shorthand plt.

25

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Finally, the %matplotlib notebook needs some explanation.


This sets the way Jupyter Notebook renders the figures from Matplotlib.
Many people use the %matplotlib inline, which for most cases is fine. The inline renders the figure
once and inserts a static image into the notebook when the cell is executed.
While the %matplotlib notebook makes the figures interactive. In this case you can zoom in on
details of the chart.
There are other interactive options as well, but they need to be installed separately at the time of
writing.
In this eBook we will use the notebook mode, but feel free to use the inline mode.
For this demonstration purpose of Matplotlib, we will use the data from the CSV file from first
chapter. This ensures you will get the same results as presented here.

It is always good habit to check the data is as expected to avoid surprises later.

This looks as expected.


Now to the exciting part. The visualization.

This is the object-oriented way to use Matplotlib. You might be familiar with rendering a chart
directly by one line of code.
In this case, the advantage is small, but still it is there. If you render another chart from the same
dataset, then this will be added to the existing figure. That leads to a lot of confusion. Here, we
avoid that.
A few notes on the above code.
The plt.subplots() returns a figure fig and axis ax. We need to parse the axis ax to the plot method
of the DataFrame (the second line .plot(ax=ax)).
The figure is the entire image of one or more charts (axes). The axis is the where the chart is
drawn. You can have multiple axes in one figure as we will see later.

26

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

This is all you need to know.


The DataFrame takes in this case the only the data from the column Close and renders it.

This shows the interactive figure that the cell outputs.


You can save the figure, you can zoom in on areas, and more.

The zoom function is given by the button. Press it and mark the area you want to zoom in on.

To get back to the original figure, simply press .

You can move around the chart by pressing the and navigate in your history of changes by
using the arrows back and forward.
This makes it easy to explore the chart interactively.

Multi-line chart
Let’s try to add the simple moving average to our chart. This requires that we calculate them and
add them to our DataFrame.

Then it can be visualized as follows.

27

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Notice, that the DataFrame (aapl) can take a list of columns and that list will be the columns it
makes available.
This will give a chart as follows.

In your Jupyter Notebook it will be shown with the interactive setup. Here we only see the chart.
Notice, that the legend in the upper left corners shows the color coding of the lines on the chart.

Limit the time period of the chart


While you can interactively zoom into the part of the chart that has your interest, you can also
focus a specific time interval on your rendered chart.
Let’s say, you are only interested in the historic stock prices from the beginning of 2020 and
forward.

Notice how easy it is to do. Simply by adding a .loc[‘2020’:] you tell the DataFrame to take the
data from the beginning of 2020 and forward.

28

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Multiple charts in one figure


Let’s try to have multiple charts (or axes) in one figure.

Now, that was a lot of code.


First look at the first line. There is a slight difference plt.subplots(2, 2).
This will return a figure with 2-by-2 axes.
As you see on the following 4 lines, you access an axis with the variable ax indexed as a double list.
Hence, the first axis is given by ax[0, 0], the second ax[0, 1] and so forth.
The trained eye will also notice we added an argument c=’r’ or similar in each plot. This will set the
color of the line.
You can find color names in the Matplotlib documentation.
The color coding is optional and just used for demonstration purposes here.

29

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Then we have some lines of the form ax[0, 0].legend(), which again are optional. They only tell the
axis to have the legend visible.
You can also call ax[0, 0].set_title(“My title”) to set a title on your axis.
Finally, we call plt.tight_layour(). All that does is to render the axes (charts) nice to avoid overlap
of the text of the axes. You can try to comment it out and see the difference.

Project – Compare two investments


In this project we will compare two investments and visualize it. We will compare an investment in
Apple to an investment in Microsoft in 2020. That is, if you invested money in Apple on the first
trading day in 2020 and sold it all on the first trading day in 2021. How would that compare if you
had invested it in Microsoft instead.
Well, let’s figure it out together.
Step 1
First, we need to read the historic stock prices of the two investments.
We can use the Pandas Datareader to get the data.
First, we need to import the library and the datetime library.

30

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Then read the data from the API.

Step 2
We will compare the investments using the Adj Close (adjusted close) price, as it includes dividend
payout and other corporate adjustments.

Let’s see if the data looks at we expect.

Step 3
To make an easy comparison we normalize the data.

And check that all is as it should be.

Step 4
Now to the fun stuff.

31

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Visualization.

Now let’s take a minute and see what we can conclude from the figure.

Because we have normalized the data both Apple and Microsoft start at the same point in start of
2020.
This is convenient, because if we invested either in Apple or Microsoft, it will show the relative
difference on how our investment would evolve.
Said differently, for each dollar invested in Apple and for each dollar invested in Microsoft, then
we can see how it evolves over time.
Here at this stage, we are only interested in the return of our investment.
Hence, we only look at what we get at the end.
Later we will learn about volatility and maximum drawdown, which are important factors when
evaluating the risk of potential investments.
But for now, let’s investigate the return.
Step 5
Let’s calculate the return and visualize it.

32

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

As we have normalized data, we can get the return from the last day. We subtract 1 to get the
percentage growth of the investment.

Hence, if we invested in Apple, we would have gained 78.24% in 2020.


While, if we invested in Microsoft, we would have gained 39.94% in 2020.
Both good investments.
This can be visualized on a bar-chart as follows.

Which results in this.

33

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Summary
In this chapter we have learned how to use Matplotlib to visualize historic stock prices. How to
use it with multiple lines in one chart or multiple axes in one figure. Also, how to create bar charts,
which we will use later in this eBook.

34

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

03 – Buy and Hold


Warren Buffett advice to both stock market novices and rich investors to buy and hold a low-cost
fund that tracks the S&P 500 index.
The argument is that, in the long run, you will outperform the majority of most money managers.
The S&P 500 has given an average return of 10-11% per year since it was created in 1926. And an
average of 8% per year from 1956 to 2018 (source).
So why bother do anything else?
We will explore that in this chapter.
Learning objectives
• Evaluate the Buy-and-hold strategy Warren Buffett proposes
• How to calculate the CAGR – the Compound Annual Growth Rate – and what it is
• How to calculate the Maximum Drawdown
• Learn about the “Lost decade”

How to approach this


It is very tempting, right?
One of the smartest, if not the smartest, investment gurus’ advices you to invest in a low-cost fund
that tracks the S&P 500 index. It will give you an expected 8% of return per year in the long run.
The long run.
What is the long run?
Well, we will figure that out in this chapter.
This chapter will also give the foundation of how to evaluate an investment strategy.
But let’s get started and learn along the way.

Read the data


The ticker for the S&P 500 is ^GSPC. This is the index. We will later explore a low-cost fund that
tracks it. But for now, we need to explore the nature of Buy-and-hold, and what the long run
means.
First step first. Import the needed libraries.

Nothing new here.

35

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Read the data from the API.

We will read from beginning of 1999, and you will know why later in this chapter.

This gives data from 1999 and forward to current day.


And let’s visualize it. It makes it easier to get an impression of the data that way.

As you see. This looks good. Nice growth in the second half.

36

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Second half?
What about the first?

The lost decade


The years 1999 to 2009 are considered the “lost decade” of S&P 500.
Why?

The index starts at 931.80 in 2009.

And in 1999 it started at 1228.10 USD.


Wait a minute?
1228.10 in 1999 and 931.80 in 2009.
10 years and it goes down?
Let’s see this.

37

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Yup. It doesn’t look good.


10 years with no growth.
What is the key learning. The long run means at least more than 10 years.
Hence, if you have decades before you need your 8% return per year, then an investment in a fund
tracking the S&P 500 is a great idea.
Otherwise, you might end up after 10 years and see your money being less worth than the day you
invested them.

The CAGR vs AAGR


Let’s formalize a few measures when it comes to evaluating an investment.
We talked about the return in the last section.
What we really should be talking about is the Compound Annual Growth Rate (CAGR).
CAGR measures the yearly return of an investment.
The formula.
1⁄
𝑒𝑛𝑑 𝑣𝑎𝑙𝑢𝑒 𝑛
( ) −1
𝑠𝑡𝑎𝑟𝑡 𝑣𝑎𝑙𝑢𝑒
Where n is the number of years.

38

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The CAGR calculates the value you are interested in.


Some financial statements use the Average Annual Growth Rate (AAGR). This is misleading.
𝐺𝑟0 + 𝐺𝑟1 + … + 𝐺𝑟𝑛−1
𝑛
This is the sum of the growth rates over the period of n years.
Let’s have an example.
Assume you have 100 USD and you invest them.
Over four years your return is: 20%, -50%, 50%, and 30%
Let’s calculate the return after 4 years.
100 × (1 + 0.2) × (1 − 0.5) × (1 + 0.5) × (1 + 0.3) = 117
Hence, the 100 USD will be 117 USD after 4 years.
Now, let’s calculate the CAGR and AAGR.
The CAGR.
1⁄ 1⁄
𝑒𝑛𝑑 𝑣𝑎𝑙𝑢𝑒 𝑛 117 4
( ) −1 = ( ) − 1 = 0.0400314334861216
𝑠𝑡𝑎𝑟𝑡 𝑣𝑎𝑙𝑢𝑒 110
The AAGR
𝐺𝑟0 + 𝐺𝑟1 + … + 𝐺𝑟𝑛−1 0.2 − 0.5 + 0.5 + 0.3
= = 0.125
𝑛 4
So why care about CAGR.
CAGR says we get an annual return of 4% and the AAGR says 12.5%.
The AAGR looks better right?
But CAGR calculates the return we are looking for.
100 × (1 + 0.04) × (1 + 0.04) × (1 + 0.04) × (1 + 0.04) = 117
Note There is some rounding in the above calculations which are not accounted for.
What does the AAGR tell us?
For me? It is only to make the percentage look better than it is.

Calculate the CAGR for S&P 500


Now we know how to calculate the CAGR on paper, how do we do it with Pandas DataFrames.
Let’s do that for the period of 1999 to 2009.
We will take the 10 years of data beginning from 1999 and ending last trading day in 2008.

The total return can be calculated as follows.

39

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Giving the following.

This means that we lost money. We lost 1 – 0.735 = 0.265 or 26.5%.


The CAGR can be calculated as follows.

That is a loss of 3% per year.


If I in 1999 invested all my money in a low-cost fund tracking the S&P 500, I would lose 3% per
year or 26.5% in total loss over the next 10 years.

Maximum Drawdown
“A maximum drawdown (MDD) is the maximum observed loss from a peak to a trough of a
portfolio, before a new peak is attained. Maximum drawdown is an indicator of downside risk over
a specified time period.” – Investopedia.org
In this first stage we will calculate the maximum drawdown over the full period of 10 years. Later
we will be calculated it as a rolling value of a period of 1 year.
For the specific study here, it will not make any difference.
How do we get the maximum and minimum value cumulated over the period?
That is actually what we are looking for.

The rolling_max will at all times keep the maximum value seen so far.
The daily_drawdown keeps the current maximum drawdown.

Remember, the DataFrame data keeps the 10 years we are investigating.


Hence, the maximum drawdown is 51.9%.
When investing you are looking for increasing the CAGR, while keeping the maximum drawdown
low.

40

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Why? Because the maximum drawdown tells you how much money you can lose, based on
historic values.
In the next chapter we will learn about volatility of a stock.
The full picture will be to maximize the return (CAGR) and minimize the maximum drawdown and
volatility of the stock.

Project – Calculate the CAGR and Maximum Drawdown of S&P 500 from
2010 to 2020
In this project we will make the same calculations for the 10-year period form 2010 to 2020.
Step 1
First, we need the data for that period.

Step 2
Then we calculate the CAGR.

That looks better. A 11% annual return.


Step 3
Finally, we calculate the maximum drawdown.

That is a maximum drawdown on 19.8%.

Summary
In this chapter we have started our journey to learn more about investing. We have explored the
advice of Buy-and-Hold a low-cost fund tracking the S&P 500. This led us to discover that 10 years
might pass without any earnings, like the “lost decade” 1999 to 2009.
We have formalized the return calculations CAGR, which is a primary interest when investing. To
learn about the risk side of an investment, we introduced the concept of maximum drawdown.
The maximum drawdown tells us how much money we might lose.
In the next chapter we will learn about volatility of a stock, which is the second risk measure we
use when evaluating an investment strategy.

41

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

04 – Volatility
In this chapter we will learn about the volatility of a stock.
“Volatility is a statistical measure of the dispersion of returns for a given security or market index.
In most cases, the higher the volatility, the riskier the security. Volatility is often measured as either
the standard deviation or variance between returns from that same security or market index.” –
Investopedia.org
A volatile stock is considered risky. It is common to assume that volatility is a good measure for
risk in investment.
Learning objectives
• Different measures of volatility.
• Learn about log return – needed for the calculations.
• How to calculate the volatility of an investment.
• A way to visualize the volatility for better understanding.

Volatility – not just one definition


If you read the quote from Investopedia.org above, you would already notice something.
“Volatility is often measured as either the standard deviation or variance between returns…”
Already hinting at two different ways to calculate the volatility.
Also, if you continue reading on Investopedia.org, you will notice that they talk about how the
Average True Range (ATR) can be used as a measure for volatility (see the video).
Further down, they talk about the Beta and more.
Luckily, one measure of volatility is enough for our purpose.
This is just to make you aware of the different ways to calculate volatility.

Log returns
What about this log?
Why bother about the log?
What is the log?
All good questions.
The answer. They make our calculations easier and faster.
It all boils down to two things about the log returns.
log(𝑎 × 𝑏) = log(𝑎) + log (𝑏)
And

42

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

𝑎 × 𝑏 = exp(𝑙𝑜𝑔(𝑎 × 𝑏)) = exp(log(𝑎) + log(𝑏))


That is the magic.
It can save us from expensive multiplications. Don’t worry if it still confuses you.
Let’s take an example before we jump to Jupyter Notebook.
Consider the following investment, which returns 20%, 15%, 10%, and 30%.
This can be calculated as a total return of.
(1 + 0.2) × (1 + 0.15) × (1 + 0.1) × (1 + 0.3) = 1.9734
That is a return of 97.34%.
Now let’s calculate it differently.
log(1 + 0.2) + log(1 + 0.15) + log(1 + 0.1) + log(1 + 0.3) = 0.6797579434409292
And
exp(0.6797579434409292) = 1.9734
It is correct.
But isn’t it just more complex?
Yes, you are right. At first sight it looks like just adding complexities.
Stay with me here, because it becomes handy later.
Let’s first jump to Jupyter Notebook and see how it can be used.

Log returns calculations with Pandas and NumPy


NumPy you ask?
NumPy is the fundamental package for scientific computing with Python. Actually, Pandas is built
upon NumPy.
The reason we need it, is to access some methods in it, which are not available directly on the
DataFrame objects.
Luckily, Pandas DataFrames integrate well with NumPy as you will see in a moment.

Importing the libraries. The new one is NumPy.

Reading the data from the “lost decade”.


Now to the interesting part. We need to calculate the log returns of the prices.

43

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

What we use is the daily change. This can be accessed by taking the current price divided by the
previous price.
If we take the logarithm of that, we get the daily log return (called the Log returns in below).

The shift() shifts the data one place forward. Hence, the rows are shifted one day forward.
Here the Adj Close price from the current day is divided from the previous day. This is done for all
days.
Then we have taken the np.log, which applies the logarithm on all rows.
Now let’s see how this connects to the return as we know how to calculate.

The sum of all the log returns is.

If we apply the exponential function on that value we get the same return.

Wow. Check that out. We can add the daily log returns together and get the same return.
This still seems a bit counterintuitive to do.
Later when we shift our investment from one stock to another, it comes in handy to just add the
daily log returns together to find the full return.
That makes it really powerful.
It makes it easy to calculate the return of investment strategies, which have different investments
portfolios from day to day.
If it is not entirely clear now? Don’t worry, we’ll get there later and you will see the advantage of
using log returns (daily log returns).

Normalized data and log returns


Let’s clarify the mystery between normalized data and log returns.
We keep the data in a column called Normalize.

Then we calculate the exponential value of the cumulated sum of the log return.

44

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Okay, we introduced two new things here.


First, the cumsum() calculates a cumulated sum of the column it is applied on. Here, we apply it to
the log returns. Then we apply apply(np.exp) on that column. As we know np.exp takes the
exponential function of the value.

As we see. This is exactly the same value.


Surprised?
Not if you think about it.
In the last one we actually specifically calculated the last row. Investigate that and see for yourself.
All we done here is to do it for each day in a cumulated way.

Volatility calculation
This is all we have been waiting for, right?

What?
The annualized standard deviation is used as volatility measure.
Let’s break it down.
The data[‘Log returns’].std() returns the daily standard deviation.
To annualize it we need to multiply by the square root of the number of days.
There are calculated on average 252 trading days. Hence, 252**0.5, is the square root of 252 and
we get the annualized standard deviation, which we use as volatility measure.
How to interpret the result?
It is a comparison. The lower the better.
As you will see later, we will use the S&P 500 index as our basis for the return, maximum
drawdown and volatility. If figures are worse than the S&P 500 in all aspects, we gain nothing.

45

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The goal is to maximize the return (CAGR), and minimize the maximum drawdown and volatility.

Project – Visualize the volatility of the S&P 500 index


In this project we will visualize the volatility of the S&P 500 index. This will help us understand
what volatility means.
Step 1
We need to import Matplotlib to visualize it.

Step 2
Convert the volatility into a string.

This is just needed to make the diagram nice with titles.


Step 3
The actual plot.

We use the histogram to make it (hist) and use 50 bins, an alpha of 0.6 (this makes it a bit
transparent, not strictly needed), and the color blue.
We also set labels (set_xlabel and set_ylabel) and titles (set_title).

46

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The volatility says something about how these bins are spread and how close the majority of data
is to 0.0.
Remember that we are looking at the daily log return here. The higher the daily log return, the
more volatile it is. Hence, if we have a log of values that are far from 0.0, then it is volatile. If all
values are close to 0.0, it is not as volatile.

Summary
In this chapter we learned about two important concepts. The log return and volatility. First, the
log return is a great way to calculate return of. It will be handy, when we calculate returns of
changing investments. Second, the volatility of an investment is a measure of risk.
We now have the tools to evaluate an investment strategy. The return (CAGR), which should be
optimized, the maximum drawdown and volatility, which should be minimized.

47

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

05 – Correlation
In this chapter we will explore an important concept when talking about investment strategies.
“Correlation, in the finance and investment industries, is a statistic that measures the degree to
which two securities move in relation to each other. Correlations are used in advanced portfolio
management, computed as the correlation coefficient, which has a value that must fall between -
1.0 and +1.0.” – Investopedia.org
Our goal is to find ways to minimize the maximum drawdown and volatility.
A great way is to find investments with negative correlation.
Why?
Because when the price of one goes down, the other is expected to go up.
Learning objectives
• Understand negative and positive correlation.
• How negative correlation can minimize the maximum drawdown and volatility.
• How to calculate the correlation.
• Visualize negative correlated investments.

What correlation tells us


The correlation tells us how stock prices move in relation to each other. That is, given a stock A
and a stock B, how are the prices related to each other.
The correlation between two stock prices is a value between -1.0 and 1.0 (both inclusive).
A perfect positive correlation means that the correlation coefficient is exactly 1. This implies that
as the price of stock A goes up, the price of stock B moves up in lockstep. Similarly, if the price of
stock A goes down, the price of stock B moves down in lockstep.
A perfect negative correlation means that two stocks move in opposite directions, while a zero
correlation implies no linear relationship at all.
Normally, the correlation is not perfect nor no relationship at all.
It is important to notice that a positive correlation does not promise that when stock A goes up, so
does stock B. It is simply expected to happen on average.
The same holds for negative correlation.

Why do we care about correlation?


We are tying to minimize the maximum drawdown and volatility of an investment.
If our initial investment portfolio has a large maximum drawdown, then we can minimize that if
we add investments with negative correlation to our portfolio.

48

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The best way to understand this is by example.


Let’s get started in our Jupyter Notebook.

We start by importing all the libraries we know.


We will explore the SPY and TLT tickers.
SPY is a SPDR S&P 500 trust is an exchange-traded fund (ETF), which trades on the NYSE Arca.
“An exchange traded fund (ETF) is a type of security that tracks an index, sector, commodity, or
other asset, but which can be purchased or sold on a stock exchange the same as a regular stock.”
– Investopedia.org
“Spider (SPDR) is a short form name for a Standard & Poor's depository receipt, an exchange-
traded fund (ETF) managed by State Street Global Advisors that tracks the Standard & Poor's 500
index (S&P 500).” – Investopedia.org
SPY is exactly what we are looking for – a low cost fund that tracks the S&P 500.
TLT is the iShares 20+ Year Treasury Bond ETF seeks to track the investment results of an index
composed of U.S. Treasury bonds with remaining maturities greater than twenty years.
Let’s investigate them and see how they correlate.

We will explore them over the 10 years from 2008 to 2018 (last day of December 2017).
As we know, we use the Adj Close.

Then calculate the log return.

The big question is how do we calculate the correlation?


Luckily, Pandas DataFrames provide it with a single call to the method corr().

49

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

As we see, SPY has perfect positive correlation with SPY.


That is of course obvious, as it is the same prices.
But notice the correlation between SPY and TLT.
It is negative. This is what we are looking for.
Let’s try to visualize that. But we want to do it in a normalized way, as the following does.

Which results in this.

Now this is interesting.


We see that when the price of SPY goes down during the second half of 2008, the price of TLT
goes up.
This looks pretty good.
For the fun of it, let’s zoom in on May 2008 until May 2011.

This gives the following.

50

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

This period looks almost too good to be true. While the SPY goes down 2008 and recovers in 2011,
the TLT goes almost opposite.
“A hedge is an investment that is made with the intention of reducing the risk of adverse price
movements in an asset.” – Investopedia.org
In the next chapter we will look at how to make a portfolio based on SPY and TLT.

Project – Calculate the return (CAGR), maximal drawdown and volatility


of SPY and TLT
In this project we will explore how SPY and TLT looks like individually from an investment
perspective. In next chapter we will explore how a combination of SPY and TLT looks like.
Step 1
Let’s start by calculate the return (CAGR).

This shows that the annual return of SPY is 8.5% and 6.3% for TLT.

51

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Step 2
To calculate the maximum drawdown, we first define a function.

Then we can call the function.

This shows that the maximum drawdown is 51.8% for SPY and 26.6% for TLT.
Step 3
We calculate the volatility of the log returns.

This shows that the volatility of SPY is 20.3% and 15.2% for TLT.
In the next chapter we will see how a combination of SPY and TLT affects the values.

Summary
In this chapter we have explored correlation. If two stocks are negatively correlated, we expect
that if the price goes up of one the price goes down of the other.
We found that SPY and TLT are negatively correlated.

52

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

06 – A Simple Portfolio
In the last chapter we realized that SPY and TLT are negatively correlated. Here we will explore if a
simple portfolio of a 50-50 percentage split of SPY and TLT will improve our investment in terms of
return (CAGR), maximum drawdown, and volatility.
The project in this chapter will investigate how an annual rebalancing will affect the result.
Learning objectives
• Calculate the return (CAGR), maximum drawdown, and volatility of a portfolio
• Compare the result to SPY.
• Investigate the portfolio visually.
• Make a rebalancing approach and compare it.

The simple portfolio


We have identified that SPY and TLT are negatively correlated.
The calculations from last chapter showed that during 2008 to 2018 we have the following figures.

CAGR Maximum drawdown Volatility

SPY 8.54% 51.9% 20.3%

TLT 6.33% 26.6% 15.2%

The maximum drawdown of SPY is scary, losing 51.9% is not what we want to experience.
This makes the period of 2008 interesting. How can we avoid a maximum drawdown like that?
In this chapter we will explore if a 50-50 percent split between SPY and TLT would improve on the
above figures.
First, we will make a simple Buy-and-hold approach with the split. Then in the project we will
make an annual rebalance of the split. This helps to avoid a that the split drifts far away from the
initial 50-50 percentage split.
Say, if we start with a 50-50 percentage split of SPY and TLT. Imagine SPY drops 50% and TLT
raises 50%. Then the split is 25-75 percentage between SPY and TLT.

50-50 split without rebalance


To make this evaluation we need to import some libraries.

53

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

And read the data from the API.

Keep the Adj Close.

Make the portfolio.

Let’s visualize the portfolio with the SPY and TLT.

Notice the .sum(axis=1), it sums along the rows. Hence, it takes the values of the portfolios and
sums each row up. This results in the normalized values of the portfolio.
Resulting in this figure.

54

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The blue line is our portfolio.


It is interesting to see that we avoid the big drawdown SPY encounters during 2008 and 2009.
This could look like we actually benefit from this approach.
Let’s calculate the figures.
To do that, we will create a series with the strategy and one with log returns.

Then we can calculate the CAGR.

This is a bit lower than SPY, which has a return of 8.54%.


The maximum drawdown.

Now that is a great improvement. The SPY had 51.9% and here we get 18.3%.

55

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Finally, the volatility.

Again, SPY had 20.3% and here we have 8.85%.


What we have gained for the price of a lower return is safety. A great improvement on maximum
drawdown, which says we could lose 18.3% comparing to 51.9%. The volatility is also greatly
improved by this strategy.
Can this strategy be improved?
Well, we talked about it. There is a problem with this strategy. The split of 50-50 percent will drift
away over the years.
Hence, in the project of this chapter we will make a yearly rebalance and see how it affects the
numbers.

Project – An annual rebalance


We will stay with SPY and TLT, but now we will rebalance the portfolio annually.
Step 1
Here we will make a rebalance the end of the year.
Let’s look at the code first and see what it does.

On a top level we iterate over the years and make a 50-50 percentage split of the portfolio.
The only thing we need to adjust for is the return from previous year. This is the if statement (as
we should not do it the first year).
Hence after the for-loop we have a list concat where we have the yearly rebalanced portfolio
adjusted from previous year return.
The pd.concat(.) simply concatenates the list concat into one Series.
Now that was nice.
Step 2
We can do as we did above to calculate the CAGR.

56

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Step 3
The maximum drawdown.

Step 4
The volatility.

Step 5
Visualize it.

Resulting in this chart.

The green line represents our rebalancing approach.

57

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

We see a difference already. At the far end of 2018 we end up at the same point as SPY (or fairly
close).
Did we lose our advantage?
Let’s compare it here.

CAGR Maximum drawdown Volatility

SPY 8.54% 51.9% 20.3%

TLT 6.33% 26.6% 15.2%

SPY-TLT split 7.49% 18.3% 8.85%

SPY-TLT rebalance 8.51% 20.3% 8.89%

Now that looks good.


We basically have the same return as SPY and keep the maximum drawdown low as well as
volatility.

A word of warning and how to avoid it


Backtesting is based on historic data and cannot predict the future. The above strategy was
presented to me and calculated over the time period of 2008 to 2018, as we have done here.
The reason is most likely that we are interested in periods where the market crashed. We want to
test up against that.
Most strategies are presented on paper and have to be recalculated manually.
Therefore, we often think that the presented strategies have been tested in all possible scenarios.
This is often a false assumption.
This eBook was written such that you can test (backtesting) the same strategies until you feel
comfortable with them.
Let’s run the above strategy over the 10 years starting from 2011 and ending in 2020 to see how it
performs.

CAGR Maximum drawdown Volatility

SPY 13.6% 33.7% 17.3%

TLT 8.2% 20.7% 14.5%

SPY-TLT split 11.2% 18.2% 8.66%

SPY-TLT rebalance 11.0% 16.0% 8.00%

58

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

It paints a different picture.


The strategies (SPY-TLT split and SPY-TLT rebalance) did not return (CAGR) as well as SPY alone.
It is a decent return of 11%. It still greatly improves the maximum drawdown and volatility over
the SPY, which lowers the risk of your investment.

Summary
In this chapter we explored our first strategy with a 50-50 percent split between SPY and TLT.
With backtesting we saw that this strategy keeps a decent return (CAGR), while lowering the risk
significantly (maximum drawdown and volatility). This can be further improved if we add an
annual rebalancing of the portfolio.
Finally, we looked at the importance to test (backtesting) presented strategies yourself. Often
strategies are presented under the best circumstances and do not show the full picture.

59

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

07 – The Asset/Hedge Split


In the last chapter we used the 50-50 percentage split between SPY and TLT. The asset is SPY, and
the more we allocate to SPY, the higher return. TLT is the hedge and provides safety, while
sacrificing return.
What ratio should we use?
It turns out that most financial advisors suggest a 60-40 percentage split. 60 percent to the asset
and 40 percent to the hedge.
In this chapter we will explore how different splits perform for our portfolio of SPY and TLT. It will
not conclude the 60-40 split as the best, but remember, we are looking at a concrete example and
the advice is more general.
Learning objectives
• Evaluate different splits (ratios) of an asset-hedge.
• Calculate it specifically for SPY and TLT
• Visualize the figures of the different SPY and TLT splits.
• Calculate and visualize it for Sharpe Ratio.

The calculations
The calculations in this section can be done for any type of asset/hedge split. We will continue our
journey with the SPY and TLT over the 10 years period of 2008 and 2018.

Let’s read the data.

As we need to evaluate different splits and are interested in the same calculations we will create a
function which returns the results.
The calculations are the same we have done in previous chapters.

60

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The function takes the data and split. The split is a value assigning the ratio to the asset.
Notice, that we do not make the rebalancing here. That is a great exercise you can do on your
own.
To see the 50-50 split simple call the function as follows.

What we want here, is to calculate these 3 values for all possible splits.
This can be done as follows.

We create an arange (a NumPy range) from 0 to 1 (inclusive) stepping 0.05.


From that we create a DataFrame to keep all the data.
The apply calls the lambda function, which applies our function from last step. Then we map out
the result to our DataFrame. Finally, we set the index to be the column with the split values.
You can see the full dataset with the following command (we only show the first few lines here).

Then we can visualize the result as follows.

61

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Resulting in this chart.

This is a great way to get an impression of the different splits.


The x-axis shows the split. In the far left we have split 0.0 (0% to SPY and 100% to TLT) and in the
far right we have the 1.0 split (100% to SPY and 0% to TLT).
We see the return (CAGR) increases as the split favors SPY. This is exactly as stated in the
introduction.
We see the lowest volatility is somewhere in the middle. Also, the optimal maximum drawdown
(it is negative) is somewhere in the middle.
This means, that in this specific evaluation the optimal split is somewhere in the middle if only
considering maximum drawdown and volatility.
More precisely with code.

The highest return (CAGR) is with 100% to SPY and 0% to TLT.

62

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

The lowest volatility is at a 50-50 percent split.

To get the optimal maximal drawdown we should have a 45-55 percentage split to SPY-TLT.
We can get a small window of the numbers here.

What can we conclude?


That the optimal split for this specific evaluation is somewhere in the middle. This is actually great
news. We see that it confirms the financial advisors’ suggestion. Also, we can see that maximal
drawdown and volatility are optimal around there.

Project – Sharpe Ratio


A common way to evaluate a portfolio is with Sharpe Ratio.
“The Sharpe ratio was developed by Nobel laureate William F. Sharpe and is used to help investors
understand the return of an investment compared to its risk.” – Investopedia.org
The formula is as follows.
𝑅𝑒𝑡𝑢𝑟𝑛 𝑜𝑓 𝑝𝑜𝑟𝑡𝑓𝑜𝑙𝑖𝑜 − 𝑅𝑖𝑠𝑘𝑓𝑟𝑒𝑒 𝑟𝑎𝑡𝑒
𝑆ℎ𝑎𝑟𝑝𝑒 𝑟𝑎𝑡𝑖𝑜 =
𝑠𝑡𝑎𝑛𝑑𝑎𝑟𝑑 𝑑𝑒𝑣𝑖𝑎𝑡𝑖𝑜𝑛
The goal is to optimize the Sharpe ratio.
We already have the return of the portfolio and the standard deviation.
For the risk-free rate the U.S. Treasury rate is often used. Sometimes, even 0 is used, as the rate
has been very low.
For the example we will use the 10 years U.S treasury yield. You can find updated values here.
Here we will use 1.65%.

63

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

One thing to notice, the Sharpe Ratio does not take the maximum drawdown into account.
Step 1
Calculate the Sharpe Ratio.

Notice, we do it simultaneously for all possible splits.


Step 2
Find the optimal split.

Hence, this is the 50-50 percent split (we see it in next step).
Step 3
Visualize and show the value of the optimal split.

Resulting in the following figure.

This shows the chart of the Sharpe Ratio according to the different splits. The x-axis is showing the
same as previous.

64

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

We see the optimal values above (the index from step 2).
What can we conclude?
Well, the theory of Sharpe Ratio also confirms that a split somewhere in the middle seems to be
optimal. Remember, Sharpe Ratio tries to optimize the return in regards to risk.

Summary
In this chapter we have explored if our findings concur with the financial advisors of a 60-40
percentage split for asset-hedge. On the concreate calculations we found a split in the middle to
be optimal. This means that the minimal risk is somewhere in the middle.
To give a precise general split would require more calculations and is outside the scope of this
eBook. That said, our findings are not contradicting the general advice.

65

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

08 – Backtesting an Active Strategy


In this chapter we will evaluate a active strategy and perform backtesting on it.
The strategy will use the simple moving average (MA) of 200 days. When the current market price
of SPY is above MA we go long (buy and hold SPY), when the current market price of SPY is below
MA, we short and buy and hold TLT.
Learning objectives
• How to evaluate an active strategy as above.
• Perform backtesting for an active strategy.
• Create a visual evaluation from the backtesting.
• Drawdown lesson from 2008 in backtesting

Getting started with an active strategy


The strategy explained
Let’s first be sure we understand the strategy.
On a high level, we either have all our holding in SPY or in TLT. This means, at all times during the
strategy we will either have 100% in SPY or 100% in TLT.
The trigger for a change is based on the current SPY price and 200 days moving average of the
price.
If the current SPY price is above the 200 days moving average, we will have 100% in SPY.
On the other hand, if the current SPY price is below the 200 days moving average, we will have
100% in TLT.
How can we model that?
Getting started
As usual, we need some libraries.

And read the data from the API.

66

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

We have a longer time span than usual, this is to be able to make different backtesting frames.
The signal-line
How do we model the strategy?
We can create a signal-line.

We first calculate the moving average (ma), then we have the signal line to be the difference
between the price of SPY and ma. This will make signal_line positive when we should have our
holdings in SPY and negative when we should have our holding in TLT.
The apply(np.sign) simply transform the sign to either 1 or -1. Hence, if 1, we hold SPY, and if -1,
we hold TLT.
Log returns of the strategy
Then we calculate the log returns.

This is needed to calculate the return of our strategy.

Now that looked too easy to be true.


Let’s break it down.
The signal_line.clip(lower=0) takes all the positive values (all the 1’s) of signal_line. First leave out
the shift(1), we’ll get back to it.
Hence, the we take all the 1’s from signal_line and get the log returns of SPY. This is all the returns
we have from holding SPY.
Why the shift(1)?
As we know, when current price shifts above the moving average, we cannot react on that day.
The calculations are done on the closing prices. Hence, we can first react first thing next morning.
Hence, this is our reaction time.
Is it 100% accurate? No, because we cannot be ensured the price at opening the day after. But this
is the best we can do in our calculations.
The second line.

As you see, it is similar and still a bit different. It takes and subtracts all the values from TLT. This is
needed as the signal_line here is negative and we want to add the log return of TLT.

67

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Notice, that we also use clip(upper=0) to get all the negative signal values (all the -1’s).
Now we have the log return of the strategy.
The cumulative return of the strategy
To calculate the cumulative return of the strategy you can use the cumsum() and apply(np.exp).

This sums the cumulative of the log returns and applies the exponential function on it. This will
calculate the normalized return of the strategy.
Similarly, you can do the same for the SPY.

This comparison can be visualized with the following code.

Resulting in this figure.

68

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Hence, this looks like our strategy performs better than SPY.
Let’s make a project and learn more about it.

Project – Backtesting different periods and visualize results


In this project we will create helper functions to achieve this. First a function that calculates the
CAGR, maximal drawdown, and volatility for a given period. Then we use that function to
calculate what we are looking for. Finally, visualize the result in a nice way.
Step 1
First, we will create a function, which calculates the CAGR, maximal drawdown, and volatility for
a given time period.

69

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Step 2
Let’s call the function for with SPY for the period 2008 to the end of 2017.

These figures look familiar.


How does our strategy perform?

Now this looks pretty good. A better return 10.2% CAGR, 25.3% maximum drawdown, and 14.5%
volatility.
Step 3
Now for the big step.
How do we visualize this in a good way?
Let’s jump into it and try.

That looks intimidating at first.


But let’s break it down.
The function has a helper function inside (x_titles()), which creates the x titles in our diagrams.
Then it calculates the values for SPY and our Backtest.
The rest of the code is creating the figure from Matplotlib. We create 4 axes in our figure. One for
the overall performance, one for CAGR, one for maximum drawdown, and one for volatility.

70

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Let’s try it.

Which results in the following chart.

This looks good, right?


Our strategy outperforms the SPY in all aspects. Nothing to discuss, there is no reason not to go all
in with this strategy.
Or is there?

The learning from 2008 backtesting


The year 2008 was interesting for many reasons.
• Cyprus and Malta adopted the Euro.
• Iran launched a rocket to space.
• A NASA spacecraft becomes the first to land on northern polar region of Mars.
To name a few things.
But also, the S&P 500 dropped over 50%.

71

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

That was a big loss and it took long to recover. Many investors lost a great deal of money. Image
you had your life savings of 1,000,000 USD in the start of 2008 and ready to retire. Later that year
you would most like have less than 500,000 USD.
That hurts.
Hence, it is no great surprise that backtesting strategies over 2008 has been of great interest. How
do avoid such a big loss?
This is the maximum drawdown factor at play. We want to minimize it to lower the risk.
What this eBook should teach you is to make backtesting on your own and not trust others
findings.
When a backtesting performs good over 2008 it actually has a great advantage.
Why?
Let’s just look at the backtesting we just did and redo it over another period (yes, we made that
easy to do).

Let’s see how 2011 until the end of 2020 looks like.

Not bad. But not as good as 2008 to and 10 years forward.


Well, there is one thing to understand.
72

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Let’s simplify it a bit.


Assume 2008 ended with 50% loss in SPY.
If your strategy broke even in 2008.
Then your strategy has a 100% advantage.
What?
Yes, the SPY needs to grow 100% again before it is on the level of your strategy.
100% growth is a lot and a great advantage.
Still confused. Let’s break it down.
If you invested 100 USD in SPY in 2008, in the end you would have 50 USD.
If you invested 100 USD in your strategy in 2008, in the end you would have 100 USD.
Hence, in 2009 your strategy has a 100% advantage.
100
−1=1
50
That means, the SPY needs to grow 100% to break even with your strategy.
If your strategy grows 50% in 2009, you will have 150 USD.
Then SPY needs to grow 200% in 2009 to break even to 150 USD.
You see, this is a huge advantage for your strategy.
Hence, if your strategy can just break even from the big loss in 2008, it doesn’t need to perform
perfect afterwards as it has a great advantage over the SPY.

Summary
In this chapter we have explored our first active strategy. We learned a great way to calculate the
return of a strategy. Using this return to make the actual backtesting. Additionally, we created a
nice way to visualize a backtesting with a SPY comparison.
Finally, we had a discussion on how the strategies beating the 2008 market collapse has a great
advantage in the following time.

73

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

09 – Backtesting Another Strategy


The strategy we will explore in this chapter comes from the book THE 12% SOLUTION by David
Allan Carter.
We will not implement the full end solution, but pretty much the whole thing. The reason for not
implementing the full solution is to keep the last details of the strategy in his book.
The exploration of his strategy starts in 2008. We will do the same here and extend it afterwards.
Learning objectives
• Understand the 12% solution.
• Show how we can implement the 12% solution.
• Perform backtesting for of the 12% solution
• Conclude if the picture changes if we change backtesting time frame

The 12% solution described


Here we will only explore a version of the 12% solution. There is a more involved solution in the
book.
The book builds up a strategy step by step in a nice and easy way. Here we will only explore the
solution, which should return 12% on average over 10 years.
Simply explained.
There is a 60-40 percent assets-hedge split.
The hedge is kept in TLT and the assets in one of the following SPY, IWM, MDY, QQQ, or cash.
In the end of every month you can change your holding between one of them. The decision is
based on a 3-month lookback of the best performing choice (including the 0-return of cash).

Implementing the 12% solution


This is a monthly strategy. We only make changes on a monthly basis and based on a 3-month
lookback. Hence, we only need monthly data for our backtesting.
That sounds like something new and something we like to try.
Luckily, the Yahoo! Finance API can help us there.
But first, we need to import some libraries.

Then we read the monthly data.


74

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Notice the interval=’m’, which will read monthly data instead of daily data.
When we create our strategy, we should also have the option to hold cash. This can be modeled
by adding a column with no growth.

Here we call the column Short and assign the full column to the value 1. This will give a 0 in return
and no growth if we keep money in it.
For convenience, we reorder the columns on the second line.
As usual, we calculate the log returns.

Now we need to find the return of the 12% solution.


We need to figure out which assets retuned the most in the last 3 months.

We simply calculate the rolling sum over the last 3 months for all of them. Notice, we have
excluded TLT from our calculations, as it will only have a 40% holding and not change.
This makes us ready to calculate the monthly return.

Yes, that is it. On the first line we simply take the maximum of the rolling_sum, which contains the
last 3 months return. We shift it, as we can first make the decision in the end of the month (we
still do not know how to look into the future).
We multiply it by 0.6 and add the log return of TLT multiplied by 0.4 to make the split.
Let’s evaluate this.

Project – Backtesting the 12% solution


This is basically adjusting last chapters code to monthly based data.

75

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Step 1
Adjust the calculation function to use monthly data.

We make sure the drawdown is looking at the full picture and the volatility is now over the last 12
months and not 252 trading days.
Step 2
Copy the code of the visualization from last chapter.

Yes, this code does not need adjustment.


Step 3
Let’s do some backtesting. First for the period 2008 and 10 years forward.

Resulting in the following.

76

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Well, on paper it looks pretty solid here. A return of 10.2%, a low maximum drawdown (7.2%) and
low volatility (8.0%).
Not the fully 12% the solution is named after, but we have left out some details, like rebalancing
which actually can add some.
Let’s try it for 2011 and 10 years forward.

This results in the following.

77

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Still good. A return of 11.7%, though during this 10-year period the SPY returned 13.8% (calculated
on a monthly basis). Low drawdown and volatility.
Remember the comments from last chapter about the evaluating over 2008. It gives a great
advantage to beat 2008.

Summary
In this chapter we have explored a version of the 12% solution (not the final version in the book).
We have found it is not by default outperforming the return of the last 10 years, but still keeping
the risk in form of maximum drawdown and volatility lower than the SPY.

78

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Next Steps – Free Video Courses


The purpose of this eBook is to make you able to make your own backtesting. This way you do not
depend on other people’s impressive figures from a strategy. You can calculate them yourself and
verify them over other periods.
My experience shows that most simple and impressive looking strategies are not as impressive
when you do the backtesting in a broader spectrum.
Most backtesting shared with the public only shows the best angle of an investment strategy.

Free Online video courses


Do you want to learn more about Python for financial analysis?
…and for FREE?
Then I have created two free courses with financial analysis.
Python for Finance: Risk and Return

Learn Python for Finance with Risk and Return with Pandas and NumPy (Python libraries) in this
2.5 hour free 8-lessons online course.
The 8 lessons will get you started with technical analysis for Risk and Return using Python with
Pandas and NumPy.
The 8 lessons
• Introduction to Pandas and NumPy – Portfolios and Returns
• Risk and Volatility of a stock – Average True Range
• Risk and Return – Sharpe Ratio
• Monte Carlo Simulation – Optimize portfolio with Risk and Return
• Correlation – How to balance portfolio with Correlation
• Linear Regression – how X causes Y

79

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

• Beta – a measure of a stock’s volatility in relation to the overall market.


• CAPM – Relationship between systematic risk and expected return
Read more on the course page and see the video lectures.
The code is available on GitHub.
Financial Data Analysis with Python

Learn Python for Financial Data Analysis with Pandas (Python library) in this 2 hour free 8-lessons
online course.
The 8 lessons will get you started with technical analysis using Python and Pandas.
The 8 lessons
• Get to know Pandas with Python – how to get historical stock price data.
• Learn about Series from Pandas – how to make calculations with the data.
• Learn about DataFrames from Pandas – add, remove and enrich the data.
• Start visualize data with Matplotlib – the best way to understand price data.
• Read data from APIs – read data directly from pages like Yahoo! Finance the right way.
• Calculate the Volatility and Moving Average of a stock.
• Technical indicators: MACD and Stochastic Oscillator – easy with Pandas.
• Export it all into Excel – in multiple sheets with color formatted cells and charts.
Read more on the course page and see the video lectures.
The Code is available on GitHub.

80

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

A 21-hour course Python for Finance

Did you know that the No.1 killer of investment return is emotion?
Investors should not let fear or greed control their decisions.
How do you get your emotions out of your investment decisions?
A simple way is to perform objective financial analysis and automate it with Python!
Why?
• Performing financial analysis makes your decisions objective - you are not buying
companies that your analysis did not recommend.
• Automating them with Python ensures that you do not compromise because you get tired
of analyzing.
• Finally, it ensures that you get all the calculation done correctly in the same way.
Does this sound interesting?
• Do you want to learn how to use Python for financial analysis?
• Find stocks to invest in and evaluate whether they are underpriced or overvalued?
• Buy and sell at the right time?
This course will teach you how to use Python to automate the process of financial analysis on
multiple companies simultaneously and evaluate how much they are worth (the intrinsic value).
You will get started in the financial investment world and use data science on financial data.
Read more on the course page.

81

Copyright © 2021 learnpythonwithrune.org


PYTHON FOR FINANCIAL ANALYSIS

Feedback
Any feedback and suggestions are welcome.
You can contact me on
• Twitter: @PythonWithRune
• Facebook: @learnpythonwithrune
• Email: [email protected]
Disclaimer
The content of this eBook and any associated resources are provided for educational purposes
only. You assume all risks and costs associated with any trading you choose to take.
Version history
Version 0.9.2.1 – updates (2021.03.20)
Version 0.9.2 – updates (2021.03.15)
Version 0.9.1 – updates (2021.03.14)
Version 0.9 – first release (2021.03.13)

82

Copyright © 2021 learnpythonwithrune.org

You might also like