There are many, many brokers that provide an API that will allow you to buy or sell stocks via python, java, or whatever platform you’d like. The tutorial below will set you up with Interactive Brokers, which has very low (I believe the lowest) transaction costs, though they require at least 10k to open an account. Also, be aware, you need to have 25k in your account to execute more than 10 trades a week, which may matter depending on the way you execute your strategy.
The tutorial below is using one of the free micro amazon ec2 ubuntu instances. I like those because it’s always on and I don’t have to worry about my router dying or other operational events. Please keep in mind, this approach is one of many possible solutions, and this particular implementation has no hope of competing with anything resembling High Frequency Trading. However, it would be a very effective as part of a larger trading application built on your own pricing theories.
If you are unfamiliar with setting up an EC2 instance, this guy does a decent explanation of setting one up in a few minutes, but be sure to pick a newer LTS. I tested the instructions below on 10.10, but I’m sure a newer one would be fine.
Go ahead and set that up. I’ll wait. Now, once you’ve ssh’d into the machine..
How to Install TWS on Ubuntu 10.10
Install Java and ldxe ( a lightweight desktop env )
sudo apt-get install openjdk-6-jdk lxde
The login process to TWS wants you to use a GUI so that you can use their security process. You can avoid this depending on how you set up your account, but its just as easy to set up a VNC server on the EC2 instance, then vnc in, login to your (or the demo ) IB account, and then forget about it. Some people use FreeNX for this, which I think is *way* too fancy, but to each their own.
To get VNC working on your EC2 server, you must open up port 5901 on your EC2 security group (which you do via the command line tool or via the EC2 Management console).
sudo apt-get install vnc4server
Create a startup file:
############## #Land following code to : /home/ubuntu/.vnc/xstartup ############## ============================= #!/bin/sh
# Uncomment the following two lines for normal desktop: # unset SESSION_MANAGER # exec /etc/X11/xinit/xinitrc
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup [ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources xsetroot -solid grey vncconfig -iconic & x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" & x-window-manager &
To start up the vnc server ( run this on the ec2 server ):
vncserver :1 -geometry 1024x768 -depth 16
To kill the vnc instance:
vncserver -kill :1
Now download a VNC viewer client of your choice (like TightVNC) and connect with:
Ok, the set ups done.. now we can..
wget http://www.interactivebrokers.com/download/unixmacosx.jar jar xf unixmacosx.jar cd IBJts java -cp jts.jar:hsqldb.jar:jcommon-1.0.12.jar:jfreechart-1.0.9.jar:jhall.jar:other.jar:rss.jar -Xmx512M jclient.LoginFrame .
(To login for testing you can use the demo account. Again, you’ll need 10k to open a real account.)
user name: edemo
Go to Configuration -> API -> Settings. Check “Enable ActiveX and Socket Clients”
(exit if you like)
GUI-less ( low resource ) tws
java -cp jts.jar:hsqldb.jar:jcommon-1.0.12.jar:jhall.jar:other.jar:rss.jar -Dsun.java2d.noddraw=true -Xmx512M ibgateway.GWClient .
If you use the GUI-less tws (which is abig improvement for me, you must go into configure -> settings -> api -> configuration and change the port to 7496
############## # USE IB via Python ############## # http://code.google.com/p/ibpy/wiki/GettingStarted
wget http://ibpy.googlecode.com/files/IbPy-0.7.0-9.00.tar.gz tar xfz IbPy-0.7.0-9.00.tar.gz cd IbPy-0.7.0-9.00 sudo python setup.py install cd demo # Examples (when tws is running) ./reference_python python example_opt
The references provided in the IbPy package are very helpful, but if you are struggling, here is an example of how to place a market on open order for an equity:
from ib.ext.Contract import Contract from ib.ext.Order import Order from ib.opt import ibConnection, message
def makeStkContract(sym): contract = Contract() contract.m_symbol = sym contract.m_secType = 'STK' contract.m_exchange = 'SMART' contract.m_primaryExch = 'SMART' contract.m_currency = 'USD' contract.m_localSymbol = sym return contract
def makeStkOrder(shares,action): order = Order() order.m_minQty = shares order.m_orderType = 'MKT' order.m_tif = 'OPG' order.m_totalQuantity = shares order.m_action = str(action).upper() return order
IBcon = ibConnection() IBcon.connect() stkContract = makeStkContract(sym) stkOrder = makeStkOrder(shares, action) IBcon.placeOrder(orderID, stkContract, stkOrder) IBcon.disconnect()
Ok, you’re done! You should have a shiny new trading interface. Now, if you only had good data to figure out what you should trade..