Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMP226
Assignment
Have an understanding of market microstructure and
its impact on trading.
Goal of
Assignment
Reconstruct a limit order book from order messages;
compute quantities based on the limit order book
Marking Criteria Pre-deadline visible CodeGrade tests of
correctness of 6 functions (70%); Post-deadline
CodeGrade tests of correctness for 4 "extra"
functions (30%)
Submission
necessary in
order to satisfy
module
requirements
No
Expected time
taken
Roughly 8-12 hours
Pre vs. post deadline tests
• 70% of marks are available via visible pre-deadline tests on
CodeGrade for 6 functions. If your code passes all the tests you
get the full 70% for this part of the assignmnent
• You can submit as many times as you want, and use the
CodeGrade feedback to improve your mark for this part
• 30% of marks are for passing post-deadline tests for 4 extra
functions but these marks are only available if you got all 70%
for the pre-deadline tests
Code/data zip handout
• Download comp226_a1.zip
• Unzip comp226_a1.zip
• This will yield a directory called comp226_a1
comp226_a1
nnn common.R
nnn input
n nnn book_1.csv
n nnn book_2.csv
n nnn book_3.csv
n nnn empty.txt
n nnn message_a.txt
n nnn message_ar.txt
n nnn message_arc.txt
n nnn message_ex_add.txt
n nnn message_ex_cross.txt
n nnn message_ex_reduce.txt
n nnn message_ex_same_price.txt
nnn main.R
nnn output
n nnn book_1-message_a.out
n nnn book_1-message_ar.out
n nnn book_1-message_arc.out
n nnn book_2-message_a.out
n nnn book_2-message_ar.out
n nnn book_2-message_arc.out
n nnn book_3-message_a.out
n nnn book_3-message_ar.out
n nnn book_3-message_arc.out
nnn template.R
2 directories, 23 files
The contents are explained on the next slide..
Code/data zip handout
• main.R: the script that you should call, examples below (do not
edit it)
• common.R: provides a range of fully implemented functions (do
not edit it)
• template.R: code template that contains 10 empty functions that you
need to complete
• input: subdirectory that contains two types of input files, initial book
files and message files
• output: subdirectory that contains sample output that allows you to
check your code implementations
Ex: using main.R with template.R
$ Rscript main.R template.R input/book_1.csv input/empty.txt
$ask
oid price size
1 a 105 100
$bid
oid price size
1 b 95 100
Total volume:
Best prices:
Mid-price:
Spread:
input/book_1.csv is the initial book, input/empty.txt is the message
file (empty in this case)
main.R
options(warn=-1)
args <- commandArgs(trailingOnly = TRUE); nargs = length(args)
log <- (nargs == 4) # TRUE is there are exactly 4 arguments
arg_format <- "<--log> "
if (nargs < 3 || nargs > 4) # check that there are 3 or 4 arguments
stop(paste("main.R has 3 required arguments and 1 optional flag:", arg_format))
if (nargs == 4 && args[1] != "--log") # if 4 check that --log is the first
stop(paste("Bad arguments format, expected:", arg_format))
solution_path <- args[nargs-2]
book_path <- args[nargs-1]
messages_path <- args[nargs]
if (!all(file.exists(c(solution_path, book_path, messages_path))))
stop("File does not exist at path provided.")
source(solution_path); source("common.R") # source common.R from pwd
book <- book.load(book_path)
book <- book.reconstruct(data.load(messages_path), init=book, log=log)
book.summarise(book)
main.R
• checks the command line arguments are ok
• assigns them to variables
• sources common.R and the file at solution_path
• loads an initial book
• reconstructs the book according to the messages
• prints out the book
• prints out the book stats
Rscript from Rstudio
Rscript main.R template.R input/book_1.csv input/empty.txt
• In R studio, you can call Rscript from the "terminal" tab (as opposed to
the "console")
• On Windows, use Rscript.exe not Rscript:
Rscript.exe main.R template.R input/book_1.csv input/empty.txt
70%: 6 functions to implement
Order book stats:
1. book.total_volume <- function(book) [5%]
2. book.best_prices <- function(book) [5%]
3. book.midprice <- function(book) [5%]
4. book.spread <- function(book) [5%]
Reconstructing the limit order book:
5. book.reduce <- function(book, message) [15%]
6. book.add <- function(book, message) [35%]
input/book_1.csv
oid,side,price,size
a,S,105,100
b,B,95,100
oid side price size
a S 105 100
b B 95 100
• oid (order id) is used to process (partial) cancellations of orders that
arise in "reduce" messages
• side: 'B' for a buy/bid; 'S' for a sell/ask order
• price and size are self-explanatory
Order book stats
• book.total_volumes should return a list with named elements,
bid and ask where bid (ask) should be the total volume in the bid
(ask) book
• book.best_prices should return a list with two named
elements, bid and ask where bid (ask) should be the best bid (ask)
price
• book.midprice should return the midprice
• book.spread should return the spread
The functions can be tested with an empty message file (you do not
need to have implemented book.add or book.reduce)
Expected output
$ Rscript main.R solution.R input/book_1.csv input/empty.txt