Socket Client (IB API)

Project Code available here link

Trading Strategy

Our team implemented a long/short trading strategy. In this one buys long on a stock expected to rise, and one sells short on a stock whose price is expected to decrease in value. This strategy is known as a market neutral strategy often used by hedge funds. Fund managers typically use this strategy to reduce volatility in a portfolio by diversifying or hedging positions across certain investment categories. For example a hedge fund might short DaimlerChrysler stock for a certain amount and long Ford stock for the same amount. If an event occurs causing auto industry stocks to fall, the fund will earn a profit on DaimlerChrysler and a matching loss on the Ford position. If both stocks rise, there will also be little effect on the position. It can also be presumed that the fund manager is shorting DaimlerChrysler and going long on Ford because he expects Ford stock to perform better. If this manager is correct in this case, the fund should profit.

There are some disadvantages with the long/short trading strategy. It is difficult to estimate and hedge the risks in a portfolio and to actively manage unsuccessful short positions in an active manner. A hedge fund must also be adept at determining through careful analysis which stocks will outperform their benchmark target. Fund managers and analysts must use available data to outperform other capable investors who have the same data at hand. This analysis can be time consuming in a fast moving marketplace.

Financial Instruments

The project uses a template function design pattern to take user input for stocks to long and short. This leaves it to the application user to create the strategy. As part of the testing, we used DELL and GOOG as the example in pairs trading.
Conceptual Design

The project is designed to use 5 design patterns from the course to implement a long/short trading strategy using the InteractiveBrokers API to manage our order book. Initially the Template design pattern is used to manage user input. Values that are input using the functions laid out in the Template pattern. The Bridge pattern is used to manage the long and short price quote. Once the quote is created, a factory and prototype contructor is instantiated to generate an order. The order is then placed into the book which is designed using the Singleton pattern. There is a buy and sell quote bridge and factory. The program then runs an infinite loop in which it connects to the IB socket and processes messages. Messages are handled using an instance of the singleton order book.

5 Design Patterns / Structure / Definition

Template Function pattern

The Template Function pattern is used to prompt the user for input values needed to implement the trading strategy. The values are retrieved in the order laid out by the functions in the pattern. The class InputProcess derived from the TemplateProcess object. Values in the object are stock, quantity, and limit values for both the long and short side. Methods are then laid out in the object to retrieve each value. This pattern is illustrated below.

Bridge pattern

The intent of the bridge pattern is to decouple an abstraction from its implementation so that the two can vary independently.
The PriceQuote class contains the actual quote information; price, symbol, and number of shares. The QuoteBridge contains a pointer to a PriceQuote and accesses the information through a set of accessor function.

Singleton Pattern

A Singleton ensures a class has only one instance, and provides a global point of access to it. The Book singleton contains a vector that holds pointers to the MyOrder objects. The singleton has two functions, insert and getBook. The insert function adds new MyOrders to the vector and the getBook function returns the vector to determine what’s been added.

Prototype Design pattern (Virtual Constructor pattern)

The prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects. This pattern is used to:
avoid subclasses of an object creator in the client application, like the abstract factory pattern does.
avoid the inherent cost of creating a new object in the standard way (e.g., using the ‘new’ keyword) when it is prohibitively expensive for a given application.

Factory method design pattern

The factory method pattern is an object-oriented creational design pattern to implement the concept of factories and deals with the problem of creating objects (products) without specifying the exact class of object that will be created. The essence of this pattern is to “Define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses
Factory and Prototype Design Pattern – Order

Input and Output

Input for the project is handled using the Template Function pattern which in the code is referred to as the TemplateProcess object. The user is prompted for each input item using the performTemplateProcess() method in the object. Input values are stock, quantity, and limit values for both the long and short side.

The project used the Interactive Brokers API code downloaded from their website to perform our order book operations. Our team was able to get the PosixTestClient.cpp code to handle our socket operations.

Analysis / Suggestions

The application is successful because the long short strategy is implemented and can be tested. However since we are leaving up to the user input we can’t give analysis on the success of a particular long short strategy as it would vary. Analysis and selection of the stocks used play a role in the profitablility of the strategy. Market conditions at the time and the time period over which the strategy is implemented are also a factor in the strategy results.

One possibility going forward would be to run a series of example tests. We could then examine the results and create a report on how to improve the interface, execution and results. Another possible future improvement would be to input multiple orders in one screen. Currently there was only one long/short trade setup. However, users should be able to input 5 sets of long/short trades.