# F# for Quantitative Finance

Formats:

save 15%!

save 37%!

**Free Shipping!**

Also available on: |

- Learn functional programming with an easy-to-follow combination of theory and tutorials
- Build a complete automated trading system with the help of code snippets
- Use F# Interactive to perform exploratory development
- Leverage the .NET platform and other existing tools from Microsoft using F#

### Book Details

**Language :**English

**Paperback :**286 pages [ 235mm x 191mm ]

**Release Date :**December 2013

**ISBN :**1782164626

**ISBN 13 :**9781782164623

**Author(s) :**Johan Astborg

**Topics and Technologies :**All Books, Open Source

## Table of Contents

PrefaceChapter 1: Introducing F# Using Visual Studio

Chapter 2: Learning More About F#

Chapter 3: Financial Mathematics and Numerical Analysis

Chapter 4: Getting Started with Data Visualization

Chapter 5: Learning Option Pricing

Chapter 6: Exploring Volatility

Chapter 7: Getting Started with Order Types and Market Data

Chapter 8: Setting Up the Trading System Project

Chapter 9: Trading Volatility for Profit

Chapter 10: Putting the Pieces Together

Index

- Chapter 1: Introducing F# Using Visual Studio
- Introduction
- Getting started with Visual Studio
- Creating a new F# project
- Creating a new project in Visual Studio
- Understanding the program template
- Adding an F# script file

- Creating a new F# project

- Understanding F# Interactive
- Language overview
- Explaining mutability and immutability
- Primitive types
- Explaining type inference
- Explaining functions
- Learning about anonymous functions
- Explaining higher-order functions
- Currying
- Investigating lists
- Concatenating lists

- Tuples
- The pipe operator

- Documenting your code
- Your first application
- The whole program
- Understanding the program
- Extending the example program
- The entire program

- The power of prototyping
- Functional languages in quantitative finance
- Understanding the imperative code and interoperability
- Summary

- Chapter 2: Learning More About F#
- Structuring your F# program
- Looking into modules
- Using functions and values in modules

- Namespaces

- Looking into modules

- Looking deeper inside data structures
- Record types
- Discriminated unions
- Enumerations
- Arrays
- Interesting functions in an array module

- Lists
- Pattern matching and lists
- Interesting functions in a list module

- Sequences
- Interesting functions in the sequence module

- Sets
- Maps
- Interesting functions in the map module
- Options
- Strings
- Interesting functions in the string module

- Choosing data structures
- Arrays
- Lists
- Sets
- Maps

- More on functional programming
- Recursive functions
- Tail recursion
- Pattern matching
- Incomplete pattern matching
- Using guards
- Pattern matching in assignment and input parameters
- Active patterns

- Introducing generics
- Lazy evaluation
- Units of measure

- Asynchronous and parallel programming
- Events
- Background workers
- Threads
- Thread pools
- Asynchronous programming
- The F# asynchronous workflows
- Asynchronous binding
- Examples of using an async workflow

- Parallel programming using TPL
- MailboxProcessor

- A brief look at imperative programming
- Object-oriented programming
- Classes
- Objects and members
- Methods and properties
- Overloaded operators

- Object-oriented programming

- Using XML documentation
- Useful XML tags
- Typical XML documentation

- Summary

- Structuring your F# program

- Chapter 3: Financial Mathematics and Numerical Analysis
- Understanding the number representation
- Integers
- Two's complement

- Floating-point numbers
- The IEEE 754 floating-point standard

- Integers

- Learning about numerical types in F#
- Arithmetic operators
- Learning about arithmetic comparisons
- Math operators
- Conversion functions
- Introducing statistics
- Aggregate statistics
- Calculating the sum of a sequence
- Calculating the average of a sequence
- Calculating the minimum of a sequence
- Calculating the maximum of a sequence
- Calculating the variance and standard deviation of a sequence
- Looking at an example application

- Aggregate statistics

- Using the Math.NET library
- Installing the Math.NET library
- Introduction to random number generation
- Pseudo-random numbers
- Mersenne Twister

- Probability distributions
- Normal distribution

- Statistics
- Linear regression
- Using the least squares method
- Using polynomial regression

- Learning about root-finding algorithms
- The bisection method
- Looking at an example

- Finding roots using the Newton–Raphson method
- Looking at an example

- Finding roots using the secant method
- Looking at an example

- The bisection method

- Summary

- Understanding the number representation

- Chapter 4: Getting Started with Data Visualization
- Making your first GUI in F#
- Composing interfaces
- More about agents
- The user interface
- The main application
- Learning about event handling

- Displaying data
- Extending the form to use a table
- Displaying financial data from Yahoo! Finance
- Understanding the application code

- Extending the application to use Bollinger bands
- Using FSharp.Charting
- Creating a candlestick chart from stock prices

- Creating a bar chart
- Summary

- Chapter 5: Learning Option Pricing
- Introduction to options
- Looking into contract specifications
- European options
- American options
- Exotic options
- Learning about Wiener processes

- Learning the Black-Scholes formula
- Implementing Black-Scholes in F#
- Using Black-Scholes together with charts
- Introducing the greeks
- First-order greeks
- Second-order greeks

- Implementing the greeks in F#
- Delta
- Gamma
- Vega
- Theta
- Rho
- Investigating the sensitivity of the greeks
- Code listing for visualizing the four greeks

- The Monte Carlo method
- Summary

- Introduction to options

- Chapter 6: Exploring Volatility
- Introduction to volatility
- Actual volatility
- Implied volatility
- Exploring volatility in F#
- The complete application

- Learning about implied volatility
- Solving for implied volatility

- Delta hedging using Black-Scholes
- Exploring the volatility smile
- Summary

- Introduction to volatility

- Chapter 7: Getting Started with Order Types and Market Data
- Introducing orders
- Order types
- Market orders
- Limit orders
- Conditional and stop-orders

- Order properties

- Order types

- Understanding order execution
- Introducing market data
- Implementing simple pretrade risk analysis
- Validating orders

- Introducing FIX and QuickFIX/N
- Using FIX 4.2
- Configuring QuickFIX to use the simulator

- Using FIX 4.2

- Summary

- Introducing orders

- Chapter 8: Setting Up the Trading System Project
- Explaining automated trading
- Understanding software testing and test-driven development
- Understanding NUnit and FsUnit
- Requirements for the system
- Setting up the project
- Installing the NUnit and FsUnit frameworks

- Connecting to Microsoft SQL Server
- Introducing type providers
- Using LINQ and F#
- Explaining sample code using type providers and LINQ

- Creating the remaining table for our project

- Using LINQ and F#

- Writing test cases
- Details about the setup

- Summary

- Chapter 9: Trading Volatility for Profit
- Trading the volatility
- Plotting payoff diagrams with FSharpCharts
- Learning directional trading strategies
- Trading volatility using options
- Trading the straddle

- Trading the butterfly spread
- The long butterfly spread
- The short butterfly spread

- Trading the VIX
- Trading the delta neutral portfolio

- Trading volatility using options

- Deriving the mathematics
- Hedging with implied volatility
- Implementing the mathematics

- Learning relative value trading strategies
- Trading the slope of the smile

- Defining the trading strategy
- Case 1 – increasing the slope
- Case 2 – decreasing the slope
- Defining the entry rules
- Defining the exit rules

- Summary

- Chapter 10: Putting the Pieces Together
- Understanding the requirements
- Revisiting the structure of the system
- Understanding the Model-View-Controller pattern
- The model
- The view
- The controller

- Executing the trading strategy using a framework
- Building the GUI
- Presenting information in the GUI
- Adding support for downloading the data
- Looking at possible additions to the system
- Improving the data feed
- Support for backtesting
- Extending the GUI
- Converting to the client-server architecture

- Summary

### Johan Astborg

### Code Downloads

Download the code and support files for this book.

### Submit Errata

Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.

### Errata

- 17 submitted: last submission 05 Feb 2014Errata Category: **Code**

On page no: **110**

Under the **Finding roots using the secant method** heading:

The last line of the code is:

**secant (n + 1) N f x x0**

It should be:

**secant (n + 1) N f x x0 x2**

Errata Category: **Typo**

On page no: **149**

Under the **Investigating the sensitivity of the greeks** heading:

The figure caption is: **The surface plot of Delta, Gamma, Theta, and Rho of a call option**

It should be this: **The surface plot of Delta, Gamma, Theta, and Vega of a call option**

Errata Category: **Code**

On page no: **17**

Under the **Explaining higher-order functions** heading:

The following code snippet:

let squareByFour f

f 4

> squareByFour square

Should be:

**let squareByFour f =**

**f 4**

**squareByFour square**

Errata Category: **Typo**

On page no: **49**

The statement: **We can check whether s1 contains 9**

Should be: **We can check whether s1 contains 1**

Errata Category: **Technical**

On page no: **40**

Under the **Interesting functions in an array module **heading:

The decription for the **Array.zip a b** function is **Sorts the elements in array a in increasing order, see sortBy to use predicate**

It should be: **Zips the elements in array a together with elements in b, see zipBy to use predicate**

Errata Category:** Code**

On page: **31**

Under the **Using functions and values in modules** heading:

In the code snippet:

**module Module2 =**

**let Version() = **

**""Version 1.0"**

Should be

**module Module2 =**

**let Version() = **

**"Version 1.0"**

Errata Category:** Code**

On page: **17** and **18**

Under the **Currying** heading:

In the code snippet:

**> sumBy2 4;;**

**val it : int = 5**

**> sumBy2 5;;**

**val it : int = 5**

Should be:

**> sumBy2 4;;**

**val it : int = 6**

**> sumBy2 5;;**

**val it : int = 7**

Errata Category: **Code**

On page: **26**

Under **The entire program** heading:

**let filePath = @" table.csv"**

Should be:

**let filePath = @"table.csv"**

Errata Category: **Layout**

On page: **39**

**This can be shortened further, for example, if you select the elements **

**from the beginning to the element with index 2: array1.[..2] **

This section is the part of the text content and not the part of the code.

Please ignore the incorrect formatting.

Errata Category:** Code**

On page no: **37**

The code snippet:

**let toggle1 = match x with | Buy -> Sell | Sell -> Buy**

Should be:

**let toggle1 x =**

**match x with**

**| Buy -> Sell**

**| Sell -> Buy**

**Node of Tree * Tree**

Errata Category:** Code**

On page no:** 38**

Under the **Enumerations** heading:

**let col1 : Color = Color.Red**

Should be:

**let col1 : RGB = RGB.Red**

Errata Category:** Code**

**On page no: ****46**

After the paragraph: **Minimum and maximum values of a sequence are found by Seq.minand Seq.max, respectively:**

In the code snippet:

**let seq1 = {1 .. 10}**

Should be added.

Errata Category:** Code**

**On page no:****47**

The following line in the code snippet appears twice:

**> Seq.filter (fun elem -> elem > 3) seq1;;**

**val it : seq<int> = seq [4; 5; 6; 7; ...]**

One of them should be removed.

Errata Category:** Code**

**On page no: 57**

**The following code snippet:**

**let rec tailrecfact n acc =match n with| 0 -> acc| _ -> trecfact (n - 1) (acc * n)tailrecfact n 1**

Should be:

**let rec tailrecfact n acc =**

match n with

| 0 -> acc

**| _ -> tailrecfact (n - 1) (acc * n)**

**tailrecfact n 1**

Errata Category:** Code**

On page no:

**62**

Under the **Introducing generics** heading:

The code indentation should be:

**let genericListMaker<'T>(x, y, z) = let list = new List<'T>() list.Add(x) list.Add(y) list.Add(z) list**

Errata Category:** Technical**

On page no:** 87**

**As you can see, the range for the 8-bit signed integers is from -128 to -127**

Should be: **As you can see, the range for the 8-bit signed integers is from -128 to 127**

Errata Category: **Code**

On page no: **96**

Under the **Calculating variance** heading

The line in the code snippet:

**let variance(values=**

Should be:

**let variance values =**

### Sample chapters

You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

- Use Visual Studio as your main tool for writing F#
- Utilize F# to aggregate data and calculate statistics
- Plot and visualize data in F#
- Learn about volatility, delta hedging, and volatility arbitrage
- Understand basic numerical analysis and algorithm implementation
- Model orders and market data together with basic pre-trade risk
- Structure and write object-oriented code
- Develop larger programs using F#
- Explore automated trading systems and quantitative trading models

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.

The approach is to guide you as a reader from the basics of functional programming and F# to more complex tasks using tutorials and a lot of code examples. As you gain more confidence through out the book, you will be able to modify and write your own code to solve various problems in finance.

If you are a practitioner of quantitative finance, economics, or mathematics and wish to learn F#, then this book is for you. You may have a basic conceptual understanding of financial concepts and models, but no previous knowledge is expected.