Skip to content
All library documents

When MATLAB Code Generation Fits Live Trading Workloads

Article Quant Q&A · Author: Dmitri Nesteruk

Summary

The document considers whether MATLAB-generated code can meet live trading speed requirements. Its central guidance is that suitability depends on the strategy’s trading frequency, holding period, model complexity, and surrounding system. The responses cite use of Excel and R in lower-frequency workflows, and describe MATLAB code generation and compiled implementations as possible production approaches. Hardware-specific deployment, including GPU use, is presented as potentially useful for computationally intensive models such as Monte Carlo value-at-risk calculations.

The discussion also gives a counterexample: one user reports poor performance with MATLAB’s Java Builder for numerical optimization, while a native Java library ran the task much faster. These accounts are anecdotal and do not establish general benchmark results. Generated or compiled code therefore needs workload-specific measurement, including integration overhead and deployment constraints. The answer suggests that routing work to secondary hardware may be unsuitable for very simple ultra-high-frequency models, while slower strategies may not need such optimization at all.

Key ideas

  • Live trading suitability depends on latency needs, holding period, model workload, and system design.
  • MATLAB code generation can target specialized hardware for some computationally intensive models.
  • Compilation and code generation do not guarantee faster performance for every task.
  • One reported Java Builder optimization ran much slower than a native Java alternative.
  • Benchmark the complete deployment path against the requirements of the strategy.

Tags

Full text
# Is MATLAB-generated code good enough for use in live trading?


# Is MATLAB-generated code good enough for use in live trading?












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?

## Answer by Matt Wolf (score 13, accepted)

https://quant.stackexchange.com/a/4797

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.

## Answer by Christoph Glur (score 4)

https://quant.stackexchange.com/a/4802

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 ;-)

## Answer by Edmondo (score 1)

https://quant.stackexchange.com/a/4807

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

Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.