Search This Blog

Tuesday, December 7, 2010

Brokerage Account Choices

There are a lot of things to consider when choosing what brokerages to use. I use 2 - Scottrade and Interactive Brokers. Here are some of my reasons.

Interactive Brokers caters more to serious traders. You must deposit at least $10,000 to open an account. They have some really nice features, though. For example, if you want to create your own custom trading software, they have an API that you can use to interface your software to their server. They will also set up a paper trading account for you for free.  They also have a lot of data you can download, some free, some not. Some of the free data you can download is 6 months of historical stock, option, and futures prices and volumes at up to 1 second intervals. They have a lot of advanced data that can be streamed like market depth. They also have decent commission rates. For option contracts, they have the lowest prices I have seen vs all of the other retail brokers I have looked at. For stocks, they charge $0.05/share, with a $1 minimum, which is really inexpensive until you start buying thousands of a single share at once.

Scottrade would be a good brokerage to use if you don't have much capital, since they will let you open an account with as little as $500. They don't have the bells and whistles though, like being able to download historical price data, and they don't have an API. In comparison to the basic retail brokerages, their commissions are better than most but nothing to get excited about, unless you want to submit a large stock order. They charge $7/stock trade for stocks over $1, and there is not a limit on the size of the order, so you could buy, for example, 15,000 shares of BAC and only pay $7 in commissions. I don't know any other broker that can get close to that.  If you want to try Scottrade, they also give out free trades sometimes. If you want to try them out use the code 'AINA4998' when you set up your account and they will give you 3 free trades.

Personally, I trade stocks and options. Whenever I use margin, I will always trade with Interactive Brokers because their rates are a lot lower. Likewise, they will always get my options business because they are a lot cheaper. However, whenever I buy more than about 2000 shares at a time, I will be using Scottrade. I hope this helps someone. 

Wednesday, November 3, 2010

IbMatlab API Functions

Lately I have been working on writing some matlab functions to connect Matlab and Interactive Brokers via their ActiveX API. Recently I found someone had written some very nice code and posted it at leptokurtosis. You have to create a login to download the code, but that takes about 30 seconds. It is called IbMatlab, and the most recent version he has posted is 0.0.4. The guy that wrote it says that it is rough, but after looking at his code, I would say he is no novice. I just started using his functions a couple days ago, and would be interested to hear what other people think of them.

Wednesday, August 18, 2010

Matlab IB Historical Data

Hello again. Today I wanted to talk about how to use Matlab to connect to Interactive Brokers via their IB API, and then download historical data. The setup for this is pretty straight-forward, and I will talk about that in a minute, but first a little bit about setting up an account. If you have $10,000 just sitting around, use it to fund a cash account at IB. There are plenty of perks that come with it. If you don't have that kind of cash, but are a high school or college student or professor, set up a free paper-trading account through their Student Trading Lab program. This gives you full access to their trading platforms, API platforms, and historical data. These are some incredible perks which normally are not accessible to the general public until they set up an account and fund it with at least $10K. If you don't have the cash, and you aren't in school, sorry but you won't be able to actually connect to IB. Don't let that stop you from learning what to do when you do have the cash though.

Let's get down to business. First of all, I just want to let everyone know that I am running Windows XP, and I will be using ActiveX controls, which only work in Windows. If you are using a different OS, they do have other ways to connect to their API, but I haven't used them yet. I may look into them at some point, but right that it's not very high on my priority list. With that said, here are the instructions for connecting Matlab to IB:

1. Make sure Matlab is installed on your machine. No toolboxes are required, just Matlab.

2. Download and install Trader Work Station (TWS) and IB API from Interactive Broker's web site.

3. Check to make sure that ActiveX is installed on your machine. This is the method we will be using to communicate between Matlab and the IB software.

4. Login to TWS and click on the Configuration Menu. In the API tab there should be an option to allow ActiveX and Socket Clients. This box needs to be checked.

5. Logout of TWS and restart your computer.

6. Login to TWS, and start Matlab.

7. Download this set of functions and place them in your current matlab directory. The demo file uses all of the functions to connect to TWS through the IB API, download some historical data for a single stock, and then disconnect. The data should will be stored in a struct called HistData. Here is a brief description of each of the functions:

tws_Connect.m - Creates an invisible figure, places an ActiveX component in it, and uses the ActiveX component to create a connection to TWS via the API.
tws_Disconnect.m - Closes the connection between Matlab and TWS.
tws_CreateRequestID.m - Uses the name of the security you want to request historical data for and creates a Request ID.
tws_CreateContract.m - Uses the information you specify about the stock of interest to create an ActiveX object used in the data request.
tws_RequestHistory.m - Sends the request to TWS to download the specified historical data, recieves, the data, and stores it in a convenient format.
tws_Event.m - You will never call this function directly, but it is very important. It coordinates the event handling.
tws_conn_hdata_demo.m - script that demos all of the above functions.

That's it! Now you can use the demo script as a starting point to write your own script that meets your needs. Notice I didn't save the data anywhere. I'll leave that up to you. My last post was on how to connect Matlab to a MySQL database, and I think my next post will be how to create tables in MySQL and store downloaded data in those tables using Matlab. I hope you enjoy this. As always, let me know if you have any suggestions on how to improve my methods. I am always interested in learning new tricks.

Tuesday, August 17, 2010

Matlab and MySQL

Today I wanted to talk about how to configure MySQL and Matlab Database Toolbox so you can access an SQL server directly from Matlab. I am posting these instructions because when I was doing this I had to look through about 40 web sites to find the information I needed to configure the Database Toolbox because Matlab didn't feel like writing decent documentation. Just to let everyone know, I mainly use Windows, so these instructions may not be very useful if you are interested in using another OS, but I hope they help.

1. Install Matlab, including the Database toolbox on your computer.

2. Install MySQL on your computer. The latest release can be downloaded from If you are unfamiliar with MySQL, install using the default settngs. When you are prompted to set a passoword, this is for the root (administrator) account.  You can use the root account as your primary login for MySQL, which will give you every possible privilige.  However, it is generally considered good practice to only use the root account for administrative purposes, and to set up and use a separate login with limited privileges for everyday use.

3. Make sure Java is installed on your computer, as this is how Matlab will communicate with MySQL. If it is not installed, it can be downloaded from

4. Download the MySQL Connector/J from This is the MySQL Java driver. The driver can be stored anywhere you want, but I recommend navigating to the Matlab installation files, and placing the driver in the Java directory. On my computer, the file is located in the following location:  C:\Program Files\MATLAB\R2009a\java\mysql-connector-java-5.1.12-bin.jar.

5. Associate the driver with Matlab. This is done by adding the location of the driver to the classpath file. To do this, in the Matlab command line type 'edit classpath.txt'. On the classpath file, type the following on a separate line: '$matlabroot/java/mysql-connector-java-5.1.12-bin.jar'. $matlabroot is Matlab's way of abbreviating the folder that matlab is installed in, so for example, on my computer, $matlabroot is the same as C:\Program Files\MATLAB\R2009a\. Using $matlabroot is better than typing the entire path because Windows allows spaces in path names, but sometimes they can cause problems in Matlab. If you are installing a different version of the driver than I am, remember to put the correct name of the driver in the classpath file.

6. Restart your computer.

7. If you do not have a database setup in MySQL, create a dummy database and table for testing your connection. The following commands can be typed into the MySQL command line to make the dummy database and variable:

     create database testdb;
     create table testtbl ( var1 varchar(8),var2 int);
     insert into testtbl VALUES ( 'hello',7);

8. In the Matlab command line, type 'confds'. This command brings up a window that will allow you to configure a data source. Select the 'Create New File...' option and name the configuration file that will store the connection information, then click 'OK'. In the Name field, designate the name you want for the data source. In the driver field, type 'com.mysql.jdbc.Driver'. In the URL field, type 'jdbc:mysql://localhost/testdb'. Click on 'Add/Update'. If you have a database that is located on a different computer, replace 'localhost' in the URL field with the location of the database.  I have only used a database located on the same machine as the Matlab installation, so If anyone can verify this last bit about connecting to a database located on a different computer I would appreciate the feed back.

9. Test the connection by clicking 'Test'.  You will be prompted to type the username and password. If you are logging in using the root account, use 'root' as username, and whatever password you set.

10. Open the Visual Query Builder by typing 'querybuilder' into the Matlab command line. If the name of your database does not show up in your in the data sources, click Query -> Load, and load the configuration file. You may be asked to enter your username and password for the database again.  From here you can play around with VQB and see how it works.

Now that the connection has been set up between Matlab and MySQL, you can either use the VQB, or use the database functions. I recommend either using the database function, or building your own GUI that does what you want it do do, as the VGB GUI has a number of limitations. I will probably post more later on how define tables in the database and exactly how to use the database toolbox to import/export data to/from  the database. Good luck!