共计 5424 个字符,预计需要花费 14 分钟才能阅读完成。
全文链接:http://tecdat.cn/?p=30948
Limit Order Book Pricer
Submit your code via department electronic submission system.
http://www.csc.liv.ac.uk/cgi-…
Please submit a single file called x3xx.R (cs-username.R)
This file must contain a function called pricer
pricer can call other functions in the same file
But the function pricer should not be executed when the file is sourced with source(‘cs-username.R’)
Problem
Create R program to analyse log file of a limit order market
The log file contains messages that describe changes to the book
Each message either
• adds an order to the book, or
• reduces the size of an order in the book (possibly
removing the order entirely)
Example Input
28800538 A b S 44.26 100
28800562 A c B 44.10 100
28800744 R b 100
28800758 A d B 44.18 157
28800773 A e S 44.38 100
28800796 R d 157
28800812 A f B 44.18 157
28800974 A g S 44.27 100
28800975 R e 100
28812071 R f 100
28813129 A h B 43.68 50
Problem
You should write a function called pricer
It takes an positive integer called targetsize and an argument
and calculates:
• total expense to buy targetsize shares (by taking as
many asks as necessary, lowest first), and
• total income if you sold targetsize shares (by hitting as
many bids as necessary, highest first)
Each time the income or expense changes, it prints the changed
value to an output file in the format described below
Corresponding example output
For targetsize = 200
28800758 S 8832.56
28800796 S NA
28800812 S 8832.56
28800974 B 8865.00
28800975 B NA
28812071 S NA
28813129 S 8806.50
Arguments to pricer function
The function pricer should take three arguments as follows
`pricer <- function(infile, outfile, targetsize)
`
• infile is a string which contain the path to the input file
which is read from the harddrive; the file infile contains the
messages of the limit order book log file
• pricer should write its output to the a file with path specified
the string outfile, which is an argument to pricer
• the final argument is targetsize, which is a natural number,
e.g., 1 or 100, or 250
Example call to pricer
• suppose you have stored the a sample input file “sample1.txt”
in the current working directory; and
• suppose you have created an output directory called “output”
in the current working directory;
• suppose you want to try the function with targetsize=250
Then a call to pricer might then be:
pricer(infile="sample1.txt",
outfile="output/output1_250.txt",
targ
Test Data
You can download three sample input files here:
https://www2.csc.liv.ac.uk/~r…
For these three sample inputs, output for three values of
targetsize are given, so you have 9 examples to use for testing
Your program needs to workwith any value of `
“targetsize
Sample files
Input | Ouput | |
---|---|---|
targetsiz e = 1 | targetsize = 250 | targetsize = 1000 |
sample1 | ||
sample2 | ||
sample3 |
Input Format
The input file will contain one message per line
Each message is a series of fields separated by spaces, e.g.
28800538 A b S 44.26 100
An Add Order to Book message looks like this:
timestamp ‘A’ order-id side price size
A Reduce Order message looks like this:
timestamp ‘R’ order-id size
The log file messages are sorted by timestamp
Output Format
pricer’s output should be one message per line in this format:
timestamp action total 28800758 S 8832.56
• If pricer encounters an error in an input message, it prints a
warning to the R console and goes to the next message
• Note: the book initially contains no orders, and the
buying expense and selling income start as ‘NA’
• Since pricer only produces output when the income/expense
changes, it does not print anything until the total size of
all bids or asks meets or exceeds targetsize
Standard Input | Standard Output/Notes | |
---|---|---|
28800538 A b S 44.26 100 | No output yet because neither the bids nor the asks in the book have a total of 200 shares yet. | |
28800562 A c B 44.10 100 | Still not enough shares on either side of the book. | |
28800744 R b 100 | This reduces order ‘b’ to zero shares, which removes it from the book, so now the book contains no asks. But there’s still no change to the total income or expense on 200 shares. |
data.frame
• data.frame is a data structure in R, like a matrix, but
• unlike a matrix, the columns can be of different types
> df <- data.frame(a=1:3,
b=c("A","B","C"),
stringsAsFactors=F)
> df
a b
1 1 A
2 2 B
3 3 C
局部解答
pricer <- function(infile,outfile,targetsize) {
#infile<-"E:\\input.txt"
#outfile<-"E:\\output1.txt"
#targetsize=250
Sbook=data.frame(Timestamp=c(NA),Type=c(NA),OrderId=c(NA),Side=c(NA),Price=c(NA),Size=c(NA))
Bbook=data.frame(Timestamp=c(NA),Type=c(NA),OrderId=c(NA),Side=c(NA),Price=c(NA),Size=c(NA))
Sbook<- Sbook[-1,]
Bbook<- Bbook[-1,]
flag1=0
flag2=0
for (i in 1: nrow(trade)){#type=A
if(trade[i,"Type"]=="A"){if(trade[i,"Side"]=="B") {Bbook=rbind(Bbook,trade[i,])
if(is.null(bprice(Bbook,targetsize))==FALSE) {cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "S", format(bprice(Bbook,targetsize),nsmall=2),'\n')
flag2=1
}
else{if(flag2==1)cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "S NA",'\n')}
}
else {Sbook=rbind(trade[i,],Sbook)
if(is.null(price(Sbook,targetsize))==FALSE) {cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "B", format(price(Sbook,targetsize),nsmall=2),'\n')
flag1=1
}
else{if(flag1==1)cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "B NA",'\n')}
}
}
#type=R
else{reduceS=which(Sbook[1:i-1,"Order-Id"]==trade[i,"Order-Id"])
if(is.null(reduceS)!=TRUE){Sbook[reduceS,"Size"]=as.numeric(Sbook[reduceS,"Size"])-as.numeric(trade[i,"Size"])
if(length(Sbook[reduceS,"Size"])==0)Sbook=Sbook[-reduceS,]
if(is.null(price(Sbook,targetsize))==FALSE) {cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "B", format(price(Sbook,targetsize),nsmall=2),'\n')
flag1=1
}
else{if(flag1==1)cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "B NA",'\n')}
}else {reduceB=which(Bbook[1:i-1,"Order-Id"]==trade[i,"Order-Id"])
Bbook[reduceB,"Size"]=as.numeric(Bbook[reduceB,"Size"])-as.numeric(trade[i,"Size"])
if(length(Bbook[reduceB,"Size"])==0)Bbook=Bbook[-reduceB,]
if(is.null(bprice(Bbook,targetsize))==FALSE) {cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "S", format(bprice(Bbook,targetsize),nsmall=2),'\n')
flag2=1
}
else{if(flag2==1)cat(file=outfile,append=TRUE,trade[i,"Timestamp"], "S NA",'\n')}
}
}
}
}