Mind the Flex

Implementing Custom Functionality in Air for Trading

January 12th, 2010 | Posted by Martin
Filed under Adobe Air, Finance, Flex |

The following article demonstrates that Adobe Flex can easily replace an existing stock trading application Java API and by using the features of Air, add easily new functionality to it. It all started when I read the book "High Probability Trading Strategies" from Robert C. Miner where he describes how to predict the behaviour of stock price. It is a very good book and I recommend it to everyone who is interested in trading. The main idea of the book is that the stock prices almost always move in a certain range and the range is predictable by the price movements in the past. Since the concept was interesting and my current trading platform didnt offer all the features described in the book, I decided to try how difficult it would be to develop an extension to my current trading platform.

I'm currently using the Interactive Brokers Trader Workstation and am quite happy with all the functionality it offers. Still, the platform didn't offer all the functionality that the book described and I started to look for possibilities how to add it. I found out that Interacive Brokers offers several APIs (JAVA, C++, ActiveX) that can be used to implement your own functionality. The API itself consists of two logical components: connection to the workstation and the interpretation of the incoming data. The architecture of an extension component is following:

  • on top is running the main application: Trader Workstation. The application takes care of the the connection to the market data server and presenting the data.
  • in case you want to use your own functionality that uses the market data, you have to switch on the API feature in the Workstation settings panel. This means that the Workstation starts to listen to certain socket.
  • your application sends socket requests to the Workstation where they will be processed and forwarded to the market data server
  • Trader Workstation forwards the data from the market data server to your application using the socket connection

When writing socket software in actionscript, the first thing you have to pay attention to is the security policy aspect. Adobe has decided that you cannot connect to hosts that do not provide security policy file. The idea is that the host server has to provide a policy file to the flash player which then decides whether it is allowed to connect to the host. The policy is in xml format and more about it can be read here. By default the flash player expects the policy file to be served on port 843. There are multiple servers available in web that are free and that have only one purpose - to serve flash security policy file. One of these servers can be found here.

Since I wanted the extension to be in Air and there were only the APIs that were mentioned previously, I decided to port the existing Java API to Actionscript. The process of porting the API was somewhat monotonous since there were basically four things to be done (I found later that there are automatic converters for this like the one here):

  • add the keyword "function" to every method and modify the signature so that the return type is in the end of method
  • modify the error handling
  • modify socket connection to event based
  • modify data types

The code itself stayed pretty much the same and therefore the porting process could be considered rather easy. The only major change was related to a part that contained the thread that read from a socket. As the actionscript does not support multithreading, this part had to be removed and replaced with the event based socket data handling.

As mentioned initially, the main goal was to create a chart application that would receive data from Trader Workstation, display it in Air application chart that is used for analyzing the stock price data. Adobe Air suites quite well for this purpose and the development process is somewhat quicker and easier than using the other API-s.

One Response to “Implementing Custom Functionality in Air for Trading”

  1. Implementing Custom Functionality in Air for Trading | Tutz - Best Tutorials Says:

    […] the original: Implementing Custom Functionality in Air for Trading By: Admin Date: January 15, 2010 3:31 am Categories: Object, adobe, adobe air, amazon, art, […]

Leave a Reply