R语言代做编程辅导COMP 226: Computer-based trading in financial markets(附答案)
全文链接:http://tecdat.cn/?p=30948
Limit Order Book Pricer
Submit your code via department electronic submission system. http://www.csc.liv.ac.uk/cgi-bin/submit.pl
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/~rahul/teaching/comp226/assess.html 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')}
}
}
}
}
相关文章
- 彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-用户系统EP03
- c语言编程 sort()什么意思,void sort在C语言中什么意思?「建议收藏」
- JetBrains GoLand 2022 Mac中文激活版(GO语言编程软件)
- 15.Go语言-通道
- Java 并发编程中的死锁 ( Kotlin 语言讲解)
- continue和break的区别 python_c语言continue c.编程.net
- 学生选课管理系统c语言课程设计_大一c语言学生选课系统代码
- r语言 固定效应模型_r语言coef函数
- R语言代做编程辅导和解答M3S9/M4S9 Stochastic Simulation: Project 2
- R语言代做编程辅导STATG/M003 STATISTICAL COMPUTING | IN-COURSE ASSESSMENT 2(附答案)
- c语言编程用什么软件-学习c语言需要什么软件_c语言编程软件有哪些
- R语言稀疏主成分分析SPARSEPCA、因子分析、KMO检验和Bartlett球度检验分析上市公司财务指标数据
- 用c语言手搓一个600行的类c语言解释器: 给编程初学者的解释器教程(4)- 语法分析1:EBNF和递归下降文法
- 解读Go语言的2022:泛型时代开启
- Julia编程01:介绍Julia语言
- Go语言使用range复用临时变量
- Linux C编程入门:新手快速掌握语言基础(linuxcnew)
- Linux下学习C语言编程:从入门到精通(linux编写c语言程序)
- 语言Linux下开发:从C语言到Python(linux下用什么编程)
- Linux C语言脚本:开启你的编程之旅(linuxc语言脚本)
- Linux下实现多国语言输入法框架(linux输入法框架)
- Go语言操作Oracle轻松实现数据库编程(go语言访问oracle)
- 混合语言编程—C#使用原生的Directx和OpenGL绘图的方法
- c语言多线程编程使用示例
- GO语言并发编程之互斥锁、读写锁详解