- Undocumented Matlab - https://undocumentedmatlab.com -

Real-time trading system demo

Posted By Yair Altman On July 5, 2013 | 8 Comments

In May 23, 2013 I gave a presentation at the MATLAB Computational Finance Conference [1] 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 [2]. The presentation (PDF format) is provided here [3], the video recording is available here [4].

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 [5] to demonstrate live market data feed and account/portfolio input, as well as for sending trading orders to the market, via the IB-Matlab [6] connector:

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

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 [8] (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 [6] and an Interactive Brokers account to run it.

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

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


8 Comments (Open | Close)

8 Comments To "Real-time trading system demo"

#1 Comment By Kenan On August 31, 2013 @ 09:59

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.

#2 Comment By Yair Altman On September 4, 2013 @ 07:45

@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.

#3 Comment By Marco Ruijken On April 30, 2015 @ 03:48

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?


#4 Comment By Yair Altman On April 30, 2015 @ 05:22

@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…

#5 Comment By Leonel On July 18, 2017 @ 22:13

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



#6 Comment By Mike On August 24, 2017 @ 00:32

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


#7 Comment By Yair Altman On August 24, 2017 @ 10:25

Mike – My program works with IB-Matlab, which is a connector between Matlab and Interactive Brokers. It does not work with Excel RTD but connects directly to IB via their TWS API.
You can read more information on IB-Matlab and also request a trial here: [10]

#8 Comment By Yair Altman On February 8, 2019 @ 16:22

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

Article printed from Undocumented Matlab: https://undocumentedmatlab.com

URL to article: https://undocumentedmatlab.com/ib-matlab/real-time-trading-system-demo

URLs in this post:

[1] MATLAB Computational Finance Conference: http://www.mathworks.com/company/events/conferences/matlab-computational-finance-conference-nyc/2013/agenda.html

[2] MATLAB Computational Finance Virtual Conference: http://engage.vevent.com/index.jsp?eid=1972&seid=900

[3] here: http://undocumentedmatlab.com/files/Matlab%20real-time%20trading%20presentation.pdf

[4] here: https://www.youtube.com/watch?v=e4bdKcFl4GA

[5] Interactive Brokers: http://www.interactivebrokers.com/en/main.php

[6] IB-Matlab: http://undocumentedmatlab.com/ib-matlab/

[7] Image: http://undocumentedmatlab.com/images/demo_screenshot.png

[8] here: http://undocumentedmatlab.com/files/Matlab%20trading%20demo.zip

[9] an email: mailto: altmany @gmail.com?subject=Matlab assistance&body=Hi Yair, &cc=;&bcc=

[10] : https://undocumentedmatlab.com/ib-matlab

[11] : https://undocumentedmatlab.com/files/Matlab-trading-demo.zip

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.