Saturday, September 5, 2009
Presenting Erlang at the Polyglot Programmers of Chicago meetup on September 23rd
I'll be visiting Obtiva later this month to present Erlang, here are the details. I'm probably going to gloss over plain vanilla functional programming principles in order to focus more on concurrency, as well as another equally important and overlooked core feature ... reliability. We'll cover the basics of what an Erlang process is, runtime code swapping, and a brief intro to a few Erlang OTP behaviours. Attendees should walk away from this presentation with a few new ideas in their heads and enough information to be dangerous. My slides are on SlideShare.
Tuesday, August 18, 2009
Option Volatility & Pricing in Erlang, Java and JavaScript


You can view the source code here if you want to in take a look under the hood. In a nutshell I used Erlang to model a #position{}, composed of long and short #side{} records. Each #side{} is composed of #option{} and #underlying{} records. The PNL of a #position{} is calculated and graphed. The data is serialized as JSON to any browser where it is rendered with a Java applet and JQuery event handlers.
Monday, August 17, 2009
My first impressions of the Scala programming language
Is Scala the new Java? I'd bet money on it for two reasons. First, Scala excels at several things that are not available in Java. The type system and closures have effectively grabbed the attention of language enthusiasts and fad followers. But to replace an institution like Java you need more. You need the average Joe to vote for you. This is the second reason: the cost of switching is low. Java developers won't have trouble with a transition like this. Idiomatic Scala might take a little effort ... but at a bare minimum, there is a one to one conceptual match up across the two languages. I dusted off a Java applet and ported it to Scala, see for yourself.
After playing with Scala a little bit I found my first problem with it. I could not have picked a worse way to get my feet wet. I'm not fond of applets but I had one laying around and thought I'd make things interesting by porting it. Then I fired up my web server and browser to find that my applet no longer worked. After a little while I traced the failure to a class loader problem in the browser. It turns out that everything that passes through scalac has a dependency on scala.ScalaObject and a host of other things in the Scala runtime. This means my applet gains 3.5 MB . Not a show stopper, but I think it's safe to say this is one thing Scala will not excel at.
And finally, I have no idea what's wrong with scala.collection.mutable.PriorityQueue . This REPL session says it all ...
scala> val q = new scala.collection.mutable.PriorityQueue[Int]
q: scala.collection.mutable.PriorityQueue[Int] = PriorityQueue()
scala> q.isEmpty
res0: Boolean = true
scala> q.size
res1: Int = 1 <- This has got to be a bug?
Wednesday, July 22, 2009
QCon Presentation Published by InfoQ
Sunday, June 14, 2009
Erlang at SpeakerConf: What is an Erlang process?
Thursday, June 4, 2009
Option Profit and Loss in Erlang
Options are a complex game. I was quite surprised at how many of the rules can be expressed in a dozen lines of code. This function calculates the PNL of an options position at the market price of the underlying.
pnl(Px, #position{long = Long, short = Short}) ->
Pnl = [ Px - U#underlying.px || U <- Long#side.underlyings ] ++
[-Px + U#underlying.px || U <- Short#side.underlyings ] ++
[-C#option.px || C <- Long#side.calls, Px =< C#option.strike ] ++
[ C#option.px || C <- Short#side.calls, Px =< C#option.strike ] ++
[-P#option.px || P <- Long#side.puts, Px >= P#option.strike ] ++
[ P#option.px || P <- Short#side.puts, Px >= P#option.strike ] ++
[ Px - C#option.strike - C#option.px || C <- Long#side.calls, Px > C#option.strike ] ++
[-Px + C#option.strike + C#option.px || C <- Short#side.calls, Px > C#option.strike ] ++
[ Px - P#option.strike + P#option.px || P <- Short#side.puts, Px < P#option.strike ] ++
[-Px + P#option.strike - P#option.px || P <- Long#side.puts, Px < P#option.strike ],
lists:sum(Pnl).
Before we dive into the nuts and bolts of this function let's first discuss list comprehensions and how they are written in Erlang. A list comprehension is a way of composing a list from another list. For example, the following line of code creates a list of four integers and doubles every element greater than two.
[ X * 2 || X <- [1,2,3,4], X > 2 ]
% yields [6,8]
This expression can be read as "Double every X taken from [1,2,3,4] where X is greater than 2". Everything to the left of "||" is known as the expression template while everything to the right is known as the generator. The pnl/0 function sums the concatenated results of ten list comprehensions. Each list comprehension is an expression of how the business works. Let's walk through these expressions, one by one. Keep symmetry in your mind along the way ...
Line 1: Long underlyings
[ Px - U#underlying.px || U <- Long#side.underlyings ]You gain(lose) the market price less the underlying price for every underlying instrument you buy.
Line 2: Short underlyings
[-Px + U#underlying.px || U <- Short#side.underlyings ]You gain(lose) the underlying price less the market price for every underlying instrument you sell.
Line 3: Out of the money long calls
[-C#option.px || C <- Long#side.calls, Px =< C#option.strike ]You lose the premium for every call you buy if it expires worthless.
Line 4: Out of the money short calls
[ C#option.px || C <- Short#side.calls, Px =< C#option.strike ]You keep the premium for every call you sell if it expires worthless.
Line 5: Out of the money long puts
[-P#option.px || P <- Long#side.puts, Px >= P#option.strike ]You lose the premium for every put you buy if it expires worthless.
Line 6: Out of the money short puts
[ P#option.px || P <- Short#side.puts, Px >= P#option.strike ]You keep the premium for every put you sell that expires worthless.
Line 7: In the money long calls
[ Px - C#option.strike - C#option.px || C <- Long#side.calls, Px > C#option.strike ]You keep the market price less the strike price and premium for every call you buy when the market price exceeds strike price. Bonus points to anyone who has noticed that by this point the function begins a second traversal of the options.
Line 8: In the money short calls
[-Px + C#option.strike + C#option.px || C <- Short#side.calls, Px > C#option.strike ]You lose the strike price and the premium less the market price for every call you sell when you are assigned.
Line 9: In the money short puts
[ Px - P#option.strike + P#option.px || P <- Short#side.puts, Px < P#option.strike ]You lose market price plus the premium less the strike price for every put you sell when you are assigned.
Line 10: In the money long calls
[-Px + P#option.strike - P#option.px || P <- Long#side.puts, Px < P#option.strike ]You keep the strike price less the market price and premium when strike price exceeds market price.
Now let's look at some trends.
Big Risks
pnl(Px, #position{long = Long, short = Short}) ->
Pnl = [ Px - U#underlying.px || U <- Long#side.underlyings ] ++
[-Px + U#underlying.px || U <- Short#side.underlyings ] ++
[-C#option.px || C <- Long#side.calls, Px =< C#option.strike ] ++
[ C#option.px || C <- Short#side.calls, Px =< C#option.strike ] ++
[-P#option.px || P <- Long#side.puts, Px >= P#option.strike ] ++
[ P#option.px || P <- Short#side.puts, Px >= P#option.strike ] ++
[ Px - C#option.strike - C#option.px || C <- Long#side.calls, Px > C#option.strike ] ++
[-Px + C#option.strike + C#option.px || C <- Short#side.calls, Px > C#option.strike ] ++
[ Px - P#option.strike + P#option.px || P <- Short#side.puts, Px < P#option.strike ] ++
[-Px + P#option.strike - P#option.px || P <- Long#side.puts, Px < P#option.strike ],
lists:sum(Pnl).
Big Rewards
pnl(Px, #position{long = Long, short = Short}) ->
Pnl = [ Px - U#underlying.px || U <- Long#side.underlyings ] ++
[-Px + U#underlying.px || U <- Short#side.underlyings ] ++
[-C#option.px || C <- Long#side.calls, Px =< C#option.strike ] ++
[ C#option.px || C <- Short#side.calls, Px =< C#option.strike ] ++
[-P#option.px || P <- Long#side.puts, Px >= P#option.strike ] ++
[ P#option.px || P <- Short#side.puts, Px >= P#option.strike ] ++
[ Px - C#option.strike - C#option.px || C <- Long#side.calls, Px > C#option.strike ] ++
[-Px + C#option.strike + C#option.px || C <- Short#side.calls, Px > C#option.strike ] ++
[ Px - P#option.strike + P#option.px || P <- Short#side.puts, Px < P#option.strike ] ++
[-Px + P#option.strike - P#option.px || P <- Long#side.puts, Px < P#option.strike ],
lists:sum(Pnl).
Out of or at the money
pnl(Px, #position{long = Long, short = Short}) ->
Pnl = [ Px - U#underlying.px || U <- Long#side.underlyings ] ++
[-Px + U#underlying.px || U <- Short#side.underlyings ] ++
[-C#option.px || C <- Long#side.calls, Px =< C#option.strike ] ++
[ C#option.px || C <- Short#side.calls, Px =< C#option.strike ] ++
[-P#option.px || P <- Long#side.puts, Px >= P#option.strike ] ++
[ P#option.px || P <- Short#side.puts, Px >= P#option.strike ] ++
[ Px - C#option.strike - C#option.px || C <- Long#side.calls, Px > C#option.strike ] ++
[-Px + C#option.strike + C#option.px || C <- Short#side.calls, Px > C#option.strike ] ++
[ Px - P#option.strike + P#option.px || P <- Short#side.puts, Px < P#option.strike ] ++
[-Px + P#option.strike - P#option.px || P <- Long#side.puts, Px < P#option.strike ],
lists:sum(Pnl).
In the money options
pnl(Px, #position{long = Long, short = Short}) ->
Pnl = [ Px - U#underlying.px || U <- Long#side.underlyings ] ++
[-Px + U#underlying.px || U <- Short#side.underlyings ] ++
[-C#option.px || C <- Long#side.calls, Px =< C#option.strike ] ++
[ C#option.px || C <- Short#side.calls, Px =< C#option.strike ] ++
[-P#option.px || P <- Long#side.puts, Px >= P#option.strike ] ++
[ P#option.px || P <- Short#side.puts, Px >= P#option.strike ] ++
[ Px - C#option.strike - C#option.px || C <- Long#side.calls, Px > C#option.strike ] ++
[-Px + C#option.strike + C#option.px || C <- Short#side.calls, Px > C#option.strike ] ++
[ Px - P#option.strike + P#option.px || P <- Short#side.puts, Px < P#option.strike ] ++
[-Px + P#option.strike - P#option.px || P <- Long#side.puts, Px < P#option.strike ],
lists:sum(Pnl).
Tuesday, March 10, 2009
Erlang at SpeakerConf
Aslak Hellesoy, Amanda Laucher, Dave Hoover, Dennis Byrne, Fred George, George Malamidis, Jay Fields, Josh Graham, Justin Gehtland, Michael Feathers, Mike Roberts, Obie Fernandez, Philippe Hanrigou, Stuart Halloway and Venkat Subramaniam.
