Real-time trading system demo

In May 23, 2013 I gave a presentation at the MATLAB Computational Finance Conference in New York. The room was packed-full with close to 200 professionals in the finance industry. The energy and feedback were tremendous, it was a great experience. If you came to the conference, thank you for being a great audience.

In September 19, 2013 I gave a variation of that presentation at the MATLAB Computational Finance Virtual Conference. The presentation (PDF format) is provided here, the video recording is available here.

In both cases I presented a demo application that showed how Matlab can be used to create a full end-to-end trading system, highlighting Matlab’s potential as a platform of choice. I used Interactive Brokers to demonstrate live market data feed and account/portfolio input, as well as for sending trading orders to the market, via the IB-Matlab connector:

Real-time trading system demo in Matlab (click for details)

The trading algorithm used in the demo is trivially simplistic (random). In a real-life system you would naturally replace it with your own proprietary algorithm. But feel free to use this demo as a starting point for your application.

The demo source code is provided here (tradingDemo.m and supporting files). Note that this is provided as-is, free of charge but without any warranty or support. You would naturally need IB-Matlab and an Interactive Brokers account to run it.

IB-Matlab connectivity (Matlab and TWS may be on separate computers)

I hope we have a chance to work together on your projects. Send me an email if you would like my help in any consulting, training or development work.

8 Responses to Real-time trading system demo

  1. Kenan says:

    I have tried the Activex route before purchasing the product. There is one major fundamental flaw when it comes to using ActiveX with Matlab. Say,you are running an algorithm and you are processing a function, and at the same time TWS fires an Event. If you use ActiveX, MATLAB will NOT update the price until the processing of your function has completed. So several events will be missed and the price you would be looking would be a different one. Whereas in JAVA , there is no such problem. As any event fired will be immediately captured by java which is running in background. So when you call getLastPrice, you will get the correct price. Another flaw is obviously the fact that you can use ActiveX ONLY with WINDOWS. Whereas with JAVA you can use it with windows, Mac, Linux etc.

    It is NOT a good idea to stream in Live Trades data as it comes in into MATLAB. Imagine, you have 100 symbols, which updates every say 200 msec, so you have a trade happening so quickly and being captured and stored into Matlab. Due to MATLAB’s single-threaded issue, some Trades ticks will be missed and also will eat up your memory. So all that you will be able to do is just to stream in data and not do anything else.

    • @Kenan – indeed, the Java API (which is used by IB-Matlab) has many advantages over the ActiveX API (which is used by MathWorks’ Trading Toolbox). One of the fortunate results of using Java is that IB-Matlab can run on all platforms that run Matlab (Windows, Mac, Linux), since all these platforms have both Java and an IB TWS client. The Java API is also much faster and more reliable (the ActiveX connector is reported to be dropping IB events every now and then).

      Regarding streaming-quote latency, this depends on the security volatility, number of monitored securities, network bandwidth, computer hardware, other running processes on the computer and a wide range of other aspects that can affect performance. On a standard Lenovo Thinkpad E530 laptop running Matlab R2013a on Win7, I reached streaming quote latency as low as 1-2 mSec (i.e., hundreds of IB events per second). Naturally, YMMV.

  2. Marco Ruijken says:

    Hi Yair,

    downloaded your tradingdemo app, but get errors in Matlab R2014A. Is there a newer version, or should it run with a different Matlab version only?


    • @Marco – the demo works with R2013a and R2013b.
      Matlab R2014a made some internal changes that cause errors, and I have not taken the time to update the demo to that version. after all, the demo was developed for the conferences in 2013…

  3. Leonel says:

    Hi Yair,
    Could you please contact me, I have a question regarding a similar application



  4. Mike says:

    Does your program work with excel rtd as a realtime data source? Where can I see the program running live?


  5. For all those who are interested, I have updated the files and they are now compatible with all Matlab releases up to the latest (currently R2019a):

Leave a Reply

Your email address will not be published. Required fields are marked *