Quantitative Finance Stack Exchange is a question and answer site for finance professionals and academics. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I know that MATLAB has mechanisms for generating code, but I've never used them. Have you? If you have - is it good enough (=fast enough, I guess) to be used in live trading systems? Anything one needs to watch out for?

share|improve this question
This kind of question get asked all the time. The only answer to this question is for you to test your application. – chrisaycock Dec 17 '12 at 21:22
up vote 13 down vote accepted

Of course it is fast enough. But what is fast enough? I know guys who trade off Excel sheets and they make millions, but those guys are clearly not active in high frequency space. So, it entirely depends on your trading frequency and average holding period. I also know of shops that run live trading systems by calling R functions, so, obviously Matlab generated code is fast enough for a lot of algorithms. Chrisaycock is right in saying that it depends mostly on what you actually try to achieve and your own framework.

share|improve this answer

If I understand you right, you are talking specifically about Matlab's embedded code generation facility (see here: http://www.mathworks.ch/embedded-code-generation/). In my view, the answer to your question is clearly yes. This feature allows you to generate hardware specific code, e.g. for deployment on GPU's (video cards). It's used for aerospace systems, among other. In our area of expertise, this is probably as fast as it gets today, at least for some types of models. As a rule of thumb: the more complex your model, the more you get a competitive advantage with this technology. In my opinion, this favors mid- to high-frequency strategies (ultra-HF models are usually much simpler, so the overhead of routing calls to secondary hardware is usually too slow; for slow models, it's not worth the trouble because you have enough time to run it on a well-equipped desktop). A typical example where this would really pay off is a monte carlo simulation to calculate a VaR for the risk-sizing of an intraday trading strategy. In my view, even without code generation, Matlab is also a very robust and fast tool for production use. For example, you can compile code and, if well done, this is much faster then R. In fact, the company I work for does technology and trading strategy implementation for quant hedge funds, and Matlab is one of the technologies we use very often. The code generation, on the other hand, is still seen as leading edge by many. Thus, the time might still be right to gain a comparative advantage by using it ;-)

share|improve this answer
Not sure I would call the code generation in Matlab "leading edge", it can by definition (due to the overhead that comes with it) not be faster than pure code written in C or C++, even C# in many cases beats Matlab generated code, if not written under the influence of alcohol. But it surely is very convenient and speeds up development. – Matt Wolf Dec 19 '12 at 0:53
Of course, if you generate code to C, then it won't be faster than C. Also, some functions may even be faster in native Matlab (see here for details: blogs.mathworks.com/loren/2011/11/14/…). However, our experience shows that with a smart strategy using MEX for bottlenecks, your overall performance will often beat both native C and Matlab. But, as I said, for many applications it's not worth the trouble. And, if you have good C skills, you might as well develop directly in C. – Christoph Glur Dec 19 '12 at 9:43
...If you go full throttle I would say its hard to beat native C++ code, in the end Matlab is not doing a whole lot more than scripting code and calls a compiler. Matlab is not the only language/environment which can perform vector and matrix calculations. Plus, in the end Matlab just adds another layer on top of the code base it is forced to use to access GPUs, for instance. So, it can come close to C or C++ but I do not see a single argument where one may claim it beats native code bases. But it kind of moves away from the core discussion. – Matt Wolf Dec 19 '12 at 9:52

We were using Matlab with the Java Builder for numerical optimization and the results were very poor. Fmincon was taking about 30 seconds to converge, now we are using a native java library and the optimization takes from 0.1sec to 0.5 seconds

share|improve this answer
Depending on the structure of your optimisation problem, you might be able to generate code for a custom solver, which should do the trick much faster than Matlab's general-purpose fmincon. – Jubobs Aug 9 '13 at 12:54
sounds very interesting, can you please detail your comment or give some links? thank you very much – Edmondo1984 Aug 12 '13 at 6:47
This article should be a good start. – Jubobs Aug 12 '13 at 9:32

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.