Final Assignment, CSC490, Spring 2009
1 Assignment
Your task is to write a player for a simple stock market simulation.
2 Architecture
The market simulation will consist of a "market" and a bunch of "players". The market will coordinate the game, passing along information about new orders to each player. You’re writing the code for one of the players.
Each player will live in a thread of its own, communicating with the stock market using two channels; one that brings information from the market, and the other that you use to send messages to the market. The following signature (userDefs.mli) is implemented by the userDefs.ml module. Both of these are provided as part of the sample bundle, below.
(* basic type aliases *) |
type company = string;; |
type putOrCall = bool;; |
type strike = int;; (* in pennies *) |
type price = int;; (* in pennies *) |
type size = int;; (* number of shares or options: may be negative, can't be zero *) |
type expiry = int;; (* in seconds since midnight *) |
type id;; (* order id's must be unique *) |
|
|
|
|
|
|
|
|
|
(* something that you might buy *) |
type thingtobuy = |
| Share of company |
| Option of company * putOrCall * strike * expiry;; |
|
|
|
|
|
|
|
|
|
|
|
(* an order *) |
type order = thingtobuy * price * size;; |
|
|
|
|
|
|
|
|
|
|
type messageToPlayer = |
NewOrder of order * id |
| Confirm of messageToMarket * id |
| Fail of messageToMarket |
|
|
|
|
|
|
|
|
|
|
and messageToMarket = |
FillOrder of id |
| PlaceOrder of order |
| CancelOrder of id;; |
|
Some of this will already make sense; the world consists of orders, where an order has the share or option desired, the price per share offered, and the number of shares desired. If this number is positive, it’s an offer to buy. If this number is negative, it’s an offer to sell. Each order has a unique id, assigned by the market.
Communication will happen over two channels: an input channel, that the market will use to pass information to the players, and an output channel, that the players will use to pass information to the market.
The messageToMarket type describes the messages that the market can pass to a user. The NewOrder message (sent to everyone) indicates that there’s a new order in the world to be filled, the OrderFilled message (sent to everyone) indicates that the order with the given id has been filled. The Confirm message (sent only to one player) indicates that the specified message is confirmed. The Fail message indicates that the specified message failed.
The messageToPlayer type describes the messages that the Player can send to the Market. The FillOrder message indicates a desire to fill the order with the given id. The PlaceOrder message indicates a desire to place a new order. The CancelOrder message indicates a desire to cancel an earlier-placed order.
The market will keep track of your balance; if your balance ever goes negative, your player will simply be terminated.
3 Interface
Your module, "player.ml", must match this interface:
(* given an initial cash balance, an input channel and an ouput channel, do your thing. *) |
val go : int -> messageToPlayer channel -> messageToMarket channel -> unit;; |
The first argument represents the money that you start with, and the other two arguments are the channels that your program will use to communicate with the market.
4 Further Details and Hints
To see how the whole thing is going to work, the file sample.tgz contains a complete working project that contains a near-trivial server (It just makes up a new order every 3 seconds), and a near-trivial player (It just acknowledges each order by printing something to stdout).
Edit the makefile to set OCAMLLIB to the path to the ocaml libs on your system. The existing makefile is set up correctly for the lab machines.
run ’make’.
run ’./prog1’
You should see something like this:
clements@bowie:/tmp/sample $ ./prog1 |
I see a new order! |
I see a new order! |
I see a new order! |
I see a new order! |
I see a new order! |
I see a new order! |
... |
|
with one message every 3 seconds.
You may use this as the basis for your code.
5 Handin
Put your code and tests in a single ML file, and hand it in using the DrScheme handin plugin.
6 Disclaimer
There may well be bugs in this assignment. Let me know right away if you find some!