Mastering Python for Finance

Book description

Understand, design, and implement state-of-the-art mathematical and statistical applications used in finance with Python

In Detail

Built initially for scientific computing, Python quickly found its place in finance. Its flexibility and robustness can be easily incorporated into applications for mathematical studies, research, and software development.

With this book, you will learn about all the tools you need to successfully perform research studies and modeling, improve your trading strategies, and effectively manage risks. You will explore the various tools and techniques used in solving complex problems commonly faced in finance.

You will learn how to price financial instruments such as stocks, options, interest rate derivatives, and futures using computational methods. Also, you will learn how you can perform data analytics on market indexes and use NoSQL to store tick data.

What You Will Learn

  • Perform interactive computing with IPython Notebook
  • Solve linear equations of financial models and perform ordinary least squares regression
  • Explore nonlinear modeling and solutions for optimum points using root-finding algorithms and solvers
  • Discover different types of numerical procedures used in pricing options
  • Model fixed-income instruments with bonds and interest rates
  • Manage big data with NoSQL and perform analytics with Hadoop
  • Build a high-frequency algorithmic trading platform with Python
  • Create an event-driven backtesting tool and measure your strategies

Table of contents

  1. Mastering Python for Finance
    1. Table of Contents
    2. Mastering Python for Finance
    3. Credits
    4. About the Author
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Python for Financial Applications
      1. Is Python for me?
        1. Free and open source
        2. High-level, powerful, and flexible
        3. A wealth of standard libraries
      2. Objected-oriented versus functional programming
        1. The object-oriented approach
        2. The functional approach
        3. Which approach should I use?
      3. Which Python version should I use?
      4. Introducing IPython
        1. Getting IPython
        2. Using pip
        3. The IPython Notebook
          1. Notebook documents
          2. Running the IPython Notebook
          3. Creating a new notebook
        4. Notebook cells
          1. Code cell
          2. Markdown cell
          3. Raw NBConvert cell
          4. Heading cells
        5. Simple exercises with IPython Notebook
          1. Creating a notebook with heading and Markdown cells
          2. Saving notebooks
          3. Mathematical operations in cells
          4. Displaying graphs
          5. Inserting equations
          6. Displaying images
          7. Inserting YouTube videos
          8. Working with HTML
          9. The pandas DataFrame object as an HTML table
        6. Notebook for finance
      5. Summary
    9. 2. The Importance of Linearity in Finance
      1. The capital asset pricing model and the security market line
      2. The Arbitrage Pricing Theory model
      3. Multivariate linear regression of factor models
      4. Linear optimization
        1. Getting PuLP
        2. A simple linear optimization problem
        3. Outcomes of linear programs
        4. Integer programming
          1. An example of an integer programming model with binary conditions
          2. A different approach with binary conditions
      5. Solving linear equations using matrices
      6. The LU decomposition
      7. The Cholesky decomposition
      8. The QR decomposition
        1. Solving with other matrix algebra methods
          1. The Jacobi method
          2. The Gauss-Seidel method
      9. Summary
    10. 3. Nonlinearity in Finance
      1. Nonlinearity modeling
      2. Examples of nonlinear models
        1. The implied volatility model
        2. The Markov regime-switching model
        3. The threshold autoregressive model
        4. Smooth transition models
      3. An introduction to root-finding
      4. Incremental search
      5. The bisection method
      6. Newton's method
      7. The secant method
      8. Combining root-finding methods
      9. SciPy implementations
        1. Root-finding scalar functions
        2. General nonlinear solvers
      10. Summary
    11. 4. Numerical Procedures
      1. Introduction to options
      2. Binomial trees in options pricing
        1. Pricing European options
          1. Are these formulas relevant to stocks? What about futures?
        2. Writing the StockOption class
        3. Writing the BinomialEuropeanOption class
        4. Pricing American options with the BinomialTreeOption class
        5. The Cox-Ross-Rubinstein model
          1. Writing the BinomialCRROption class
        6. Using a Leisen-Reimer tree
          1. Writing the BinomialLROption class
      3. The Greeks for free
        1. Writing the BinomialLRWithGreeks class
      4. Trinomial trees in options pricing
        1. Writing the TrinomialTreeOption class
      5. Lattices in options pricing
        1. Using a binomial lattice
        2. Writing the BinomialCRROption class
        3. Using the trinomial lattice
          1. Writing the TrinomialLattice class
      6. Finite differences in options pricing
        1. The explicit method
          1. Writing the FiniteDifferences class
          2. Writing the FDExplicitEu class
        2. The implicit method
          1. Writing the FDImplicitEu class
        3. The Crank-Nicolson method
          1. Writing the FDCnEu class
        4. Pricing exotic barrier options
          1. A down-and-out option
          2. Writing the FDCnDo class
        5. American options pricing with finite differences
          1. Writing the FDCnAm class
      7. Putting it all together – implied volatility modeling
        1. Implied volatilities of AAPL American put option
      8. Summary
    12. 5. Interest Rates and Derivatives
      1. Fixed-income securities
      2. Yield curves
      3. Valuing a zero-coupon bond
        1. Spot and zero rates
      4. Bootstrapping a yield curve
      5. Forward rates
      6. Calculating the yield to maturity
      7. Calculating the price of a bond
      8. Bond duration
      9. Bond convexity
      10. Short-rate modeling
        1. The Vasicek model
        2. The Cox-Ingersoll-Ross model
        3. The Rendleman and Bartter model
        4. The Brennan and Schwartz model
      11. Bond options
        1. Callable bonds
        2. Puttable bonds
        3. Convertible bonds
        4. Preferred stocks
      12. Pricing a callable bond option
        1. Pricing a zero-coupon bond by the Vasicek model
        2. Value of early-exercise
        3. Policy iteration by finite differences
        4. Other considerations in callable bond pricing
      13. Summary
    13. 6. Interactive Financial Analytics with Python and VSTOXX
      1. Volatility derivatives
        1. STOXX and the Eurex
        2. The EURO STOXX 50 Index
        3. The VSTOXX
        4. The VIX
      2. Gathering the EUROX STOXX 50 Index and VSTOXX data
      3. Merging the data
      4. Financial analytics of SX5E and V2TX
      5. Correlation between SX5E and V2TX
      6. Calculating the VSTOXX sub-indices
        1. Getting the OESX data
        2. Formulas to calculate the VSTOXX sub-index
        3. Implementation of the VSTOXX sub-index value
        4. Analyzing the results
      7. Calculating the VSTOXX main index
      8. Summary
    14. 7. Big Data with Python
      1. Introducing big data
      2. Hadoop for big data
        1. HDFS
        2. YARN
        3. MapReduce
      3. Is big data for me?
      4. Getting Apache Hadoop
        1. Getting a QuickStart VM from Cloudera
        2. Getting VirtualBox
        3. Running Cloudera VM on VirtualBox
      5. A word count program in Hadoop
        1. Downloading sample data
        2. The map program
        3. The reduce program
        4. Testing our scripts
        5. Running MapReduce on Hadoop
        6. Hue for browsing HDFS
      6. Going deeper – Hadoop for finance
        1. Obtaining IBM stock prices from Yahoo! Finance
        2. Modifying the map program
        3. Testing our map program with IBM stock prices
        4. Running MapReduce to count intraday price changes
        5. Performing analysis on our MapReduce results
      7. Introducing NoSQL
        1. Getting MongoDB
        2. Creating the data directory and running MongoDB
          1. Running MongoDB from Windows
          2. Running MongoDB from Mac OS X
        3. Getting PyMongo
        4. Running a test connection
        5. Getting a database
        6. Getting a collection
        7. Inserting a document
        8. Fetching a single document
        9. Deleting documents
        10. Batch-inserting documents
        11. Counting documents in the collection
        12. Finding documents
        13. Sorting documents
        14. Conclusion
      8. Summary
    15. 8. Algorithmic Trading
      1. Introduction to algorithmic trading
      2. List of trading platforms with public API
      3. Which is the best programming language to use?
      4. System functionalities
      5. Algorithmic trading with Interactive Brokers and IbPy
        1. Getting Interactive Brokers' Trader WorkStation
        2. Getting IbPy – the IB API wrapper
        3. A simple order routing mechanism
      6. Building a mean-reverting algorithmic trading system
        1. Setting up the main program
        2. Handling events
        3. Implementing the mean-reverting algorithm
        4. Tracking our positions
      7. Forex trading with OANDA API
        1. What is REST?
        2. Setting up an OANDA account
        3. Exploring the API
        4. Getting oandapy – the OANDA REST API wrapper
        5. Getting and parsing rates data
        6. Sending an order
      8. Building a trend-following forex trading platform
        1. Setting up the main program
        2. Handling events
        3. Implementing the trend-following algorithm
        4. Tracking our positions
      9. VaR for risk management
      10. Summary
    16. 9. Backtesting
      1. An introduction to backtesting
        1. Concerns in backtesting
        2. Concept of an event-driven backtesting system
      2. Designing and implementing a backtesting system
        1. The TickData class
        2. The MarketData class
        3. The MarketDataSource class
        4. The Order class
        5. The Position class
        6. The Strategy class
        7. The MeanRevertingStrategy class
        8. The Backtester class
        9. Running our backtesting system
        10. Improving your backtesting system
      3. Ten considerations for a backtesting model
        1. Resources restricting your model
        2. Criteria of evaluation of the model
        3. Estimating the quality of backtest parameters
        4. Be prepared to face model risk
        5. Performance of a backtest with in-sample data
        6. Addressing common pitfalls in backtesting
        7. Have a common sense idea of your model
        8. Understanding the context for the model
        9. Make sure you have the right data
        10. Data mine your results
      4. Discussion of algorithms in backtesting
        1. K-means clustering
        2. K-nearest neighbor machine learning algorithm
        3. Classification and regression tree analysis
        4. The 2k factorial design
        5. The genetic algorithm
      5. Summary
    17. 10. Excel with Python
      1. Overview of COM
      2. Excel for finance
      3. Building a COM server
        1. Prerequisites
        2. Getting the pythoncom module
        3. Building the Black-Scholes model COM server
        4. Registering and unregistering the COM server
        5. Building the Cox-Ross-Rubinstein binomial tree model COM server
        6. Building the trinomial lattice model COM server
      4. Building the COM client in Excel
        1. Setting up the VBA code
        2. Setting up the cells
      5. What else can I do with COM?
      6. Summary
    18. Index

Product information

  • Title: Mastering Python for Finance
  • Author(s): James Ma Weiming
  • Release date: April 2015
  • Publisher(s): Packt Publishing
  • ISBN: 9781784394516