Python for Finance
Table of Contents
A couple notes:
- When watching the videos, be sure to follow along in your copy of the Colab Notebooks.
- When you modify a Colab notebook it automatically creates a new notebook (with a new link). This means you can always go back to the original Colab Notebooks using the links below. So don't hesitate to modify your copy—you learn best by experimenting.
- Most courses in using Python in Finance use stock data and create trading algorithms. This is a very niche skill. Here we are going to focus on core financial analyst tasks, e.g. what is the average PPP loan size in Texas? These are common tasks, and are increasingly being answered in Python rather than Excel.
- There are exercises at the end of each session. Be sure to complete them.
Module 0: Introduction and Setup
Week 0
- Course Overview:
- What we will cover in the course.
- The use of Python in finance.
- Python Relative to other tools.
- Excel
- Other Programming Languages
- Comparison with R
- Using Javascript for Visualization
Week 1
Session Video (Part 1) Colab Notebooks and Basic Arithmetic
Session Video (Part 2) Defining Functions and Data Types
- Interacting with Python
- Google's Colab and Jupyter
- Introduction to markdown
- Sharing notebooks
- Google's Colab and Jupyter
- Intro to the Linux Server
- Creating Functions and Time-Value-of-Money calculations
- Basic control flow
- The importance of indentation
Week 2
- Sharing code and the Python ecosystem.
- Python Packages
- Extending the core language
- Our most used packages.
- Installing and Loading.
- How to call methods/functions from a package.
- Package security.
Module 1: Pandas and Spreadsheet (tabular) Data
This module focuses on corporate finance related tasks. Accordingly we read data from spreadsheet and Dataframe structures.
Week 3
Exercise: Is there a correlation between industry institutional ownership and return on equity?
- An intro to Pandas
- Reading csv and excel files over http.
- Data cleaning.
- Inspecting the objects
- Determining dataframe size and the column types.
- Adding new columns
- A note of functions vs methods.
Week 4
Exercise: What is the average on and off-peak real-time fuel mix?
- Dates in Pandas.
- Interacting with the operating system from Python.
- Summarizing by creating group means, etc.
Week 6
The previous sections have used data available online through APIs and flat files. Commonly the financial analyst will have to query relational (SQL) databases. In this section we'll cover the basics of connecting to a SQL database in Python, and executing SQL queries, and returning the data as a Pandas DataFrame.
Module 2: Numpy/Scipy and APIs
This module focuses on using scientific computing libraries such as Numpy Scipy to complete investments-focused tasks. We also retrieve JSON data from APIs.
Week 7
- An introduction to Application Programming Interfaces (APIs) and online
data. Above notebook uses the yfinance package. Additional APIs we can use.
- Example: Coinbase API
- Example: Interactive Brokers API
- EIA API.
- Connecting to databases.
Exercise: Pull stock data via API and estimate a Markowitz mean-variance optimal portfolio.
Alternative: Calculate futures prices given options (but we cant get this from API data).
Week 8
Old video and notebook. Coinbase API Packages are now broken.
- An overview of scipy and basic statistics in Python.
- Extracting coefficients from an estimated regression model.
Exercise: Calculate market depth via Kyle's lambda. Use Coinbase API to get trades.
Week 9
Exercise: Deposit insurance valuation using (Merton's) option pricing method. (chapter 19, Merton's CTF)
Module 3: Sharing Your Work
This module focuses on showcasing your work via visualizations, sharing code via git and GitHub. We also cover reproducible environments.
Week 10
- Visualization:
- Matplotlib
- Seaborn
- Plotly
- ggplot (using plotnine library)
- Interactive Web Apps via Plotly's Dash
Week 11
- The Integrated Development Environment (IDE)
- What does an IDE provide?
- Code Completion
- Linter
- Git/Github and IDE Integration
- What does an IDE provide?
- Virtual Environments
- Ensuring compatible python and package versions.
- Anaconda Python
Week 12
- Git/Github and IDE Integration
- Why use version control?
- Collaborate with others more easily.
- Show others your work.
- Why use version control?