views:

1939

answers:

10

I’ve never worked in a trading environment before and I was curious to see that few of the trading houses seem to use C#, but most of them do heavily rely on C++. Why is it?

  1. Is it because C++ is better performance wise?
  2. Is it because of legacy code bases?
  3. Is it because of cross-platform issues?
  4. What about dynamic languages (Ruby, Python)? Are they too slow for this kind of work in terms of performance?

Updated: If reliability and performance are important would "Erlang" be the "next big thing" in trading platforms?

+19  A: 

Because of Speed. C++ is way faster than C# and Java. So although it's harder to develop, but the sheer programming executing speed you gain translates vastly into trading profits.

Latency orders are important here. Every microsecond counts, especially in High Frequency trading. I doubt whether the JIT or JVM can do that well in matching C++ performance.

Ngu Soon Hui
Really? C# and Java are comparable these days.
RichAmberale
@RichAmberale: not when it starts GCing in the worst possible moment. So in other words, it's not the shier performance which counts, but predictability and consistency of performance
Rom
-1 for the "C++ is way faster than C# and Java". It's not that simple, and even if you are right in the rest of your post you are wrong to use such a misleading statement.
Ed Swangren
+1 I did worked in trading before and I know what kind of speed is required ..... they cannot compromise on speed
Xinus
@Ed Swangren: C# code runs on virtual machine and c++ is native code which has to be faster that c#. C# calls goes from virtual machine->operating system-CPU .. and for c++ there is no virtual machine
Xinus
Out of curiosity: if the argument is that microseconds count, why C++ over C? Seems like you could say the same thing for assembly... (A slippery slope, to be sure.)
Benjamin Oakes
@Benjamin Oaks If you can write it in C, you can write in C++. And if you need to write in assembly, you can use inline asm, SIMD intrinsics, or link to whole asm modules easily. Core routines in this kind of code are _very_ careful to avoid slow operations like new/malloc or exception throws in the innermost loops, so the lowest layers of stuff ends up looking like C anyway (assuming it's not actually written directly in asm, SSE intrinsics, or CUDA).
Jack Lloyd
@Benjamin: Because in C++ you *can* get the speed of C in places where it's really needed (by avoiding complex C++ features) and in C you *can* get the speed of assembly when you need it (at the extreme, this means inline assembly). In other words, C++ doesn't *force* layers of abstraction on you, but it gives you that option.
Artelius
If your trading application is running on Windows there are many other performance related issues such as disk swapping, multi-tasking etc that you should worry about equally as much as the perceived speed of your programming language.
Ash
I recently had an interview with a HF trading company, and they use predominantly Java with C++ where they need it (such as at the network layer).
James Black
@Ash Windows?!? Maybe for the trader displays. For the real work, think Linux, FreeBSD, or Solaris, CPU per major process, and mlockall(). Trust me, when there is $$$ at stake, people worry a lot about every conceivable aspect of performance.
Jack Lloyd
What about Erlang?
Jeffrey C
@Jack, I'm well aware that Windows is not used for the "real stuff". My point is that it is a mistake to worry about the programming language used before the platform, hardware etc in such a time critical environment.
Ash
@Jack, you're welcome to substitute *Windows* for any other non-realtime OS.
Ash
+3  A: 

Because that's what the systems are written in!

Sophisticated derivitives started being widely traded in the early to middle 1990s.

This was the same period that C++ was the next big thing. So the early trading systems and supporting analysis were naturally written in C++. Smalltalk, APL, SAS and even Lisp were also used, but C++ came to dominate.

The result is a large number of existing platforms, and domain libraries are written in C++ so it's just common sense to write the new stuff in C++ as well.

There is a similar situtation in retail banking where mainframe and COBOL were the hot technologies when the first systems were written, and most retail banks still use mainframe/COBOL extensively, even for new development.

James Anderson
There's a huge difference between banks and derivative traders, in terms of motivation. Just because banks shun/fear technology doesn't mean everyone in the financial sector does.
Tom
Its not a question of shunning technoligy - there is a critical mass of packages, tools, libraries, programmers and tools for doing this stuff in C++. If you switch to C# you have to build this all up from scratch.
James Anderson
Its not a question of shunning technoligy - there is a critical mass of packages, tools, libraries, programmers and tools for doing this stuff in C++. If you switch to C# you have to build this all up from scratch.
James Anderson
+4  A: 

Legacy issues aside, a lot of newer systems in this space are still being built with C++ for a few reasons, particularly performance concerns (the best optimizing compilers are still for C and C++, you can write inline asm easily, you can access SIMD instructions easily, etc) and the fact that people with quant or options experience typically have C++ experience from their last gig, so it becomes the default even in brand new from-scratch development projects.

Languages like Python, Ruby, R, etc are used a lot 'out on the fringes' (for administration tasks, automation, database manipulation, etc), but mostly the core systems of these shops are all C++.

Jack Lloyd
+6  A: 

I remember hearing a while back that writing real-time applications with garbage-collected languages was an issue; that might be part of it, as trading is pretty much real time.

Mathias
+1 Imagine this: you're trading a hot commodity, when suddenly garbage collection kicks in. At that moment, your latency spikes, and your algos take that extra mili second to move your position. Suddenly you lost 12 million dollars, because the position of the market moved too far away from your sell threshold. Whoops. Too bad. Call tech support.
Marcin
(BTW, I've heard these types tech support calls before. Let's just say they are very "inspiring".)
Marcin
So you just basically ensure that you won't need to do garbage collecting, by careful use of Java objects and perhaps having enough memory where you don't have to delete objects.
James Black
Marcin's right on the money. I worked on a trading system in the late 80s. One of the concerns was that the equipment be easily and quickly replaced. Somebody who just lost several million dollars on a trade was liable to pick up the nearest large, heavy movable object and hurl it in a random direction. But they needed to be back online asap; you didn't want them to miss their next opportunity just because they'd imploded their monitor against the wall.
Bob Murphy
Anybody who throws large and heavy objects in random directions is a danger to anybody around them, and needs to be restrained. If nothing else, they're a major legal liability waiting to happen. Throwing monitors into a wall could be reasonably safe, I suppose. So our financial system is run by people whose behavior I wouldn't find acceptable in a three-year-old? Somehow, that doesn't surprise me.
David Thornley
+4  A: 

I think you're not seeing a broad spectrum of these kinds of positions. C++, C# and Java are all common as are mixed environments. Sometimes even when C# or Java are used you'll find key parts done in C++. The key parts are:

  1. Market communication: you need the lowest latency possible; and
  2. Price calculation: most option pricing models use Black-Scholes (or some variant). You may need to reprice a lot of instruments many times a second. Speed is important for that.

Legacy code is another reason. But the need for C++ these days is only in small parts of the overall system.

cletus
+1  A: 

See this question regarding techniques used for space hardening. Real-time trading software is treated just as critically.

It's not just about execution speed, and speedups obtained by compiler optimization, it's also about reliability.

I suspect that's going to change a bit in the coming years. Java has made great strides in its stability and footprint. However, it's very likely that you'll be working on existing code, which is likely written in C, C++ or both.

Tim Post
I don't see it changing - while Java is making great strides, C++ compilers are continuing to improve as well. You *have* to eliminate garbage collection cost entirely, which means doing some fancy footwork at the code level in either Java or C#. It's possible, but certainly not easy, and not really related to general JVM improvements.
Tom
+1  A: 

John Lakos (of Large-Scale C++ Software Design fame) used to work for Bear Sterns; I think his book was rather influential. It showed how C++ could be used to build large applications. The "large" part is quite important, languages like C or Perl simply don't scale well past 100 developers. In fact, it's no coincidence that C++ came from AT&T. They used C for a number of their own projects (naturally, it too was a Bell Labs language) and Bjarne noted C's limitations in the real world.

MSalters
+1  A: 

I don't think this is actually the case. It just depends on the specific field of derivatives trading. The high frequency trading people probably (think they) need the performance of C++, and legacy systems could also be an issue in places. However, firms with a more technical/fundamental and more long-term approach are more interested in ease of development, and this is reflected in the languages used. I think I've read somewhere about a firm that was erlang only. I know there are a lot of places that use mostly SAS. There is a lot of java usage all round... anything goes really.

Also, a lot of the people doing the programming at trading firms -though probably very smart people- are not necessarily good programmers. Something as complicated as C++ might very well just be out of their league.

The lack of C# jobs might be because it is rather new. If all you see are C++ shops then your search job-strategy is biased.

jilles de wit
In high frequency trading if you quote prices to 3rd parties and calculate to slowly the other parties can arbitrage against your prices which would cause you to loose large amounts of money.
Tony Lambert
Indeed, so HFT people are going to need speed. But all other quantitative traders... not so much.
jilles de wit
Are you referring to this article "Are High-level Languages suitable for Robust Telecoms Software?*"? http://lambda-the-ultimate.org/node/1871
Jeffrey C
PDF version is here http://www.macs.hw.ac.uk/~dsg/telecoms/publications/SafeComp2005.pdf
Jeffrey C
I'm not referring to that paper (thanks for the link), but I agree with the idea behind it.
jilles de wit
+3  A: 

I have worked in the trading area off and on for 20+ years. As time has gone on C++ has been used less and less. I would say the bulk of C++ has been lost to Java and latterly C#. C++ is still used for pricing and risk calculations, this is because this is where most of the processing goes. In high frequency trading they need to turn round pricing in as short a time as is possible. In lower frequency exotic trading pricing and calculating risk just takes a large amount of processing power. It can require 100's or indeed 1000's of machines (known as a Grid). Overnight risk runs can take hours even with those resources. So here speed is also an issue but in a different way. I think in time C++ will be pushed from these areas too, but this will be slow to change as there are huge historic code bases that must work reliably and process known results so changing anything is a high risk process.

Tony Lambert
+22  A: 

The real reasons for using C++ over C# in trading houses aren't really touched in any of the other answers. Some are flat incorrect.

  1. The first, most important reason is that C++ has been around a far longer time than C#. That's much like COBOL. There's many millions of lines of COBOL still used in banks and government that was written in the 70's and 80's because the cost of totally rewriting it in C, C++, Java, or C# doesn't make sense. So, many companies keep legacy code around nearly forever. Most of the answers here came from "techies" (like you and I) who are rarely the decision makers. Unfortunately, it's non-techies, managers who decide these kinds of things and often for totally non-technical reasons. Ever read Dilbert?

  2. The second most important reason is the reliability of the Operating System. Traders need reliability even beyond speed. Windows Servers notoriously crash frequently, need regular rebooting, whereas the *nixes can run for many months without stopping. Many of these firms trade markets around the world so they dislike any unexpected downtime. Since the trading firms run all their systems on *nixes, they tend distrust Micro$. Yes, of course, you can use C# on Linux on Mono but that's not nearly as exciting for various reasons.

  3. Performance? I'm the author of a C# trading application that performs faster than most trading apps written in C++ and I did it in C#. How? Unlike Java, C# has an "unsafe" mode so you can do C++ style code with pointers for performance.. Plus you can use C++ style structs which is the most important aspect of performance. For critical code, you manage your own memory in C# just like C++ by preallocating so you never need garbage collection when running real time. C# can, in fact, even run faster that C++. It's been tested and proven. How? Why? Read on. Plus, it's really nice programming in C# over C++ for all the non-critical path parts of the code. Why? It's because of all the tools that are available and easily created like Unit Testing and others due to the "reflection" ability of C#.

You can only definitively say whether one is better than the other if you actually compare the machine code generated by both for the same piece of code and do micro benchmarks as comparisons.

CONCLUSION

The reasons for C++ over C# are business reasons. Interestingly my company has trading fund and bank customers using C# for trading system development based our platform, TickZoom which is also in C#. So why are they using C#? Again that's a business, not a techinical decision. These particular managers of these trading funds and banks are new-comers to the high frequency automated trading world. So they don't have any legacy code. Therefore, with a clean slate they can make different choices.

So how do they arrive at C#?

As a business decision, good C++ developers are very, very expensive. Plus the manager read reports from business mags who show that time-to-market of C# apps is generally faster than comparable projects in C++. So some businesses will tend to look at C# since it can perform and there's a larger developer pool.

NOTE: Some people experiment (like I have) with taking certain pieces of code and writing them in C++ as libraries for C# to use to get faster performance where needed. However, those efforts have always been proven that somebody can tweak C# to go equally fast so it becomes easier to just do it all in native C#.

Just to qualify this answer, I have worked for 25 years as a real time and network programmer using C, C++, Java (mostly web stuff), and now C# approximately 5 years with a few years in other languages like Assembler, COBOL, BASIC, Pascal, etc. My experience has been in communication networks, phone networks, banking systems for high speed imaging, high frequency trading, and more.

Having coded in C++ and C# for equally long periods of time, it would be painful to return to C++ because I feel forced to deal with low level issues in the language which allows for speed but create complexity in parts of the app where speed is of no concern. <-- That's the majority of the app, actually.

Plus, there a no tools really nice as JUnit or NUnit of PyUnit in C++ for automated unit testing. I can't even imagine programming the old slow poke way of code and fix without automated unit testing, mock objects, and such which are necessary for "agile" development.

What I do admire about C++ is how much money the programmers make are are strong in the system and real time programming fields! That's for sure. But I do alright with my own company now doing high performance C#.

Sincerely, Wayne

Wayne
Thanks for your comprehensive answer. Based on what you are saying C++ is essential for any trading related programming job, no wonder my job applications got rejected ^_^. I have been mainly doing business apps and only used C++ back in uni. I will have a look at TickZoom project.
Jeffrey C
What is your recommendations in getting into the trading related area? Like books to read etc? Thanks? As you mentioned about the difference between *nix and W*ndows, how likely would W*ndows crash when running your TickZoom?
Jeffrey C
and what are your thoughts on languages like Erlang?
Jeffrey C
Jeffrey. Glad to help.
Wayne
Jeffrey, get into programming for traders? I fell into programming for trading firms by accident since I spent about 15 years of actually trading myself, with my money on the side, and eventually built my own personal trading platform (which became TickZoom). All but my first year of programming, I worked as a consultant and took the next job that paid a good rate--whatever the industry. So I feel unqualified to give advice to the best approach to target a specific industry or even a specific company. Hey, I know it will help to actually live either in Chicago, New York, or London.
Wayne
Jeffrey, about TickZoom on Windows? Oh, we plan to run the backend, trade server and warehouse part of TickZoom on Linux shortly. The main reason for supporting Windows is surprisingly many of the mid-size trading funds started out small and still use the "retail" brokers rather than pay the high fees to trade directly with exchanges. Many funds won't even consider TickZoom until we either do support Linux or at least plan to do so soon.
Wayne
Erlang sounds cool technically. I just read the write up on it. All the critical features of Erlang can be done in C++ and are even built into TickZoom with C#, support distributed, fault-tolerant, soft-real-time, non-stop applications. Erlang might make it easier to code but mostly you'll find the decisions of firms focus on the business issues more than technical like speed of development amount of open source code available to use, pool and cost of developers knowledgable in the language.
Wayne
@Jeffry: Recruitment in finance seems to be much more competetive than other industries. It is also normal for financial institutions to heavily favour developers who already have FI experience.
Phil
Great answer Wayne. Many managers of these companies are non-technical and have to rely on outdated and prejudiced opinions about the performance of various languages. If they get this from business magazines and fellow managers, is it any wonder? You sum it all up perfectly with: "You can only definitively say whether one is better than the other if you actually compare the machine code generated by both for the same piece of code and do micro benchmarks as comparisons."
Ash
@Jeffrey, The way I read it, Wayne is not saying C++ is essential for *any* trading position at all. There are many legacy systems in C++ so it is an advantage there, but many of the "traditional" reasons for choosing C++ are just plain incorrect. C# is perfectly good enough.
Ash
@Wayne, there are unit test frameworks for C++. CppUnit is a port of JUnit. Another is googletest. "C# can, in fact, even run faster that C++. It's been tested and proven. How? Why? Read on." I'm interested...where's the proof?
dgnorton
C# gets JIT compiled to binary code just like C++. C# built-in libraries can be slow performance. But if you write raw C# code, it can be equally fast due to ability use pointers and low level code.
Wayne