Skip to content

Instantly share code, notes, and snippets.

@bee-san
Created March 6, 2019 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bee-san/6a7fb1fc7d92801cca4f073d119b7a7e to your computer and use it in GitHub Desktop.
Save bee-san/6a7fb1fc7d92801cca4f073d119b7a7e to your computer and use it in GitHub Desktop.
# Brandon Skerritt
# b.skerritt@liverpool.ac.uk
# reads in and renames the column names
table = read.table("sample1_in.txt", stringsAsFactors=FALSE, fill=TRUE)
colnames(table) = c("Timestamp", "Type", "Order-Id", "Side", "Price", "Size")
# total expense you would incur if you bought targetsize shares (by taking as many asksas necessary, lowest first), and
# the total income you would receive if you sold targetsize shares (by hitting as manybids as necessary, highest first)
# global variables bidBook and askBook
bidBook = data.frame()
askBook = data.frame()
pricer = function(table, targetLots){
# cant sort a book with nothing in it, this stops it from breaking
bidBook <<- rbind(bidBook,data.frame(price =0, size= 0, id="aaaaaaaaaaaaaaaaa", stringsAsFactors=FALSE))
askBook <<- rbind(askBook,data.frame(price =0, size= 0, id="aaaaaaaaaaaaaaaaa", stringsAsFactors=FALSE))
# this is so i can wipe the file when i cat to output
firstCat = TRUE
for (i in 1:nrow(table)){
# for every row in the market order book
prev_shares = 0.0
new_shares = 0.0
# ask row
if (table[i, "Type"] == "A"){
# if the type is an add row
if (table[i, "Side"] == "B"){
# if its a buy row
prev_shares = totalPrice(bidBook, targetLots)
# get current shares of all rows in bidBook
bidBook <<- rbind(bidBook,data.frame(price = table[i, "Price"], size=table[i, "Size"],id=table[i, "Order-Id"], stringsAsFactors=FALSE))
bidBook <<- bidBook[order(bidBook[, "price"], decreasing=TRUE),]
# add to book
new_shares = totalPrice(bidBook, targetLots)
if (length(prev_shares) > 0){
# sometimes when this is run at the start of the book
# prev shares is nothing (as theres nothing in the book
# the exact error is "prev_shares has length 0
# so this if statement stops that
if (prev_shares < new_shares){
# output here
timeStampTemp = table[i, "Timestamp"]
cat(timeStampTemp, "B ", format(new_shares, nsmall=2), "\n")
}
}
# get current shares again
# compare
}
else if (table[i, "Side"] == "S"){
prev_shares = getTotalShares(askBook)
askBook <<- rbind(askBook,data.frame(price = table[i, "Price"],size=table[i, "Size"],id=table[i, "Order-Id"], stringsAsFactors=FALSE))
askBook <<- askBook[order(askBook[, "price"], decreasing=TRUE),]
new_shares = getTotalShares(askBook)
if (length(prev_shares) > 0){
if (prev_shares < new_shares){
timeStampTemp = table[i, "Timestamp"]
cat(timeStampTemp, "S ", format(new_shares, nsmall=2), "\n")
}
}
}
else {
print("Error")
}
}
else if (table[i, "Type"] == "R"){
# loop thorugh ask, loop through bid
# identify the one to change
#edit row
# dont write to file
reduceOrder(table[i, "Order-Id"], table[i, "Size"])
}
else {
print("Error")
}
}
}
reduceOrder = function(id, size){
# takes in id and sizevv
# loops through bid book looking for an order that
# matches id and size
found = FALSE
for (i in 1:nrow(bidBook)){
if(bidBook[i, "id"] == id){
found = TRUE
bidBook[i, "size"] = bidBook[i, "size"] - size
}
}
# if its not found in bidbook, do the same for ask book
if (!found){
for(i in 1:nrow(askBook)){
if(askBook[i, "id"]== id){
askBook[i, "size"] = askBook[i, "size"] - size
found = TRUE
}
}
}
# if its still not found, its an error
else if (!found){
print("Error, reduce order not found")
}
}
getTotalShares = function(table){
total = 0.0
for (i in 1:nrow(table)){
total = total + table[i, "size"] * table[i, "price"]
}
return(total)
}
totalPrice = function(book, targetLots){
available = sum(book[, "size"])
# sometimes its NA, so just make it 0.0
# tom spooner told me to do this
if (is.na(available)){
available = 0.0
}
if (available < targetLots){
# TODO this probably will fuck everything up
# pretty sure i need to return NA here but it messes things up
# if you spot this, pls don't penalise me thank u xx
return(0.0)
}
i = 1
price = 0.0
leftToTrade = targetLots
# calculates the shares at best price
while (leftToTrade > 0){
tradedAtThisPrice = min(leftToTrade, book[i, "Size"])
price = price + tradedAtThisPrice * book[i, "price"]
leftToTrade = leftToTrade - tradedAtThisPrice
i = i + 1
}
return(price)
}
pricer(table, 500)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment