Wednesday, March 9, 2011

Stock Ticks Database Schema

This is what the stock ticks DB schema looks like.

CREATE table eod
(
        day date not null,
        symbol char(6) not null,
        open real not null,
        high real not null,
        low real not null,
        close real not null,
        volume bigint not null
) without oids;

CREATE INDEX eod_idx1 ON eod (day,symbol);
CREATE INDEX eod_idx2 ON eod (symbol);
CREATE INDEX eod_idx3 ON eod (open);
CREATE INDEX eod_idx4 ON eod (high);
CREATE INDEX eod_idx5 ON eod (low);
CREATE INDEX eod_idx6 ON eod (close);
CREATE INDEX eod_idx7 ON eod (volume);

CREATE table fundamentals
(
        day timestamp not null,
        symbol char(6) not null,
        industry text,
        sector text,
        shares bigint,
        pe real,
        pb real,
        de real,
        eps real
) without oids;

CREATE INDEX fund_idx1 ON fundamentals (day,symbol);
CREATE INDEX fund_idx2 ON fundamentals (symbol);
CREATE INDEX fund_idx3 ON fundamentals (industry);
CREATE INDEX fund_idx4 ON fundamentals (sector);
CREATE INDEX fund_idx5 ON fundamentals (shares);
CREATE INDEX fund_idx6 ON fundamentals (pe);
CREATE INDEX fund_idx7 ON fundamentals (pb);
CREATE INDEX fund_idx8 ON fundamentals (de);
CREATE INDEX fund_idx9 ON fundamentals (eps);

CREATE type adjtype AS ENUM ('split', 'dividend');

CREATE table adjustments
(
        day date not null,
        symbol char(6) not null,
        type adjtype not null,
        value1 text,
        value2 text,
        value3 text
) without oids;

CREATE INDEX adj_idx1 ON adjustments (day,symbol);

CREATE table info
(
        symbol char(6) not null,
        name text,
        gics text,
        lastupdate_adjustments timestamp,
        lastupdate_eod timestamp,
        lastupdate_fundamentals timestamp,
        lastupdate_info timestamp
) without oids;

CREATE INDEX inf_idx1 ON info (symbol);
CREATE INDEX inf_idx2 ON info (name);
CREATE INDEX inf_idx3 ON info (gics);
CREATE INDEX inf_idx4 ON info (lastupdate_adjustments);
CREATE INDEX inf_idx5 ON info (lastupdate_eod);
CREATE INDEX inf_idx6 ON info (lastupdate_fundamentals);
CREATE INDEX inf_idx7 ON info (lastupdate_info);


As you can see it will store 4 types of data; end of day prices (eod), fundamental indicators, price adjustments and general information. In future I may introduce intraday prices however that remains a long term goal. I need to walk before I can swim.

You should also notice I will not be storing adjusted end of day prices. Instead, adjustments will be applied in real ltime. This is because adjustments are retrospective. In other words, an adjustment that occurs tomorrow will have an impact on all historical prices. Maintaining a table of adjusted prices would require updates to the affected symbols rows every time a new adjustment is made. As the number of rows increase this could overwhelm the system.

To implement this feature I will use PL/SQL integrated with memcached to store adjusted prices in memory after they are first requested and calculated. On subsequent requests if the last adjustments date in the info table does not change the adjusted prices will be recalled from memory and bypass storage.

This design (I hope) should ensure the system is able to deliver adjusted prices quickly on second and subsequent requests and should be able to scale to intraday requirements easily.

Thursday, February 24, 2011

Software Infrastructure

I am at a point where I need to decide on a design for my trading platform including the software and tools that will support it. The diagram below illustrates at a high level the proposed design.



The design is made up of a number of components that will collectively manage and automate trading strategies.

First we have the Stock Ticks DB Manager. This will interface with the Online Broker Gateway to obtain real-time stock data and record them into the database. To enhance performance it will use memcached to cache the most frequent records and expire them as it deems necessary.

There will be one or more Strategy Managers that each implement a particular trading algorithm. These managers will communicate with the Stock Ticks DB Manager and Risk Manager to monitor and execute entry and exit trades.

The Risk Manager will interface with the Portfolio DB Manager to determine current risk level. The Risk Manager will also trade positions on behalf of the Strategy Managers via the Portfolio DB Manager which maintains an interface to the Online Brokers Gateway.

The Risk Manager will also be responsible for ensuring capital is allocated relative to the performance of the portfolio, individual strategies performance and risk ratings. In other words, it will be the clearing house for all trading decisions regardless of the number of strategies in place.

Each of the components will communicate via a common bus architecture which in this case will be implemented using message queues. Message queues have several advantages the most important being they will allow me to utilise different software infrastructure and languages if I find my original choices are not optimised to address a particular requirement.

Finally, there will be a Notification Manager. This component will subscribe to message queues that I am interested in and keep me updated on the trading platforms real-time activities. It will also manage certain queues that will be used by certain processes to seek my acknowledgment before proceeding further down the path. For example, the Risk Manager may seek my acknowledgment before implementing a trade of a particular value. My acknowledgment may be in the form of a reply to an Instant Message or Email that in turn accepts or rejects the request.

Here is a list of the initial software choices I have made to support the platform.

Of course these choices are for all intents and purposes untested in the context of running a trading platform. As I progress along this journey I may encounter issues that lead me to seek alternatives. If this situation arises I will endeavour to explain my rational for making the switch. However, it is obvious now from the choices I have made that I wish to remain within my comfort zone. I can rationalise this decision on the basis that I have more to learn concerning the fundamentals of quantitative trading strategies; and I need not add to that workload when it comes to the implementation.

Language: Perl

Perl is my strongest language. Whilst I can read and write other languages (C, Python, Java) they are not languages I use on a day to day basis and therefore developing in these languages is going to slow me down. Furthermore, there are a number of Perl modules I will leverage to expedite development. These include statistical methods available here and bindings to quantlib. Other than financial methods, I will rely on database, messaging, notification and HTTP interfaces to assist with data management, communications and web site (in this case the brokers) control.

As a side note the quantlib bindings for Perl just finished compiling. Good to see things work as advertised :)

Installing /usr/local/lib/perl/5.10.1/auto/QuantLib/QuantLib.bs
Installing /usr/local/lib/perl/5.10.1/auto/QuantLib/QuantLib.so
Installing /usr/local/lib/perl/5.10.1/QuantLib.pm

Database: Postgresql

I have chosen Postgresql as my relational database platform. This is a database I am very familiar with. I have used it on numerous projects and it has proven to be a reliable performer. It may not be the fastest RDBMS but then I do not require the fastest.

Communications Bus: RabbitMQ

My trading platform is going to be made up of multiple programs and processes that will need to communicate and coordinate activities. To address this requirement I am going to rely on RabbitMQ. This software implements a comprehensive messaging and queueing system (the glue) which will be used to satisfy the requirements of a robust communication bus. This particular implementation includes good support for Perl which is paramount. I did look at another contender Spread which also seems pretty popular however the available Perl interfaces are quite limiting. Spread has better support for the Python language. Both support message persistence which is one of my main requirements as I want the trading platform to be able to resume where it left off if infrastructure fails momentarily.

Optimisation: memcached

My platform will utilise memory to optimise the retrieval of frequently accessed objects (e.g. time series and stock prices). This will be implemented using memcached - a distributed memory object caching system.

One thing I have not mentioned is all my choices are open source or free to use for non-commercial use. In making these choices I did not assess commercial alternatives.

Q. For those reading this I would be interested to know what works for you now and what didn't work - bearing in mind my intent is to create an automated system.

Tuesday, February 22, 2011

Take aways from Ernie Chan's Book - How to Build Your Own Algorithmic Trading Business

I finished reading Ernie Chan's book a few days ago.

From someone with no background in quantitative trading I thought it provided a good introduction to the topic. However, I would have liked to have seen more design and implementation details of an algorithmic trading platform. The tools used in the book to describe the implementation of trading strategies are mostly primitive (e.g. excel) and not necessarily going to provide you a sustainable, semi or fully automated solution for trading quantitatively.

I guess I had preconceived ideas about what Quantitative Trading actually involved before picking up the book. In my mind in order to build an algorithmic trading business a degree of automation from the platform is required. I didn't get the design details for such a system from this book.

So unfortunately for me I may be on my own in designing this system.

A few other key points I took away were:

  1. Risk management is a key ingredient to preventing a strategy from sending you broke. How this is done seems straight forward and Ernie introduces the Kelly formula for this purpose.

  2. The last few pages of the book rationalise why independent traders can succeed over institutional managers and hedge funds. I am not going to repeat the reasons here as they reflect on Ernie's past working for hedge funds and now as an independent trader. What it did do is leave me with a feeling that I could one day be successful in this field.

  3. The model-based pairs trading strategy resonated with me. Certainly I am hopeful it will be possible to find co-integration between stock pairs fairly quickly

  4. As a discretionary stock trader there were many new concepts and techniques I picked up from the book that I haven't seen disclosed in other stock market / trading books (although I have only read a handful). This gives me some comfort that quantitative trading offers an edge over traditional methods.

Thursday, February 17, 2011

What Happened to the Quants in August 2007?

So I finished reading the paper What Happened to the Quants in August 2007. Here are my take aways..

  1. Returns from quantitative strategies deteriorate over time - particularly as these strategies become more mainstream. To sustain returns either new strategies must be found or leverage must be increased. In Australia, leverage does not appear to be as readily available for stocks or ETF's as it is in the US so I will have to work very hard to identify unique strategies.

  2. Standard deviation is a key metric to determine the variability of a strategy. The more deviations the greater the volatility. In my experience, volatility triggers human emotion and anxiety. This is definitely something I want to avoid whilst trading.

  3. There are many research papers available on the behaviour of financial markets and trading strategies. I need to be thinking about how I invest my time following other peoples research otherwise I  may become overwhelmed with choices and no clear direction. Hopefully, I can use this blog to kept me on course.
Back to reading Ernie's book.

    Monday, February 14, 2011

    Day One - Knowledge is Power

    So today I begin my quest to amass all the knowledge I can relating to Quantitative Trading. Unfortunately for me most of the material is written for markets outside of Australia. However, on first impressions it would appear the principles to Quantitative Trading - and more importantly - building a Quantitative Trading Platform, can be applied to the Australian market as well, albeit with more work.

    Here is a list of material I am currently reading:

    Other than reading books and papers I have also commenced setting up infrastructure that will run the platform. This is currently made up of:

    • A dual core Linux server w/ 4 GB RAM with solid state disk for data (lives under my house)
    • A dual core Windows desktop w/ 6 GB RAM (when I am home) with 3 x 24" monitors
    • A lightweight laptop (when I am not home)
    • An Internet router with VPN software (so I can access my server remotely)
    • A high speed cable Internet connection that links to my Internet router
    • Gigabit network connections between all systems

    To execute trades I signed up to an Australian online broker which will remain unnamed for the time being.

    For charting and tracking my portfolio I use Insight Trader. This package was selected for general purpose stock trading. At this point I am unsure whether it will form part of my quantitative trading platform. As part of the package I purchased the database manager subscription. This costs me $154 p.a. and gives me access to adjusted stock prices. This however does not provide me ongoing end of day prices and must be sourced elsewhere. For this I rely on the online broker who offers end of day prices.

    I realise now I may be using terms without defining what they mean, like end of day prices. I encourage you to search the web in context to stocks, if you are unsure of a particular term.

    To save me from the daily burden of downloading prices from the web I have written a program that wakes up my UNIX server every weekday at market close and downloads the end of day file from the online broker. If for some reason the system skips a beat and fails to download prices it automatically detects this exception and reconciles the directory that stores all the end of day files - by date. In fact I have used this same process to automate the download of several years of end of day prices. The remaining years I purchased from the guys at Speculative Trading Ideas for a very reasonable price. They send it to you in the mail on CD. At this point I should offer a word of caution. If you are going to base your entire trading platform on someone else's data it would be wise to check it first. I have yet to go through the data supplied by STI. When I get around to it I will post my experience.

    I hope this gives you some insight as to where I am at in this journey. It will no doubt be a long and eventful experience. I hope you enjoy reading about it. Here are a couple of things on my todo list:

    • Finish reading Ernie Chan's book
    • Select a database management system that will store stock prices that I will use for back testing (I am considering NoSQL)
    • Research numerical computing environments for UNIX
    • Develop and thoroughly test a program that can automate trades using my online brokers system. Unfortunately, they do not offer an API to the general public so I will need to develop a system that interfaces with the brokers web site (like an individual would). I have a couple of tricks up my sleeve which should make this easier