34,79 €
F# is a functional programming language that allows you to write simple code for complex problems. Currently, it is most commonly used in the financial sector. Quantitative finance makes heavy use of mathematics to model various parts of finance in the real world. If you are interested in using F# for your day-to-day work or research in quantitative finance, this book is a must-have.This book will cover everything you need to know about using functional programming for quantitative finance. Using a functional programming language will enable you to concentrate more on the problem itself rather than implementation details. Tutorials and snippets are summarized into an automated trading system throughout the book.This book will introduce you to F#, using Visual Studio, and provide examples with functional programming and finance combined. The book also covers topics such as downloading, visualizing and calculating statistics from data.
F# is a first class programming language for the financial domain.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 293
Veröffentlichungsjahr: 2013
Copyright © 2013 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: December 2013
Production Reference: 1191213
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78216-462-3
www.packtpub.com
Cover Image by Aniket Sawant ( <[email protected]>)
Author
Johan Astborg
Reviewers
Yan Cui
Arthur Pham
Isaac Abraham
Acquisition Editors
Sam Birch
Aarthi Kumaraswamy
Kunal Parikh
Lead Technical Editor
Athira Laji
Copy Editors
Roshni Banerjee
Janbal Dharmaraj
Mradula Hegde
Gladson Monteiro
Deepa Nambiar
Karuna Narayanan
Shambhavi Pai
Alfida Paiva
Adithi Shetty
Shambhavi Pai
Technical Editors
Gauri Dasgupta
Shiny Poojary
Siddhi Rane
Sonali S. Vernekar
Project Coordinator
Mary Alex
Proofreader
Paul Hindle
Indexers
Hemangini Bari
Mariammal Chettiyar
Tejal Soni
Graphics
Ronak Dhruv
Production Coordinator
Melwyn D'sa
Cover Work
Melwyn D'sa
Johan Astborg is the developer and architect of various kinds of software systems and applications, financial software systems, trading systems, as well as mobile and web applications. He is interested in computer science, mathematics, and quantitative finance, with a special focus on functional programming. Johan is passionate about languages such as F#, Clojure, and Haskell, and operating systems such as Linux, Mac OS X, and Windows for his work. Most of Johan's quantitative background comes from Lund University, where he studied courses in computer science, mathematics, and physics. Currently Johan is studying pure mathematics at Lund University, Sweden, and is aiming for a PhD in the future, combining mathematics and functional programming. Professionally, Johan has worked as a part-time developer for Sony Ericsson and various smaller firms in Sweden. He also works as a part-time consultant focusing on web technologies and cloud solutions. You can easily contact him by sending an e-mail to <[email protected]> or visit his GitHub page at https://github.com/joastbg.
Yan Cui (@theburningmonk) is a lead server-side developer at the London-based, award winning gaming company GameSys. He focuses on building highly distributed and scalable server-side solutions for GameSys's social and mobile games. Yan is a regular speaker on topics such as F#, AOP, and NoSQL at local user groups and conferences in the UK and keeps an active blog at http://theburningmonk.com. He is also a co-author of the upcoming book, F# Deep Dives, Manning Publications.
Arthur Pham is working for for Thomson Reuters as a Lead Quantitative Engineer since 2006. He has spent many years designing and implementing derivatives pricing models and still loves learning new programming languages like F#, C++, Python, Flex/Actionscript, C#, Ruby, and JavaScript.
He currently lives in New York, USA, and can be contacted on Twitter @arthurpham.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
F# is a functional programming language that allows you to write simple code for complex problems. Currently, it is most commonly used in the financial sector. Quantitative finance makes heavy use of mathematics to model the real world. If you are interested in using F# for your day-to-day work or research in quantitative finance, this book is for you.
This book covers everything you need to know about using functional programming for quantitative finance. Using a functional programming language for quantitative finance will enable you to concentrate more on the model itself rather than the implementation details. Tutorials and snippets are summarized into a trading system throughout this book.
F#, together with .NET, provides a wide range of tools needed to produce high quality and efficient code, from prototyping to production. The example code snippets in this book can be extended into larger blocks of code, and reused and tested easily in a functional language. F# is considered one of the default functional languages of choice for financial and trading-related applications.
Chapter 1, Introducing F# Using Visual Studio, introduces you to F# and its roots in functional languages. You will learn how to use F# in Visual Studio and write your first application.
Chapter 2, Learning More About F#, teaches you more about F# as a language and illustrates the many sides of this paradigm language.
Chapter 3, Financial Mathematics and Numerical Analysis, introduces the toolset we'll need throughout the book to implement financial models and algorithms.
Chapter 4, Getting Started with Data Visualization, introduces some of the most common ways to use F# to visualize data and display information in a GUI.
Chapter 5, Learning Option Pricing, teaches you about options, the Black-Scholes formula and ways of exploring options using the tools at hand.
Chapter 6, Exploring Volatility, digs deeper into the world of Black-Scholes and teaches you about implied volatility.
Chapter 7, Getting Started with Order Types and Market Data, takes a rather pragmatic approach towards finance and implements a basic order management system.
Chapter 8, Setting Up the Trading System Project, builds the foundation for the project and shows how to connect to SQL Server and use LINQ for queries.
Chapter 9, Trading Volatility for Profit, studies various ways of monetizing through movements in volatility and the arbitrage opportunity defining the trading strategy for the project.
Chapter 10, Putting the Pieces Together, shows the final steps towards the complete trading system using a volatility arbitrage strategy and FIX 4.2.
Apart from an interest in F# and finance, you need a computer with Visual Studio 2012 installed. Visual Studio 2012 is the recommended IDE, supporting F# 3.0.
This book is for anyone interested in writing F# code in the financial domain, with a quantitative approach. The book is mainly intended to be a source of inspiration and uses a lot of working code examples to illustrate both the concepts of finance and F# as a functional language.
At the end of the book we develop a simple trading system for volatility arbitrage. Details about orders and the FIX protocol are explained, as well as the theory behind the strategy itself. This may work as a foundation for anyone interested in developing their own trading system based on options and volatility.
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via 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 on www.packtpub.com/authors.
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.
You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. 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.
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 would 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 erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright 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 <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
In this chapter, you will learn about the history of F# and its roots in other programming languages. We will also be introducing Visual Studio and the fundamental language constructs of F#. You will be comfortable using the interactive mode for prototyping the code step-by-step. You will get a better understanding of how to build programs in F# by putting pieces together. Also, the basics of the language are covered by using and evaluating the code in the Read Eval Print Loop (REPL).
In this chapter you will learn:
Before we dive in to the language itself, we should discuss why we need it in the first place. F# is a powerful language, which may sound like a cliché, but it combines multiple paradigms into real-life productivity and supports the .NET components and libraries natively as well as the Common Language Infrastructure (CLI). Functional programming has long been associated with academics and experts. F# is one of the few languages offering a complete environment that is mature enough to comfortably be integrated into an organization.
Also, F# has extensive support for parallel programming, where advanced features such as asynchronous and multi-threaded concepts are implemented as language constructs. It hides a lot of implementation details from the programmer. In F#, the functional programming paradigm is the main philosophy used to solve problems. The other paradigms, object-oriented and imperative programming, are prioritized to be used as subsidiaries and complements for this main paradigm. Reasons for them to coexist, involves compatibility and pragmatic, real-world productivity concerns.
We will start by introducing Visual Studio as the main tool of choice for this book. Although it's possible to use the standalone F# compiler and your favorite editor, you will most likely be more productive using Visual Studio 2012, as we will do throughout this book.
F# has been a part of Visual Studio since 2010. We will use the latest version of Visual Studio and F# throughout this book. This will enable us to use the latest functionality and enhancements available in Visual Studio 2012 and F# 3.0.
F# is open source, which means you can use it on any supported platform; it's not bound to Microsoft or Visual Studio. There is good support in other IDEs, such as MonoDevelop, which will run on Linux and Mac OS X.
For more information about F# and the F# Software Foundation, visit http://fsharp.org.
Create a new project in Visual Studio for F#, which is to be used in this guide to explore the basics, as shown in the following sections.
Using the following steps, we can create a new project in Visual Studio:
Let's have a brief look at the program template generated by Visual Studio.
If you run this program, which will just print out the arguments passed to it, you will see a terminal window appear.
The [<EntryPoint>] function in the preceding screenshot is the main function, which tells Visual Studio to use that particular function as the entry point for the program executable. We will not dig any deeper into this program template for now, but we will come back to this in the last three chapters when we'll build the trading system.
We will use an F# script file after having looked at the standard program template instead of exploring the basics of the language in a more interactive fashion. You can think of F# script files as notebooks, where you have executable code that you can explore in pieces in an incremental style:
Now that we have set up the basic project structure in Visual Studio, let's continue and explore F# Interactive.
Type inference means that the compiler will automatically deduce the type of an expression used in the code, based on the information provided from the programmer about the context of the expression. Type inference analyses the code, as you have seen in the preceding section, to determine types that are often obvious to the programmer. This spares the programmer from having to explicitly define the types of every single variable. It's not always needed to have the types defined to be able to understand the code, as seen in the preceding section for simple assignments of integers and floats. Type inference will make the code easier to write, and as a consequence, easier to read, leaving a lot of ceremony where it belongs.
