Introduction

        Overview
        What This Book Includes
        Is This Book for You
        What Do You Need to Use This Book
        How This Book is Organized
        Structure of Sample Projects
        Using Code Examples
        Customer Support


Overview

Welcome to Practical C# and WPF for Financial Markets. This book will provide all the tools you need to develop professional financial applications using the C#, the Windows Presentation Foundation (WPF), and the Model-View-View Model (MVVM) pattern based on the .NET 4.5 Framework. I hope this book will be useful for quant developers, quant analysts, individual traders, .NET programmers, and students of all skill levels.

In recent years, quantitative finance has been an attractive field due to the intellectual challenge and high remuneration. Many scientists, engineers, and students wish to change their careers to become a quant developer/analyst in investment banks and hedge fund firms. Most of them have solid background in mathematics, statistical analysis, physics modeling, and programming, but lack knowledge and experience in quantitative finance. A question that they constantly ask is “what do I need to prepare myself to become a quant developer and analyst?” This book will provide answer to this question and prepare you with solid technical skills in quantitative analysis and development.

On the other hand, more and more individuals want to become independent (“retail”) quantitative traders who are looking to start their own quantitative or algorithmic trading business. The most common issue they are facing is what kind of background do they need in order to be success in quantitative trading? Most of those individuals received their advanced degrees in physics, math, engineering, or computer science. This kind of training in the hard sciences will give them an edge in quantitative analysis and pricing complex derivative instruments. However, the capability to convert trading ideas into trading strategies and the programming skill in implementing the automatic trading system are equally important. This book will prepare you with all the necessary analysis and programming techniques to become a well-equipped individual quant trader.

So what programming languages are most commonly used in quantitative finance? C++ is traditionally associated with finance applications for pricing complex derivative securities, and much of the older financial infrastructure is also based on C++. C# and .NET Framework are a relatively new technology comparing to C++. This book will choose C# and WPF as our programming framework in developing various business applications in financial markets.

The reason for using C# in this book is that C# is relatively easy to learn comparing to C++. Scientists, engineers, students, quants, and traders can learn C# and use it to develop financial applications quickly. People with a background in VBA, R, or Java will find the transition to C# much easier than the transition to C++. Furthermore, in many cases developers’ productivity levels are much higher than those achieved with C++. It is also possible to create interoperable .NET applications that contains different technologies such as C++ and VBA legacy code.

The two key features in WPF, data binding and MVVM pattern, can further enhance developers’ productivity. The data binding provides a simple and consistent way for .NET applications to present and interact with data. It has several advantages over traditional models, including a broad range of properties that inherently support data binding, flexible UI (user interface) representation of data, and clean separation of business logic from UI.

The MVVM pattern is the most used architecture for WPF applications. MVVM introduces three layers of separation of application code, namely, Model, View, and ViewModel. View holds the actual UI; ViewModel holds the collection of properties, commands, and property changed notifications; while Model holds business data, business logic, and business rule. You will gain several advantages of using MVVM pattern, including 1) proper separation of the view and the data. The data is not stored in view and the view is just for presenting the data; 2) clean testable and manageable code; and 3) No code-behind so the presentation layer and the logic are loosely coupled.

This book is written with the intention of providing a complete and comprehensive explanation of C# and WPF programming in quantitative finance. It pays special attention to creating various business applications and reusable .NET libraries that can be used directly in real-world finance applications. Much of this book contains original work based on my own programming experience when I was developing business applications for quantitative analysis in financial field.

Practical C# and WPF for Financial Markets provides everything you need to create your own advanced applications in quantitative finance and reusable packages using C# and WPF based on MVVM pattern. It shows you how to use C# and WPF to create a variety of financial applications that range from simple database, market data API, data visualization, quantitative analysis to pricing equity options and complex fixed income instruments, machine learning, and trading strategy development. I will try my best to introduce you to C# and WPF programming in quantitative finance in a simple way – simple enough to be easily followed by a quant or .NET developer who has basic prior experience in developing business applications using .NET technology.

What this Book Includes

This book and its sample code listings, which are available for download at my website at www.drxudotnet.com, provide you with:

Is This Book for You?

You do not have to be an experienced quant developer/analyst or .NET developer to use this book. I designed this book to be useful to people of all levels of .NET programming experience and financial background. In fact, I believe that if you have some prior experience with the quantitative analysis and development, programming language C++, Java, VBA, C#, or Windows Forms, you will be able to sit down in front of your computer, start up Microsoft Visual Studio Community 2013, follow the examples provided in this book, and quickly become proficient with quantitative application development. For those of you who are already experienced quant analyst/developer or .NET developers, I believe this book has much to offer as well. A great deal of the information in this book about .NET programming in quantitative finance is not available in other tutorial and reference books. In addition, you can use most of the example programs in this book directly in your own real-world application development. This book will provide you with a level of detail, explanation, instruction, and sample program code that will enable you to do just about anything related to quantitative finance application development using C# and WPF.

Perhaps you are a scientist, an engineer, a mathematician, or a student, rather than a professional quant developer/analyst or .NET programmer; nevertheless, this book is still a good bet for you. In fact, my own background is in theoretical physics, a field involving extensive physical modeling, numerical calculations, and graphical representations of calculated data. I devoted my effort to this field for many years, starting from undergraduate up to PhD. My first computer experience was with FORTRAN. Later on, I had programming experience with Basic, C, C++, and MATLAB. I always tried to find an ideal development tool that would allow me not only to generate data easily (computation capability) but also to represent data graphically (graphics and chart power). The C# and Microsoft Visual Studio .NET development environment made it possible to develop such integrated applications. Ever since Microsoft .NET 1.0 came out, I have been in love with the C# language, and I have been able to use this tool successfully to create powerful business applications for quantitative analysis when I have worked on Wall Street.

Quant analysts/developers, individual quant traders, and .NET developers can use the majority of the example programs in this book routinely. Throughout the book, I will emphasize the usefulness of .NET programming to real-world quantitative finance applications. If you follow closely the instructions presented in this book, you will easily be able to develop various practical business applications in quantitative finance, from linear analysis, machine learning to pricing engines, and trading strategy development. At the same time, I won’t spend too much time discussing programming style, execution speed, and code optimization, because a plethora of books out there already deal with these topics. Most of the example programs you will find in this book omit error handlings. This makes the code easier to understand by focusing only on the key concepts and practical applications.

What Do You Need to Use This Book?

You will need no special equipment to make the best use of this book and understand the algorithms. To run and modify the sample programs, you will need a computer capable of running either Windows 7, 8, or 10. The software installed on your computer should include Visual Studio 2013 (Community version is fine), the .NET 4.5 standard edition or higher, and SQL Server Express 2012 or higher. If you have Visual Studio 2012, .NET 4.0, and SQL Server Express 2008 or older versions, you can also run most of the sample code with few modifications. Please remember, however, that this book is intended for Visual Studio 2013, .NET 4.5, and SQL Server Express 2014, and that all of the example programs were created and tested on this platform, so it is best to run the sample code on the same platform.

How the Book Is Organized

This book is organized into eleven chapters, each of which covers a different topic about quantitative finance applications using C# and WPF. The following summaries of each chapter should give you an overview of the book’s content:

Chapter 1, Overview of C# and WPF Programming
This chapter introduces the basics of WPF and reviews some of the general aspects of WPF programming, including XAML files used to define user interfaces.

Chapter 2, Introduction to MVVM
This chapter introduces processes for implementing the MVVM pattern in WPF applications; including data binding with property changed notifications, command binding, lambda expression, and observable collections. It also reviews some free open-source MVVM tool kits, and shows why we will use Caliburn.Micro in this book.

Chapter 3, Database
This chapter introduces the SQL Server Data Tool (SSDT) and LocalDB that are built-in features and shipped as part of the core product of Visual Studio 2013. It shows you how to create simple database and how to interact with the data. It also reviews ADO.NET Entity Framework that allows developers without extensive knowledge about SQL to interrogate the database, create complex queries, and generate classes with the help of a user-friendly interface.

Chapter 4, Market Data
This chapter contains instructions on how to interactive with market data providers’ API and how to get the free market data from online data source. These market data include the end of the day (EOD) stock data, intraday data, interest rate data, foreign exchange rate data, and option chain data. I will also discuss how to use Bloomberg terminal’s API to retrieve market data.

Chapter 5, Data Visualization
Data visualization plays a critical role in quantitative finance and trading. Quant analysts and traders need to monitor the real-time changes in market and trading signals visually on their screen. I will show you how to use a chart control library I created to display the market data. I will also discuss how to convert the Windows Forms version of the MSChart control into a WPF and MVVM compatible user control. I will show you how to use this control in developing quantitative finance applications.

Chapter 6, Linear Analysis
This chapter presents the most fundamental analysis approach in quantitative finance based on linear analysis. I will discuss how to develop different business applications using the linear regression, principal component analysis (PCA), and correlation.

Chapter 7, Technical Indicators
This chapter discusses various technical indicators, which are often used in quantitative analysis. A technical indicator is just a mathematical calculation based on historic market data including price and volume that is used to predict market direction. I will show you how to use the FinancialFormula object in the MSChart control to display different indicators on your screen and how to extract the output results from the indicators.

Chapter 8, Machine Learning
This chapter discusses the advanced quantitative analysis techniques: machine learning. Machine-learning technique has become one of the most promising fields in quantitative finance. It is widely used in quantitative finance for predicting the future stock prices. This chapter will concentrate on the supervised learning and covers several commonly used machine-learning algorithms in finance, including the K-nearest neighbors, support vector machines, and neural networks.

Chapter 9, Options Pricing
This chapter covers the Black-Scholes formula used for options pricing. It shows several different implementations for calculating the price and Greeks of the European and American options. It also discusses how to use the open source quant libraries to price various options, including barrier options, Bermudan options, and other exotic options.

Chapter 10, Pricing Fixed-Income Instruments
This chapter shows how to price the fixed-income instruments, including interest rates, bonds, and credit default swaps, and discusses various related topics, such as cash flows, term structures, yield curves, discount factors, and zero-coupon bonds. I will also provide the detailed procedures on how to use the open-source libraries to price these complex financial instruments.

Chapter 11, Trading Strategies and Backtesting
This chapter presents several trading strategies using the simple quantitative analysis techniques, including the moving average and linear regression, as well as the commonly used technical indicators. I will also present a long-short based backtesting framework, which allows you to examine the historical performance of your trading strategies for single stock trading and stock pairs trading.

Structure of Sample Projects

In this book, I will use example projects to show the detailed implementations for various business applications in quantitative finance. The following figure show how the sample projects are structured.

From the left panel, you can see that this book contains eleven chapters. You can click the chapter button to access the sample projects in the corresponding chapter. Here, I have clicked the “Chapter 1” button and its background (black) and foreground (white) change colors, indicating the Chapter 1 is selected. At the same time, the TabControl on the right panel will show all of the sample projects in Chapter 1 accordingly. You can then access the different projects in Chapter 1 by clicking different tabs. This way, you can access whatever sample project you like.


Using Code Examples

You may use the code in this book in your own applications and documentation. You do not need to contact the author or the publisher for permission unless you are reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing the example code listings does require permission. Incorporating a significant amount of example code from this book into your applications and documentation also requires permission. Integrating the example code from this book into commercial products is not allowed without written permission of the author.

Customer Support

I am always interested in hearing from readers, and enjoy learning of your thoughts on this book. You can send me comments by e-mail to: jxu at DrXuDotNet.com. I also provide updates, bug fixes, and ongoing support via my website:

www.DrXuDotNet.com

You can also obtain the complete source code for all of examples in this book from the foregoing website.