Discussion in 'Trading Software' started by flakac, Mar 27, 2004.

  1. flakac


    Is here anybody using Matlab for trading? (Market analysis, etc. etc..) I am doing so, I use database toolbox for accessing my stock database, datafeed toolbox (a rather buggy), etc.

    My problem is I haven't any api for instant access of data provider and broker (IB)?

    Any idea?

  2. write one.
  3. I guess you haven't been looking to hard. Here is all you need except some socket programming skills:

    If you understand how to program using the IB TWS APIs, then you're all set to go.
    I would also recommend playing around with the Adaptive Neural Network - ANN - especially MLP which is suited for time series:

    I guess you also know the NN toolbox etc.

    Happy programming - and you can publish any API on Mathworks site and IB discussion forums, if you feel like it.
  4. I do not suggest a Methlab for trading. I hear the chemicals are rather volatile and the criminal penalties are severe.

    Besides, you should be tweaking your trading skills, not your brain.
  5. prophet


    Implementing a Matlab to TWS connection using native-Matlab sockets code will be slow (much overhead), unless you really know what you are doing, or you only require real time data for a few symbols.

    I recommend using Matlab + MEX code for prototyping, simulation and backtesting. Use Matlab for high level and matrix operations, MEX for non-vector-based or performance critical routines. Then compile or convert your M code to C or C++ code, write some wrapper code, link it in with TwsSocketClient.lib, then trade it. Much less CPU overhead. Much more scalable.

    There is also a package soon to available (for purchase) called “Trading with Matlab” that is some kind of hybrid between these two solutions (high performance + native Matlab code), and includes charting and analysis tools. Give me a chance to look it up and post the info.

    I recommend against starting with ANNs. There are simpler and more robust networks and curve fitting methods available that are easier to understand, faster to fit, with guaranteed convergence. ANNs are slow and have convergence issues. Move to a more sophisticated adaptive network once you demonstrate that simpler networks are inadequate. Otherwise you will be wasting CPU cycles on fitting a sophisticated network when you could instead be testing 10, 100 or 1000 times as many simple network configurations. You'll also avoid the "black box" headaches that stem from not understand what your network is learning.
  6. The performance issue is very true, but for starters it's good because you get an idea of what you can do with your chosen platform.

    It also depends on programming experience and commitment. There are some posts on the IB discussion board about Matlab, but as far I can remember there is no sourcecode posted to look at.

    Yes, there are better solutions, but free ? :)
    Playing around with free and perhaps not the best solutions/tools still makes for good experience - and being able to make decisions about committing to a more advanced commercial tool.

    But all your arguments are true, and also repeated on Matlab discussion board. I just think that trying out stuff for free gives nice a nice experience before getting more serious about developing a proprietary platform - which costs time&money.
    It seems that anyone going for IB+Matlab might be interested in doing things on a lower budget. I haven't setup IB TWS with my Matlab 6.5.1 myself yet, but will do when I get some time.

    I am mostly interested in fooling around with NN MLP stuff. Have you done NNs in Matlab with regards to timeseries ? Curvefitting prices is rather futile, but getting indications on price direction is useful in my opinion. Getting a low RMSE for trading indications is the best way to train a successful NN I guess.
  7. prophet


    I agree. Once the socket interface is working in Matlab it can be rewritten in C. Matlab is much easier to prototype and debug than C. So it’s probably faster to start off with Matlab.

    It’s probably because most people keep their source code proprietary. I’m an good example. I have several megabytes of C and Matlab code, 16 production system designs, 100+ developmental systems, and 5 systems trading ES, NQ and 6E fully mechanically, all the time.

    I have tried to share ideas and code repeatedly in the past, simply for the satisfaction of helping others and for figuring out new ideas. Some collaborations are great. Some leave me feeling empty. Many would say I spent too much time collaborating and sharing and not enough time focusing on improving what I had started. Now it is getting more difficult to collaborate or share ideas. My testing architecture has grown specialized (for extreme performance) and sophisticated enough that it’s hard to explain and justify to others, and hard to modify to do other things. It’s not fit for distribution. I suspect most other real time Matlab trading interfaces are the same…. very proprietary, specialized, sophisticated and profitable.

    Free alternatives to Matlab… there’s GNU Octave, and lots of open source / free math libraries that provide many of the advantages.

    Just checked Ebay… Matlab 6.5 Student Edition (non commercial use) is selling for around $100. Matlab 4 is sometimes available for $10. Older versions are quite useful. I used version 5.2 up until mid 2002.

    In my opinion, Matlab is more than worth it’s price. The only tools I rely on are Matlab, Xemacs, Cygwin and MS Visual C++ (in Win XP and 2K). Linux + GCC would substitute fine.

    I’ve done some simple backpropagation NNs, both with the Matlab toolkit and with my own code, along with some exotic home-brew networks based on analyzing the singular value decomposition (SVD) products. Any kind of adaptive model for forecasting of prices is difficult, even when the model is fit across thousands of stocks and thousands of days.

    I never base my network models on price alone. I usually rely on system performance statistics or inter-stock money flows. I keep the networks very simple, constraining or structuring them as to specify a hypothesis. The network then figures out the details
  8. flakac


    I am not good at low level programming.. that is the problem with IB API...

    On the other hand, Matlab is good for indicator building. (I fond several bugs in TA indicators in financial time series toolbox)

    I thing Matlab scripting language is much simpler and does offer more convenience then scripting language of other programs (Amibroker, WealthLab, Metastock). /This is my opinion only...

    Matlab NN are slow and not so good as NN from other programs(STATISTICA). I have no problems building NNs, however, I recommend using the fuzzy logic toolbox instead (The ANFIS is great!)

    If you want excellent NNs, try SAS Enterprise Miner (but the price....) I know no better tool.

    I use Matlab with MySQL database where I store the EOD data, the results of my computation, etc. The problem with proprietary software (Metastock, Wealth-Lab, Amibroker) is, they use proprietary formats for data storage. I want to "see" into the database, what is stored in.., etc.

    I am not sure weather a MySQL is a good choice, it is a beginning, maybe Oracle could be better in the future.

    Free Octave isn't alternative . No object, no fuzzy, no NNs, as I know...

  9. prophet


    One option is to pass your data to a MEX function or save to a data file, then run it through a higher performance toolkit.

    Don’t they provide tools to export to other formats? Surely someone has figured out the formats.

    Perhaps instead of using the built-in Matlab toolkits (closed source), you could find some open source code at It should be easy to adapt these sources to older Matlab versions or Octave.

    I don’t know much about Octave…. but lack of OO support shouldn’t be a problem starting out, since your code will be mainly process oriented.
    #10     Mar 28, 2004