Book Image

Mastering Python for Finance

Book Image

Mastering Python for Finance

Overview of this book

Table of Contents (17 chapters)
Mastering Python for Finance
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Python is widely practiced in various sectors of finance, such as banking, investment management, insurance, and even real estate, for building tools that help in financial modeling, risk management, and trading. Even big financial corporations embrace Python to build their infrastructure for position management, pricing, risk management, and trading systems.

Throughout this book, theories from academic financial studies will be introduced, accompanied by their mathematical concepts to help you understand their uses in practical situations. You will see how Python is applied to classical pricing models, linearity, and nonlinearity of finance, numerical procedures, and interest rate models, that form the foundations of complex financial models. You will learn about the root-finding methods and finite difference pricing for developing an implied volatility curve with options.

With the advent of advanced computing technologies, methods for the storing and handling of massive amounts of data have to be considered. Hadoop is a popular tool in big data. You will be introduced to the inner workings of Hadoop and its integration with Python to derive analytical insights on financial data. You will also understand how Python supports the use of NoSQL for storing non-structured data.

Many brokerage firms are beginning to offer APIs to customers to trade using their own customized trading software. Using Python, you will learn how to connect to a broker API, retrieve market data, generate trading signals, and send orders to the exchange. The implementation of the mean-reverting and trend-following trading strategies will be covered. Risk management, position tracking, and backtesting techniques will be discussed to help you manage the performance of your trading strategies.

The use of Microsoft Excel is pervasive in the financial industry, from bond trading to back-office operations. You will be taught how to create numerical pricing Component Object Model (COM) servers in Python that will enable your spreadsheets to compute and update model values on the fly.

What this book covers

Chapter 1, Python for Financial Applications, explores the aspects of Python in judging its suitability as a programming language in finance. The IPython Notebook is introduced as a beneficial tool to visualize data and to perform scientific computing.

Chapter 2, The Importance of Linearity in Finance, uses Python to solve systems of linear equations, perform integer programming, and apply matrix algebra to linear optimization of portfolio allocation.

Chapter 3, Nonlinearity in Finance, discusses the nonlinear models in finance and root-finding methods using Python.

Chapter 4, Numerical Procedures, explores trees, lattices, and finite differencing schemes for valuation of options.

Chapter 5, Interest Rates and Derivatives, discusses the bootstrapping process of the yield curve and covers some short rate models for pricing the interest rate derivatives with Python.

Chapter 6, Interactive Financial Analytics with Python and VSTOXX, discusses the volatility indexes. We will perform analytics on EURO STOXX 50 Index and VSTOXX data, and replicate the main index using options prices of the sub-indexes.

Chapter 7, Big Data with Python, walks you through the uses of Hadoop for big data and covers how to use Python to perform MapReduce operations. Data storage with NoSQL will also be covered.

Chapter 8, Algorithmic Trading, discusses a step-by-step approach to develop a mean-reverting and trend-following live trading infrastructure using Python and the API of a broker. Value-at-risk (VaR) for risk management will also be covered.

Chapter 9, Backtesting, discusses how to design and implement an event-driven backtesting system and helps you visualize the performance of our simulated trading strategy.

Chapter 10, Excel with Python, discusses how to build a Component Object Model (COM) server and client interface to communicate with Excel and to perform numerical pricing on the call and put options on the fly.

What you need for this book

In this book, the following software will be required:

Who this book is for

This book is geared toward students and programmers developing financial applications, consultants offering financial services, financial analysts, and quants who would like to master finance by harnessing Python's strengths in data visualization, interactive analytics, and scientific computing. An intermediate level of Python knowledge and financial concepts is expected. Beginners will receive an introductory background before jumping into the technical process of each chapter.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "The price function of the BinomialEuropeanOption class is a public method that is the entry point for all the instances of this class."

A block of code is set as follows:

    def _traverse_tree_(self, payoffs):
        # Starting from the time the option expires, traverse
        # backwards and calculate discounted payoffs at each node
        for i in range(self.N):
            payoffs = (payoffs[:-1] * self.qu +
                       payoffs[1:] * self.qd) * self.df

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

    Set BinCRRTree = CreateObject("BinomialCRRCOMServer.Pricer")
    answer = BinCRRTree.pricer(S0, K, r, T, N, sigma, isCall, _
        dividend, True)

Any command-line input or output is written as follows:

>>> from FDCnEu import FDCnEu
>>> option = FDCnEu(50, 50, 0.1, 5./12., 0.4, 100, 100,
...                 100, False)
>>> print option.price()

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "We can compile the code by selecting Debug from the toolbar menu and clicking on Compile VBAProject:"

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.

To send us general feedback, simply e-mail , and mention the book's title in the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at , and we will do our best to address the problem.