Table Of ContentClojure for Finance
Leverage the power and flexibility of the Clojure
language for finance
Timothy Washington
BIRMINGHAM - MUMBAI
Clojure for Finance
Copyright © 2015 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: January 2016
Production reference: 1040116
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-78528-928-6
www.packtpub.com
Credits
Author Project Coordinator
Timothy Washington Sanjeet Rao
Reviewers Proofreader
Ed Babcock Safis Editing
Nicholas Quirk
Dajana Štiberová Indexer
Hemangini Bari
Acquisition Editors
Richard Brookes-Bland Graphics
Disha Haria
Divya Poojari
Production Coordinator
Content Development Editor
Nilesh Mohite
Shweta Pant
Cover Work
Technical Editor
Nilesh Mohite
Rahul C. Shah
Copy Editor
Sonia Cheema
About the Author
Timothy Washington is a senior software developer with over 15 years of
experience in designing and building enterprise web applications from end to
end. His experience includes delivering stable, robust software architectures to
organizations ranging from start-ups to Fortune 500 companies. His skills include
managing agile projects, systems analysis and design, functional programming, DSL
and language design, and object-oriented design, with contributions to the open
source community.
Thanks to Richard Brooks-Bland for first convincing me to write
this book. This wouldn't have happened without your goading. I'd
also like to thank Chris Zheng for the first bits of feedback, and all
the reviewers, for their hard work, catching errors I would have
otherwise missed.
About the Reviewers
Ed Babcock is a software developer who has been enjoying Clojure and
ClojureScript since first hearing about them back in 2011. Over the last few years, he
has been able to use Clojure for a variety of projects, ranging from mobile and web
development to a large-scale search engine. Having worked with other programming
languages in the past, he is certain that Clojure's simple design patterns and excellent
community have made him a much better programmer. He is currently working
for HomeSpotter, a home search start-up, and exploring cross-platform application
development though several open source projects on GitHub.
Nicholas Quirk is currently finishing his master's degree in computer science.
He has 5 years of industry experience, primarily with Java web application
development; however, he also loves LISP dialects, such as Clojure, and the history
of computer science itself. His staples in life are reading and programming. If
you can't find him doing either of these, he might be doodling, hiking, mountain
stomping, or enjoying the company of friends and family.
I would like to thank my friends and family for their support as I've
bought my first house.
Dajana Štiberová is a young Clojure enthusiast who's based in Bratislava, Slovakia.
After deciding to abandon her work in the finance sector, she rediscovered her passion
for math and analytical thinking with the help of programming. She currently works
on distributed testing platforms that are written almost entirely in Clojure.
She likes to spend her free time cycling to discover new roads and places, helping
animals, and seeks to improve the lives of children in hospitals.
www.PacktPub.com
Support files, eBooks, discount offers, and more
For support files and downloads related to your book, please visit www.PacktPub.com.
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 service@packtpub.com 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.
TM
https://www2.packtpub.com/books/subscription/packtlib
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital
book library. Here, you can search, access, and read Packt's entire library of books.
Why subscribe?
• Fully searchable across every book published by Packt
• Copy and paste, print, and bookmark content
• On demand and accessible via a web browser
Free access for Packt account holders
If you have an account with Packt at www.PacktPub.com, you can use this to access
PacktLib today and view 9 entirely free books. Simply use your login credentials for
immediate access.
Table of Contents
Preface v
Chapter 1: Orientation – Addressing the Questions Clojure
Answers 1
Notions of computation 2
Notions of finance 3
Concrete types of computation 4
Tooling 5
A first look at Clojure's core functions 7
Primitives 7
Collections 10
Summary 12
Chapter 2: First Principles and a Useful Way to Think 13
Modeling stock price activity 14
Function evaluation 14
First-class functions 15
Lazy evaluation 15
Basic Clojure functions and immutability 16
Namespaces and creating our first function 17
The Read-Eval-Print-Loop 17
Basic data structures 19
Macros and more in-depth data transformation 20
Elaborating our equation 23
Summary 28
Chapter 3: Developing the Simple Moving Average 29
Perception and representation 30
Knowing the data input 30
Knowing the data output 31
[ i ]
Table of Contents
Reasoning about the equation needed to achieve our output 32
Understanding Vars and bindings 34
Working with lazy sequences 35
Implementing our equation 35
Destructuring 42
Summary 45
Chapter 4: Strategies for Calculating and Manipulating Data 47
Our first refactor – the price list 47
The exponential moving average 49
The Bollinger Bands 53
Summary 56
Chapter 5: Traversing Data, Branching, and Conditional Dispatch 57
Our second refactor – the generate prices function 57
Polynomial expressions 60
A sine wave 64
Stitching the pieces together 67
Surveying the function landscape 69
Traversing data 69
Branching and conditional dispatch 70
First order functions 71
Applying functions 71
Summary 83
Chapter 6: Surveying the Landscape 85
Scalar data types 86
Numbers and precision 86
A review of collections 89
Data transformation patterns and principles 90
Clojure's model of state and identity 93
Introducing side effects 96
Concurrency and parallelism 97
Type systems 98
Comparing Clojure with object orientation 99
Comparing Clojure with FP and strong typing 103
Summary 107
Chapter 7: Dealing with Side Effects 109
Simple writing 110
Extensible Data Notation 110
Devising a persistence strategy 111
[ ii ]
Table of Contents
Consuming from a data stream 113
Using a componentized architecture 116
Summary 121
Chapter 8: Strategies for Using Macros 123
Simple reading 123
Functions for querying a system 125
An example of a regular expression 125
A basic lookup 126
Flattening structures 127
A more expressive lookup 128
A simple query language 130
Variable argument functions 130
The :pre and :post function conditions 131
The juxt higher order function 133
Separate OR AND lookups 135
Deriving a query language using macros 139
Summary 143
Chapter 9: Building Algorithms – Strategies to Manipulate and
Compose Functions 145
Structuring our data for further analysis 145
A third refactor of the analytic functions 148
Signals using moving averages 151
The Relative Strength Index 153
Bollinger Band signals 156
Summary 163
Index 165
[ iii ]