CS333 Lab P2-1:
The Client/Server Paradigm

Goals of this lab:


In this lab, you will design and implement a server process for a (not necessarily realistic) stock exchange that handles multiple clients. Your server will also act as a brokerage firm, keeping track of each client's stock and cash position. In conjunction with your server, you will implement a client module that will manage a portfolio by placing orders to buy and sell stock.


The stock exchange server accepts client orders to trade stock. Each buy (sell) order will have a desired maximum (minimum) price, along with the account number and an expiration date for the order. The server will trade in only one stock (the ABC Corporation) and it will only complete transactions in lots of exactly 100 shares.

Matching: As each order comes in, the server matches compatible orders and conducts trades. For example, if client A wants to buy 100 shares at 44 dollars per share and client B wants to sell 100 shares at 42 dollars per share, then there is a match. If there is not a match, then the order is stored in case a matching order comes in later.

Stock transactions: When there is a match, the actual trade price will depend on whether it is a bull or bear market. During a bull market (when the stock prices are rising), the seller is favored (so the trade price would be 44). During a bear market (when the stock prices are falling), the buyer is favored (so the trade price would be 42). When the trade is transacted, both the buyer's and seller's accounts are updated as follows: The stock (100 shares) is moved from the seller's to the buyer's account. The seller's cash position is credited by an amount 100 times the trade price, less a 1% sales charge. The buyer's cash position is debited by 100 times the trade price, plus a 1% sales charge.

Restrictions on orders: Clients are allowed to have at most one buy order and one sell order at a time. (The buy order should be lower than the sell order, or else the transactions will both take place and the customer will be charged commission for both sales!)

Time: The server keeps track of time as follows. The stock market will run for 100 days or until the stock price reaches 0, whichever comes first. Each day is 8 hours long, where one simulated hour is equals about one second of real time. At the end of the simulation, the final stock price is printed, along with the stock and cash positions of each remaining client.

The log: The server should print a log of its activities. Each day, it should print the day (i.e., Day 37: ) followed by an hour-by-hour listing of all the transactions taking place that day, At the close of each day, the high and low prices for that day are printed, as well as the price of the last trade for the day. At the close of each day, orders that expired that day are discarded (and a message is printed for each one).

Company stock: The ABC Corporation has a standing order to buy (sell) stock at one dollar below (above) market price, where market price is defined as the price of the most recent trade. Trades with ABC Corporation are always "last resort" trades, meaning that trades between other clients should be considered first. The same matching rule applies as with other clients, but the ABC Corporation does not pay any sales charges.

Buying on margin (optional): Clients are allowed to buy stock on margin, meaning that their cash positions are allowed to go negative for stock purchases, provided the total value of stocks in their account (number of shares times market price)is at least twice the total amount borrowed. If the borrowed amount becomes excessive (exceeding 1/2 the total worth of the stock), the server should automatically sell off shares (at the current market price or back to the ABC Corporation) until the cash position of the client is no longer excessive. Note that such a sale could be triggered by a drop in the price of the stock. (Even with "margin calls," sales are always in lots of 100 shares -- you will not deal in split lots.)


  1. Read about aggregates and element-to-aggregate connections in the Playground manual.

  2. Write the stock exchange server as a Playground module, according to the above description. Your server should have five presentation entries: the current day (0 to 99), the current hour (0 to 7), the price of the most recent trade (initially 20 dollars), a PGlist of client data, and a request entry for placing orders. Each client's data is a PGtuple containing an account number (assigned by the server), a current number of shares owned (initially 0), and a current cash balance (initially 10,000). All presentation entries should be read-only, except for the one in which trade orders are placed. Each order will be a tuple with an account number, a type (buy or sell), a price, and an expiration date (0 to 99). If the requested price in an order is missing (0), then it is taken to be an order to trade at the market price.

  3. Write a stock market client to interact with your server. The objective, of course, is to maximize the total value of the portfolio. The client will publish a PGtuple in which it will receive its account information from the server (using an aggregate-to-element connection). It will also publish a request tuple that it will use to send requests to the server. Set up your client so that it places orders on the basis of parameters that you set in a graphical interface you construct with EUPHORIA. You can also show your total portfolio value, broken down into stock and cash portions. If you want to get fancy, you could let each client keep track of its porfolio values over time in a PGarray, and show a graph of this information in Euphoria.

  4. Run some tests in which several instances of your client application interact simultaneously with your server to perform stock transactions.

To receive credit for this lab, you should:

  1. Clean up and print out your code. (Don't turn it in, but save it for your code/design review.)

  2. Turn in a Project Evaluation Form near the beginning of class on the day you want to do your demonstration and code/design review. You should be prepared to demonstrate your working application, explain your design and code, and answer questions.