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. Preface
    1. Conventions Used in This Book
    2. Using Code Examples
    3. O’Reilly Safari
    4. How to Contact Us
    5. Acknowledgments
  2. I. Python and Finance
  3. 1. Why Python for Finance
    1. The Python Programming Language
      1. A Brief History of Python
      2. The Python Ecosystem
      3. The 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, and 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. Conclusion
    6. Further Resources
  4. 2. Python Infrastructure
    1. conda as a Package Manager
      1. Installing Miniconda
      2. Basic Operations with conda
    2. conda as a Virtual Environment Manager
    3. Using Docker Containers
      1. Docker Images and Containers
      2. Building an Ubuntu and Python Docker Image
    4. 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 Setup
    5. Conclusion
    6. Further Resources
  5. II. Mastering the Basics
  6. 3. Data Types and Structures
    1. Basic Data Types
      1. Integers
      2. Floats
      3. Booleans
      4. Strings
      5. Excursion: Printing and String Replacements
      6. Excursion: Regular Expressions
    2. Basic Data Structures
      1. Tuples
      2. Lists
      3. Excursion: Control Structures
      4. Excursion: Functional Programming
      5. Dicts
      6. Sets
    3. Conclusion
    4. Further Resources
  7. 4. Numerical Computing with NumPy
    1. Arrays of Data
      1. Arrays with Python Lists
      2. The Python array Class
    2. Regular NumPy Arrays
      1. The Basics
      2. Multiple Dimensions
      3. Metainformation
      4. Reshaping and Resizing
      5. Boolean Arrays
      6. Speed Comparison
    3. Structured NumPy Arrays
    4. Vectorization of Code
      1. Basic Vectorization
      2. Memory Layout
    5. Conclusion
    6. Further Resources
  8. 5. Data Analysis with pandas
    1. The DataFrame Class
      1. First Steps with the DataFrame Class
      2. Second Steps with the DataFrame Class
    2. Basic Analytics
    3. Basic Visualization
    4. The Series Class
    5. GroupBy Operations
    6. Complex Selection
    7. Concatenation, Joining, and Merging
      1. Concatenation
      2. Joining
      3. Merging
    8. Performance Aspects
    9. Conclusion
    10. Further Reading
  9. 6. Object-Oriented Programming
    1. A Look at Python Objects
      1. int
      2. list
      3. ndarray
      4. DataFrame
    2. Basics of Python Classes
    3. Python Data Model
    4. The Vector Class
    5. Conclusion
    6. Further Resources
  10. III. Financial Data Science
  11. 7. Data Visualization
    1. Static 2D Plotting
      1. One-Dimensional Data Sets
      2. Two-Dimensional Data Sets
      3. Other Plot Styles
    2. Static 3D Plotting
    3. Interactive 2D Plotting
      1. Basic Plots
      2. Financial Plots
    4. Conclusion
    5. Further Resources
  12. 8. Financial Time Series
    1. Financial Data
      1. Data Import
      2. Summary Statistics
      3. Changes over Time
      4. Resampling
    2. Rolling Statistics
      1. An Overview
      2. A Technical Analysis Example
    3. Correlation Analysis
      1. The Data
      2. Logarithmic Returns
      3. OLS Regression
      4. Correlation
    4. High-Frequency Data
    5. Conclusion
    6. Further Resources
  13. 9. Input/Output Operations
    1. Basic I/O with Python
      1. Writing Objects to Disk
      2. Reading and Writing Text Files
      3. Working with SQL Databases
      4. Writing and Reading NumPy Arrays
    2. I/O with pandas
      1. Working with SQL Databases
      2. From SQL to pandas
      3. Working with CSV Files
      4. Working with Excel Files
    3. I/O with PyTables
      1. Working with Tables
      2. Working with Compressed Tables
      3. Working with Arrays
      4. Out-of-Memory Computations
    4. I/O with TsTables
      1. Sample Data
      2. Data Storage
      3. Data Retrieval
    5. Conclusion
    6. Further Resources
  14. 10. Performance Python
    1. Loops
      1. Python
      2. NumPy
      3. Numba
      4. Cython
    2. Algorithms
      1. Prime Numbers
      2. Fibonacci Numbers
      3. The Number Pi
    3. Binomial Trees
      1. Python
      2. NumPy
      3. Numba
      4. Cython
    4. Monte Carlo Simulation
      1. Python
      2. NumPy
      3. Numba
      4. Cython
      5. Multiprocessing
    5. Recursive pandas Algorithm
      1. Python
      2. Numba
      3. Cython
    6. Conclusion
    7. Further Resources
  15. 11. Mathematical Tools
    1. Approximation
      1. Regression
      2. Interpolation
    2. Convex Optimization
      1. Global Optimization
      2. Local Optimization
      3. Constrained Optimization
    3. Integration
      1. Numerical Integration
      2. Integration by Simulation
    4. Symbolic Computation
      1. Basics
      2. Equations
      3. Integration and Differentiation
      4. Differentiation
    5. Conclusion
    6. Further Resources
  16. 12. Stochastics
    1. Random Numbers
    2. Simulation
      1. Random Variables
      2. Stochastic Processes
      3. Variance Reduction
    3. Valuation
      1. European Options
      2. American Options
    4. Risk Measures
      1. Value-at-Risk
      2. Credit Valuation Adjustments
    5. Python Script
    6. Conclusion
    7. Further Resources
  17. 13. Statistics
    1. Normality Tests
      1. Benchmark Case
      2. Real-World Data
    2. Portfolio Optimization
      1. The Data
      2. The Basic Theory
      3. Optimal Portfolios
      4. Efficient Frontier
      5. Capital Market Line
    3. Bayesian Statistics
      1. Bayes’ Formula
      2. Bayesian Regression
      3. Two Financial Instruments
      4. Updating Estimates over Time
    4. Machine Learning
      1. Unsupervised Learning
      2. Supervised Learning
    5. Conclusion
    6. Further Resources
  18. IV. Algorithmic Trading
  19. 14. The FXCM Trading Platform
    1. Getting Started
    2. Retrieving Data
      1. Retrieving Tick Data
      2. Retrieving Candles Data
    3. Working with the API
      1. Retrieving Historical Data
      2. Retrieving Streaming Data
      3. Placing Orders
      4. Account Information
    4. Conclusion
    5. Further Resources
  20. 15. Trading Strategies
    1. Simple Moving Averages
      1. Data Import
      2. Trading Strategy
      3. Vectorized Backtesting
      4. Optimization
    2. Random Walk Hypothesis
    3. Linear OLS Regression
      1. The Data
      2. Regression
    4. Clustering
    5. Frequency Approach
    6. Classification
      1. Two Binary Features
      2. Five Binary Features
      3. Five Digitized Features
      4. Sequential Train-Test Split
      5. Randomized Train-Test Split
    7. Deep Neural Networks
      1. DNNs with scikit-learn
      2. DNNs with TensorFlow
    8. Conclusion
    9. Further Resources
  21. 16. Automated Trading
    1. Capital Management
      1. The Kelly Criterion in a Binomial Setting
      2. The Kelly Criterion for Stocks and Indices
    2. ML-Based Trading Strategy
      1. Vectorized Backtesting
      2. Optimal Leverage
      3. Risk Analysis
      4. Persisting the Model Object
    3. Online Algorithm
    4. Infrastructure and Deployment
    5. Logging and Monitoring
    6. Conclusion
    7. Python Scripts
      1. Automated Trading Strategy
      2. Strategy Monitoring
    8. Further Resources
  22. V. Derivatives Analytics
  23. 17. Valuation Framework
    1. Fundamental Theorem of Asset Pricing
      1. A Simple Example
      2. The General Results
    2. Risk-Neutral Discounting
      1. Modeling and Handling Dates
      2. Constant Short Rate
    3. Market Environments
    4. Conclusion
    5. Further Resources
  24. 18. Simulation of Financial Models
    1. Random Number Generation
    2. Generic Simulation Class
    3. Geometric Brownian Motion
      1. The Simulation Class
      2. A Use Case
    4. Jump Diffusion
      1. The Simulation Class
      2. A Use Case
    5. Square-Root Diffusion
      1. The Simulation Class
      2. A Use Case
    6. Conclusion
    7. Further Resources
  25. 19. Derivatives Valuation
    1. Generic Valuation Class
    2. European Exercise
      1. The Valuation Class
      2. A Use Case
    3. American Exercise
      1. Least-Squares Monte Carlo
      2. The Valuation Class
      3. A Use Case
    4. Conclusion
    5. Further Resources
  26. 20. Portfolio Valuation
    1. Derivatives Positions
      1. The Class
      2. A Use Case
    2. Derivatives Portfolios
      1. The Class
      2. A Use Case
    3. Conclusion
    4. Further Resources
  27. 21. Market-Based Valuation
    1. Options Data
    2. Model Calibration
      1. Relevant Market Data
      2. Option Modeling
      3. Calibration Procedure
    3. Portfolio Valuation
      1. Modeling Option Positions
      2. The Options Portfolio
    4. Python Code
    5. Conclusion
    6. Further Resources
  28. A. Dates and Times
    1. Python
    2. NumPy
    3. pandas
  29. B. BSM Option Class
    1. Class Definition
    2. Class Usage
  30. Index