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.)