Nov 8, 2011

Trading with IronRuby

Over the last year I've been using Ruby (more specifically IronRuby) to write algorithmic trading scripts. These aren't high-frequency algorithms, more implementations of various longer-term strategies using Ruby.

There was poll over in an algorithmic trading forum on EliteTrader asking about what was the programming language of the future among Java, Scala, C#, F#, C++ and OCaml. A number of other people posted about other languages such as Python or Lisp, and how well they satisfied four criteria:
  1. memory management (GC vs unmanaged)

  2. concurrency model

  3. static typing vs duck typing (or better yet, type inference)

  4. object-oriented programming vs functional programming
I wrote a post on there about my experiences using IronRuby for trading, so I decided I would share that here as well:


I've been using IronRuby for the last year, I've found it works very well as I'm able to go from idea to a working script in no time flat. Performance is not amazing, but the majority of my ideas do not require performance.

For the evaluation (note that pretty much every point also applies to JRuby, the implementation of the language within the Java virtual machine):

1. memory management (GC vs unmanaged)

Uses .NET garbage collector, which is pretty solid. It tends to use a bit more memory than the equivalent VB.NET or C# script, but that's alright. RAM is cheaper than my time.

2. concurrency model

Again, uses .NET threads which are fairly solid. You can use any of the classes in the .NET library for concurrency.

3. static typing vs duck typing (or better yet, type inference)

Ruby uses duck typing. Mixins (aka traits in Scala) means you can write code in chunks and mix-and-match them per script. For example, you could have a SpreadTrade mixin that you just drop into a script and it will make the script do spread trading.

There is a bit of clunkiness when interacting between C#/VB.NET code and IronRuby code due to type conversions and what-not - since Ruby collections can contain an arbitrary mix of types, collections are always treated as collections of Object when passing into C# code.

4. object-oriented programming vs functional programming

Ruby has a very nice blend of both. Absolutely everything is an object (including classes and primitives like integers), existing classes are open (you can add methods to existing classes), and closures passed to methods have a rather succinct syntax. For example, it is possible to build a library where the following code is valid:

symbol.changes_by 2.5.percent do
# now within a closure with some code to execute when the stock changes by 2.5%
end


At the same time, you still have all your nice functional programming techniques:

# higher order functions:
(1..5).map { |i| i * 2 } # produces: [2, 4, 6, 8, 10]
(1..100).select &:odd? # gets all the odd numbers from 1 to 100
(1..100).inject &:+ # produces the sum of 1 to 100

# currying
f = lambda { |a, b| a + b }
g = f.curry[1]
g[2] # produces 3


Two new criteria that could be added:

5. Meta-programming. In Ruby not only are things dynamically typed, but types themselves are dynamic. Rather than manually coding something every time, I can write one line within a class that will automatically generate the code that I want:


class SomeOrder
# this code will automatically generate code to cause orders of type SomeOrder to be
# hedged by an object of SomeOtherOrder
hedged_by SomeOtherOrder

# generate code that will cancel the order on some condition
cancel_if {
# market is tanking
}

# etc.
end


This allows you to write code in a more declarative way, which can lead to less bugs as there are less moving parts you have to manually specify each time.

6. Readability: As demonstrated in the examples above, I can show my scripts to non-programming traders and they are able to follow the strategy fairly easily. They may not understand the meaning of the punctuation, but the flow of the logic can be set up in a way similar to the way they might describe the strategy in English.

Some downsides:
- Like I said, performance is not amazing, it's on par with Python and the other scripting languages.
- When coming from static languages it is inevitable you will get frustrated the first time you stub your toe on the dynamic typing system - you can get some weird bugs when you accidentally pass in a string instead of an array or something like that.
- Those nice declarative meta-programming things can be rather tricky to implement within a library.
- Finally, the language assumes that everybody knows what they're doing. It is possible for some people to write very bad code in Ruby that messes things up - for example, overriding the + operator for arrays to implement vector addition when the standard library has it defined as the array concatenation operator. Imagine your surprise when [1, 2] + [3, 4] produces [4, 6] instead of the expected [1, 2, 3, 4].


Out of the readers who are also Rubyists, what do you guys think about this topic?

2 comments:

Romain said...

Not an answer but a question :) :

Do you have any recommendations on getting started with automated trading?
For instance, introductory texts, frameworks / libraries that blend well with JRuby... from the perspective of a complete beginner.

Cheers!

rob said...

I'm not sure of many texts on automated trading, it is a very secretive industry since once lots of people know about a certain technique it tends to lose its effectiveness. I did read this book on automated trading, but it only talks about very basic ideas, you still have to use your own creativity to come up with trading strategies. It also gives the examples in MATLAB, not JRuby.

On JRuby specifically, you can probably use it for any platform that has a Java API. I know that Interactive Brokers has a Java API, so I doubt it would be terribly difficult to write a little add-on in Java that uses the IB API but loads in a JRuby script that manages the trading logic. I don't know for sure since I haven't used IB specifically, but that's basically what I do for IronRuby and .NET.