Showing posts with label design. Show all posts
Showing posts with label design. Show all posts

Thursday, July 23, 2009

Proof Of Concept Recap and Game Plan

With Proof Of Concept postings completed, I now have a better understanding of what it will take to develop my trading robot. I have demonstrated that it’s possible to perform the following:


- Use JRuby to interface with TWS API

- Get market quote

- Get historical quote

- Check account information (balance and open position info)

- Check order status

- Submit stock order (market and limit orders)

- Submit options order

- Submit complex options order




Along the way, I have also identified some limitations and glitches within TWS API and JRuby. Most of these can be overcome, but they make development harder. Here are just a few:


- JRuby error messages are not that readable and it can be difficult to determine where the error is coming from. So during development, it’s important to test early and often before too much coding is done.

- TWS API or JRuby upgrades may cause the application to break. Any decision to perform an upgrade should not be taken lightly and complete regression testing will be needed.

- If TWS pops up a dialog window, any API calls will be interrupted until the window is closed. The finished product will need a way to detect or prevent this from happening.

- openOrder() callback method sometimes get triggered more than once for unknown reasons.

- Retrieved data (such as market price) from TWS isn’t perfect. So it’s a good idea to build in some sort of redundant requests to make sure no trading decision is made because of bad pricing data.

- There is a need to be able to recover in the case where order is submitted and TWS crashes. Part of the recovery process would be to detect what orders have been submitted, and executed or not.


It is my intent to develop the program in three phases, with the actual trading robot phase being the last, and most difficult to create. The first phase of development will be a watch list of my investment portfolio that will issue buy/sell signals according to my plan. There is no trading automation in this phase, as these investments not in my Interactive Brokers account. The second phase will be the backbone of the Trading Robot. It will keep track of which positions I have in my Interactive Brokers account and provide a user interface to manage data. The following is a more detailed description of each phase:

Phase 1) Watcher - Ruby on Rails application which allows me to watch my investment portfolio (composed of various ETFs and mutual funds). It will get security prices at regular time intervals or on demand and will issue buy/sell signals according to my rules (mostly based on moving averages). Data will be stored in a database and pricing information will be gathered from various sources on the web. It will provide a web interface from which to edit watch list data.




Phase 2) Account Monitor - Extension of the above Rails application. It will store open positions and trading rules in the database. It will provide a web interface to control application parameters and display activities performed by Trading Robot.




Phase 3) Trading Robot - Separate JRuby application with connection to TWS and database. It will populate the database with data from TWS, apply trading rules, and perform trades when needed. It will run in a batch mode and not have a user interface. All of the Trading Robot's activities will be monitored through the Account Monitor.

Sunday, January 4, 2009

Technologies

I use Interactive Brokers to do my trading because of their rock bottom commissions and speed of execution. Another plus with IB is that they make their proprietary application programming interface (API) available for free. These APIs are available in Excel DDE, Java, ActiveX, C++. Since I had done some work in Java a long time ago, I was interested in the Java API. Well, after looking at Java programming again, I'm not too enthusiastic about doing anything in Java. It's just too syntax intensive and complicated for a part time project.

Last year, I was introduced to Ruby as a programming language and was able to develop a couple of applications using one of its web application frameworks called Ruby On Rails. These small applications that I developed made me appreciate the beauty, simplicity, and power of Ruby language. So this is my programming language of choice for this project.

Having settled on a language, the next question is: Would it be possible to develop my trading robot in Ruby while calling IB's Java API? Well, anything in programming is possible, but would it be feasible? That is the better question. I've found JRuby, which is essentially Ruby implemented in Java. This will make it easy for my Ruby program to access IB's Java API. It will allow me to do my development in Ruby while accessing any Java classes and libraries. Theoretically this should work.

Note that the technologies I've mentioned are all open source. This is important, since the idea behind this project is to be able to develop and deploy it with no cost. Another advantage of using Java and Ruby is that it is supported on many platforms. While I'm working exclusively on MS Windows at this time, I would like to have the flexibility to deploy it on Linux in the future. When I incorporate a database system in the future, it will also be open source.