Free Sample
+ Collection

Python for Finance

Yuxing Yan

If your interest is finance and trading, then using Python to build a financial calculator makes absolute sense. As does this book which is a hands-on guide covering everything from option theory to time series.
RRP $26.99
RRP $44.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781783284375
Paperback408 pages

About This Book

  • Estimate market risk, form various portfolios, and estimate their variance-covariance matrixes using real-world data
  • Explains many financial concepts and trading strategies with the help of graphs
  • A step-by-step tutorial with many Python programs that will help you learn how to apply Python to finance

Who This Book Is For

Python for Finance is perfect for graduate students, practitioners, and application developers who wish to learn how to utilize Python to handle their financial needs. Basic programming knowledge is helpful, but not necessary.

Table of Contents

Chapter 1: Introduction and Installation of Python
Introduction to Python
Installing Python
Different versions of Python
Ways to launch Python
Quitting Python
Error messages
Python language is case sensitive
Initializing the variable
Finding the help window
Finding manuals and tutorials
Finding the version of Python
Chapter 2: Using Python as an Ordinary Calculator
Assigning values to variables
Error messages
Choosing meaningful names
Using dir() to find variables and functions
Basic math operations – addition, subtraction, multiplication, and division
The power function, floor, and remainder
Choosing appropriate precision
Finding out more information about a specific built-in function
Importing the math module
A few frequently used functions
The tuple data type
Chapter 3: Using Python as a Financial Calculator
Writing a Python function without saving it
Default input values for a function
Indentation is critical in Python
Checking the existence of our functions
Defining functions from our Python editor
Activating our function using the import function
Debugging a program from a Python editor
Two ways to call our pv_f() function
Generating our own module
Types of comments
The if() function
Annuity estimation
Converting the interest rates
Continuously compounded interest rate
A data type – list
Net present value and the NPV rule
Defining the payback period and the payback period rule
Defining IRR and the IRR rule
Showing certain files in a specific subdirectory
Using Python as a financial calculator
Adding our project directory to the path
Chapter 4: 13 Lines of Python to Price a Call Option
Writing a program – the empty shell method
Writing a program – the comment-all-out method
Using and debugging other programs
Chapter 5: Introduction to Modules
What is a module?
Importing a module
Module dependency
Chapter 6: Introduction to NumPy and SciPy
Installation of NumPy and SciPy
Launching Python from Anaconda
Showing all functions in NumPy and SciPy
More information about a specific function
Understanding the list data type
Working with arrays of ones, zeros, and the identity matrix
Performing array manipulations
Performing array operations with +, -, *, /
The x.sum() dot function
Looping through an array
Using the help function related to modules
A list of subpackages for SciPy
Cumulative standard normal distribution
Logic relationships related to an array
Statistic submodule (stats) from SciPy
Interpolation in SciPy
Solving linear equations using SciPy
Generating random numbers with a seed
Finding a function from an imported module
Understanding optimization
Linear regression and Capital Assets Pricing Model (CAPM)
Retrieving data from an external text file
Installing NumPy independently
Understanding the data types
Chapter 7: Visual Finance via Matplotlib
Installing matplotlib via ActivePython
Alternative installation via Anaconda
Understanding how to use matplotlib
Understanding simple and compounded interest rates
Adding texts to our graph
Working with DuPont identity
Understanding the Net Present Value (NPV) profile
Graphical representation of the portfolio diversification effect
Retrieving historical price data from Yahoo! Finance
Understanding the time value of money
Candlesticks representation of IBM's daily price
IBM's intra-day graphical representations
Presenting both closing price and trading volume
Performance comparisons among stocks
Comparing return versus volatility for several stocks
Finding manuals, examples, and videos
Installing the matplotlib module independently
Chapter 8: Statistical Analysis of Time Series
Installing Pandas and statsmodels
Using Pandas and statsmodels
Open data sources
Retrieving data to our programs
Several important functionalities
Return estimation
Merging datasets by date
T-test and F-test
Many useful applications
Constructing an efficient frontier
Understanding the interpolation technique
Outputting data to external files
Python for high-frequency data
More on using Spyder
A useful dataset
Chapter 9: The Black-Scholes-Merton Option Model
Payoff and profit/loss functions for the call and put options
European versus American options
Cash flows, types of options, a right, and an obligation
Normal distribution, standard normal distribution, and cumulative standard normal distribution
The Black-Scholes-Merton option model on non-dividend paying stocks
The p4f module for options
European options with known dividends
Various trading strategies
Relationship between input values and option values
Greek letters for options
The put-call parity and its graphical representation
Binomial tree (the CRR method) and its graphical representation
Hedging strategies
Chapter 10: Python Loops and Implied Volatility
Definition of an implied volatility
Understanding a for loop
Estimation of IRR via a for loop
Understanding a while loop
Using keyboard commands to stop an infinitive loop
Estimating implied volatility by using an American call
Measuring efficiency by time spent in finishing a program
The mechanism of a binary search
Sequential versus random access
Looping through an array/DataFrame
Retrieving option data from CBOE
Retrieving option data from Yahoo! Finance
The put-call ratio
Chapter 11: Monte Carlo Simulation and Options
Generating random numbers from a standard normal distribution
Generating random numbers from a uniform distribution
Using simulation to estimate the pi value
Generating random numbers from a Poisson distribution
Bootstrapping with/without replacements
Distribution of annual returns
Simulation of stock price movements
Finding an efficient portfolio and frontier
Geometric versus arithmetic mean
Long-term return forecasting
Pricing a call using simulation
Exotic options
Barrier in-and-out parity
Pricing lookback options with floating strikes
Using the Sobol sequence to improve the efficiency
Chapter 12: Volatility Measures and GARCH
Conventional volatility measure – standard deviation
Tests of normality
Lower partial standard deviation
Test of equivalency of volatility over two periods
Test of heteroskedasticity, Breusch, and Pagan (1979)
Retrieving option data from Yahoo! Finance
Volatility smile and skewness
The ARCH model
The GARCH (Generalized ARCH) model

What You Will Learn

  • Build a financial calculator based on Python
  • Learn how to price various types of options such as European, American, average, lookback, and barrier options
  • Write Python programs to download data from Yahoo! Finance
  • Estimate returns and convert daily returns into monthly or annual returns
  • Form an n-stock portfolio and estimate its variance-covariance matrix
  • Estimate VaR (Value at Risk) for a stock or portfolio
  • Run CAPM (Capital Asset Pricing Model) and the Fama-French 3-factor model
  • Learn how to optimize a portfolio and draw an efficient frontier
  • Conduct various statistic tests such as T-tests, F-tests, and normality tests

In Detail

Python is a free and powerful tool that can be used to build a financial calculator and price options, and can also explain many trading strategies and test various hypotheses. This book details the steps needed to retrieve time series data from different public data sources.

Python for Finance explores the basics of programming in Python. It is a step-by-step tutorial that will teach you, with the help of concise, practical programs, how to run various statistic tests. This book introduces you to the basic concepts and operations related to Python. You will also learn how to estimate illiquidity, Amihud (2002), liquidity measure, Pastor and Stambaugh (2003), Roll spread (1984), spread based on high-frequency data, beta (rolling beta), draw volatility smile and skewness, and construct a binomial tree to price American options.

This book is a hands-on guide with easy-to-follow examples to help you learn about option theory, quantitative finance, financial modeling, and time series using Python.


Read More

Recommended for You

Learning Python Data Visualization
$ 28.99