CS290I - Scalable Internet Services and Systems

Thorsten von Eicken - UCSB - Spring 2001

Handout #5 - Project 3

Updated 5/14/2001
look for NEW (added 6/4) and modified (changed since printed handout)
Due Thursday, May 17th, 1pm

Important:

Objective

In this project you will add dynamic activity to your site in the form of stock trading. The trading rules below are meant to be only a very vague approximation of what happens in a real stock market. The purpose being to implement something with interesting behavior, not to simulate reality.

Database tables & fields

You will have to augment the database tables from project 2 in order to implement the stock trading. We strongly suggest you add the following tables and fields (more may be needed). Before deviating from this structure, think carefully whether you really need to.

Accounts table:

Orders table:

Holdings table:

Web pages

Your site will feature the following pages for its users (in addition to the pages from project 2):

In addition, your site must have the following pages for robots that submit orders and verify the operation of the stock market. For simplicity, these pages are not password protected. Each page must be of content-type text/plain (not HTML!), and consists of a table, one row per line, and each field separated by a comma. The fields must be in the order shown in the descriptions below. Date&time fields must be represented [NEW] either as YYYY-MM-DD HH:MM:SS.S or in milliseconds since "the epoch". For example:

tve, Thorsten von Eicken, false, 1000000.00
josep, Josep Blanquer, true, 0.01

Notes:

How to settle orders

Modified: Your order settlement is event driven and is triggered whenever a new order is entered into the system. First, each new order is assigned an "order id", which is a monotonically increasing integer, which captures the order which which orders (uhh) are entered. The new order is then matched against all orders of opposite type (buy vs. sell) that are in the company order book. The following matches can occur:

  1. Both orders have a limit price, the buy price is lower than the sell price
  2. Both orders have a limit price, the buy price is higher than the sell price
  3. One order has a limit price, the other doesn't
  4. Neither order has a limit price

Any order that "falls through" the matching process (i.e. cannot be matched) is added to the order book.

At the end of a day (at midnight: we assume 24/7 trading), all order books are cleared: all pending orders receive an outcome of "expired".

Example of an order settlement with a split:

  1. Suppose the order book initially contains one order:
  2. Now a buy order is placed:
  3. Settlement applies rule #2
  4. The original order is now fulfilled:
  5. The second order is only partially fulfilled and has to be split. This results in the following three orders:

Note on ordering constraints: modified

Orders have to be processed strictly according to the assigned order id. So this must be a sequential process. However, there is some room for adjustment in assigning the order id. A client can only observe three events: the time at which the HTTP request is sent (Treq), the time at which the HTTP response is received (Tresp), and the order id that was assigned (Oid). The only invariants that the server must maintain are:

Tresp1 < Treq2  =>  Oid1 < Oid2
Oid1 < Oid2  =>  Treq1 < Tresp2

How to compute quotes

The quotes displayed on the company page and on the robot quote page are detailed quotes that show the top of the order book, and not just  a single number representing the price of the last transaction. This full quote consists of 5 items:

  1. the ticker symbol
  2. the highest limit price of all buy orders in the order book
  3. the quantity to be bought at that price
  4. the lowest limit price of all sell orders in the order book
  5. the quantity to be sold at that price 

For example:

AAPL: buy 100 @ $100.000 : sell 1000 @ $110.550

indicates that the best buy offer is at $100 and that 100 shares are being asked for at that price, while the best sell offer is at $110.550 and that 1000 shares are available at that price.

In more detail, the prices and quantities are computed as follows (apply these steps for buy and sell orders separately):

For the historical quote information (i.e. filling in the tables inherited from project 1), the closing price is the price of the last transaction of the day. The opening price is the same as the previous day's closing price. The high and low prices are the max/min transaction prices of the day.

How to compute time

The order settling algorithm clearly must take the time at which orders are placed into account. In addition, some bookkeeping needs to be done at the end of a "day". In order to avoid having to run benchmarks for days, we will use a fake clock that runs 1000 times faster than real-time. So simulating one day takes 86.4 seconds. In unix, the date&time are kept as milliseconds since "epoch" (sometime in 1970), call current_date the current number of real seconds since the epoch. In addition, call start_date the value of current_date at the time the last robot initialization page request was received. Then the fake_date is computed as follows:

fake_date = start_date + ( current_date - start_date ) * 1000

I.e., we make time run 1000 times faster since the last robot init page request. The server must use this fake time throughout its operation.

What to turn in modified

Write a "one page" project overview describing the overall structure of your code. Describe the presentation layer (using WebMacro?), the business logic, and the storage/DB layer. Any special features you are using? Special algorithms, tricks, etc? Any packages you found on the web that are useful?

What's coming next

Project 4 will have a very simple problem statement: run your web service on 3 machines at the same time, all using the same database on bugatti (4th machine). We will load-balance incoming requests round-robin to your 3 servers. Provide the best performance you can, and make your service resilient to server and machine failures (i.e. we will kill your processes and/or halt the machines).