O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Python for Finance, 2nd Edition

Book Description

The financial industry has recently adopted Python at a tremendous rate, with some of the largest investment banks and hedge funds using it to build core trading and risk management systems. Updated for Python 3, the second edition of this hands-on book helps you get started with the language, guiding developers and quantitative analysts through Python libraries and tools for building financial applications and interactive financial analytics.

Using practical examples throughout the book, author Yves Hilpisch also shows you how to develop a full-fledged framework for Monte Carlo simulation-based derivatives and risk analytics, based on a large, realistic case study. Much of the book uses interactive IPython Notebooks.

Table of Contents

  1. I. Python and Finance
  2. 1. Why Python for Finance
    1. Python Programming Language
      1. Brief History of Python
      2. The Python Ecosystem
      3. Python User Spectrum
      4. The Scientific Stack
    2. Technology in Finance
      1. Technology Spending
      2. Technology as Enabler
      3. Technology and Talent as Barriers to Entry
      4. Ever-Increasing Speeds, Frequencies, Data Volumes
      5. The Rise of Real-Time Analytics
    3. Python for Finance
      1. Finance and Python Syntax
      2. Efficiency and Productivity Through Python
      3. From Prototyping to Production
    4. Data-Driven and AI-First Finance
      1. Data-Driven Finance
      2. AI-First Finance
    5. Conclusions
    6. Further Resources
  3. 2. Python Infrastructure
    1. Introduction
    2. Conda as a Package Manager
      1. Installing Miniconda
      2. Basic Operations with Conda
    3. Conda as a Virtual Environment Manager
    4. Using Docker Containers
      1. Docker Images and Containers
      2. Building a Ubuntu & Python Docker Image
    5. Using Cloud Instances
      1. RSA Public and Private Keys
      2. Jupyter Notebook Configuration File
      3. Installation Script for Python and Jupyter Notebook
      4. Script to Orchestrate the Droplet Set-up
    6. Conclusions
    7. Further Resources
  4. II. Mastering the Basics
  5. 3. Data Types and Structures
    1. Introduction
    2. Basic Data Types
      1. Integer
      2. Float
      3. Boolean
      4. String
      5. Excursion: Printing and String Replacements
      6. Excursion: Regular Expressions
    3. Basic Data Structures
      1. Tuple
      2. List
      3. Excursion: Control Structures
      4. Excursion: Functional Programming
      5. Dict
      6. Sets
    4. Conclusions
    5. Further Resources
  6. 4. Numerical Computing with NumPy
    1. Introduction
    2. Arrays of Data
      1. Arrays with Python Lists
      2. The Python Array Class
    3. Regular NumPy Arrays
      1. The Basics
      2. Multiple Dimensions
      3. Meta-Information
      4. Reshaping and Resizing
      5. Boolean Arrays
      6. Speed Comparison
    4. Structured NumPy Arrays
    5. Vectorization of Code
      1. Basic Vectorization
      2. Memory Layout
    6. Conclusions
    7. Further Resources
  7. 5. Data Analysis with pandas
    1. Introduction
    2. DataFrame Class
      1. First Steps with DataFrame Class
      2. Second Steps with DataFrame Class
    3. Basic Analytics
    4. Basic Visualization
    5. Series Class
    6. GroupBy Operations
    7. Complex Selection
    8. Concatenation, Joining and Merging
      1. Concatenation
      2. Joining
      3. Merging
    9. Performance Aspects
    10. Conclusions
    11. Further Reading
  8. 6. Object Orientated Programming
    1. Introduction
    2. A Look at Python Objects
      1. int
      2. list
      3. ndarray
      4. DataFrame
    3. Basics of Python Classes
    4. Python Data Model
    5. Conclusions
    6. Further Resources
    7. Python Codes
      1. Vector Class
  9. III. Financial Data Science
  10. 7. Data Visualization
    1. Introduction
    2. Static 2D Plotting
      1. One-Dimensional Data Set
      2. Two-Dimensional Data Set
      3. Other Plot Styles
    3. Static 3D Plotting
    4. Interactive 2D Plotting
      1. Basic Plots
      2. Financial Plots
    5. Conclusions
    6. Further Resources
  11. 8. Financial Time Series
    1. Introduction
    2. Financial Data
      1. Data Import
      2. Summary Statistics
      3. Changes over Time
      4. Resampling
    3. Rolling Statistics
      1. An Overview
      2. A Technical Analysis Example
    4. Correlation Analysis
      1. The Data
      2. Logarithmic Returns
      3. OLS Regression
      4. Correlation
    5. High Frequency Data
    6. Conclusions
    7. Further Resources
  12. 9. Input/Output Operations
    1. Introduction
    2. Basic I/O with Python
      1. Writing Objects to Disk
      2. Reading and Writing Text Files
      3. SQL Database
      4. Writing and Reading NumPy Arrays
    3. I/O with pandas
      1. SQL Database
      2. From SQL to pandas
      3. Data as CSV File
      4. Data as Excel File
    4. I/O with PyTables
      1. Working with Tables
      2. Working with Compressed Tables
      3. Working with Arrays
      4. Out-of-Memory Computations
    5. I/O with TsTables
      1. Sample Data
      2. Data Storage
      3. Data Retrieval
    6. Conclusions
    7. Further Resources
  13. 10. Performance Python
    1. Introduction
    2. Loops
      1. Python
      2. NumPy
      3. Numba
      4. Cython
    3. Algorithms
      1. Prime Numbers
      2. Fibonacci Numbers
      3. The Number Pi
    4. Binomial Trees
      1. Python
      2. NumPy
      3. Numba
      4. Cython
    5. Monte Carlo Simulation
      1. Python
      2. NumPy
      3. Numba
      4. Cython
      5. Multiprocessing
    6. Recursive pandas Algorithm
      1. Python
      2. Numba
      3. Cython
    7. Conclusions
    8. Further Resources
  14. 11. Mathematical Tools
    1. Introduction
    2. Approximation
      1. Regression
      2. Interpolation
    3. Convex Optimization
      1. Global Optimization
      2. Local Optimization
      3. Constrained Optimization
    4. Integration
      1. Numerical Integration
      2. Integration by Simulation
    5. Symbolic Computation
      1. Basics
      2. Equations
      3. Integration and Differentiation
      4. Differentiation
    6. Conclusions
    7. Further Resources
  15. 12. Stochastics
    1. Introduction
    2. Random Numbers
    3. Simulation
      1. Random Variables
      2. Stochastic Processes
      3. Variance Reduction
    4. Valuation
      1. European Options
      2. American Options
    5. Risk Measures
      1. Value-at-Risk
      2. Credit Value Adjustments
    6. Conclusions
    7. Further Resources
    8. Python Script
  16. 13. Statistics
    1. Introduction
    2. Normality Tests
      1. Benchmark Case
      2. Real-World Data
    3. Portfolio Optimization
      1. The Data
      2. The Basic Theory
      3. Portfolio Optimization
      4. Efficient Frontier
      5. Capital Market Line
    4. Bayesian Statistics
      1. Bayes’ Formula
      2. Bayesian Regression
      3. Two Financial Instruments
      4. Updating Estimates over Time
    5. Machine Learning
      1. Unsupervised Learning
      2. Supervised Learning
      3. Deep Neural Network
      4. Feature Transforms
      5. Train-Test Splits
    6. Conclusions
    7. Further Resources
  17. IV. Algorithmic Trading
  18. 14. Trading Platform
    1. Introduction
    2. Getting Started
    3. Retrieving Data
      1. Retrieving Tick Data
      2. Retrieving Candles Data
    4. Working with the API
      1. Retrieving Historical Data
      2. Retrieving Streaming Data
      3. Placing Orders
      4. Account Information
    5. Conclusions
    6. Further Resources
  19. 15. Trading Strategies
    1. Introduction
    2. Simple Moving Averages
      1. Data Import
      2. Trading Strategy
      3. Vectorized Backtesting
      4. Optimization
    3. Random Walk Hypothesis
    4. Linear OLS Regression
      1. The Data
      2. Regression
    5. Clustering
    6. Frequency Approach
    7. Classification
      1. Two Binary Features
      2. Five Binary Features
      3. Five Digitized Features
      4. Sequential Train-Test Split
      5. Randomized Train-Test Split
    8. Deep Neural Network
      1. DNN with scikit-learn
      2. DNN with TensorFlow
    9. Conclusions
    10. Further Resources
  20. 16. Automated Trading
    1. Introduction
    2. Capital Management
      1. Kelly Criterion in Binomial Setting
      2. Kelly Criterion for Stocks and Indices
    3. ML-Based Trading Strategy
      1. Vectorized Backtesting
      2. Optimal Leverage
      3. Risk Analysis
      4. Persisting the Model Object
    4. Online Algorithm
    5. Infrastructure and Deployment
    6. Logging and Monitoring
    7. Conclusions
    8. Further Resources
    9. Python Script
      1. Automated Trading Strategy
      2. Strategy Monitoring
  21. V. Derivatives Analytics
  22. 17. Valuation Framework
    1. Introduction
    2. Fundamental Theorem of Asset Pricing
      1. A Simple Example
      2. The General Results
    3. Risk-Neutral Discounting
      1. Modeling and Handling Dates
      2. Constant Short Rate
    4. Market Environments
    5. Conclusions
    6. Further Resources
  23. 18. Simulation of Financial Models
    1. Introduction
    2. Random Number Generation
    3. Generic Simulation Class
    4. Geometric Brownian Motion
      1. The Simulation Class
      2. A Use Case
    5. Jump Diffusion
      1. The Simulation Class
      2. A Use Case
    6. Square-Root Diffusion
      1. The Simulation Class
      2. A Use Case
    7. Conclusions
    8. Further Resources
  24. 19. Derivatives Valuation
    1. Introduction
    2. Generic Valuation Class
    3. European Exercise
      1. The Valuation Class
      2. A Use Case
    4. American Exercise
      1. Least-Squares Monte Carlo
      2. The Valuation Class
      3. A Use Case
    5. Conclusions
    6. Further Resources
  25. 20. Portfolio Valuation
    1. Introduction
    2. Derivatives Positions
      1. The Class
      2. A Use Case
    3. Derivatives Portfolios
      1. The Class
      2. A Use Case
    4. Conclusions
    5. Further Resources
  26. 21. Market-Based Valuation
    1. Introduction
    2. Options Data
    3. Model Calibration
      1. Relevant Market Data
      2. Option Modeling
      3. Calibration Procedure
    4. Portfolio Valuation
      1. Modeling Option Positions
      2. The Options Portfolio
    5. Conclusions
    6. Further Resources
    7. Python Code
  27. A. Dates and Times
    1. Python
    2. NumPy
    3. pandas
  28. B. BSM Option Class
    1. Class Definition
    2. Class Usage
  29. Index