Created
March 3, 2019 20:08
-
-
Save bee-san/783c7d7235937b26a0532360d499dda4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
table = read.table("sample1_in.txt", stringsAsFactors=FALSE, fill=TRUE) | |
colnames(table) = c("Timestamp", "Type", "Order-Id", "Side", "Price", "Size") | |
# get reduce rows | |
# table[input$Type == "R", ] | |
# get add rows | |
# input[input$Type == "A", ] | |
# cosmetic fix for reduce rows | |
# input[input$Type == 'r', 'Size'] = input[input$Type == 'r', 'Side'] | |
# input[Input$Type == 'R', 'Side'] = NA | |
pricer <- function(infile, outfile, targetsize){ | |
for (i in 1:nrow(table)) { | |
if (input[i, "Type"] == "A") | |
print("Add") | |
} | |
else if (input[i, "Type"] == "R") | |
print("Reduce") | |
else | |
print("Error") | |
} | |
# making an order book (maintaining bid and ask books) | |
# so you have to have 2 internal books | |
# bid and ask books | |
# ask is buying, bid is selling (i think) | |
# the code below is how you make one book (an example) | |
book = data.frame() | |
book = rbind(book.data.frame(price = 10, size = 20, id = "ab", stringsAsFactors = FALSE)) | |
book = rbind(book.data.frame(price = 12, size = 25, id = "ac", stringsAsFactors = FALSE)) | |
# stirngs as factors is false is because id is not same if one is factor one is string | |
# sorting the book | |
book = book[order(book[, "price"], decreasing=TRUE),] | |
# the solution to work sheet 2 is useful here | |
# we can order a dataframe by using permutation of the rows | |
# we order the book by price, why? | |
# we are going to compute using the bid order book, the income we would get by placing a market order | |
# for target size. | |
# the reaosn we use bids is because when a sell market order comes in, its a seller getting | |
# matched against bidders | |
# if its a buy market order, (one of the rows with a b in it) we'll be looking at the ask limit | |
# order book | |
# to do the matching we start with either the best bid in the bid order book or best ask | |
# and we go through in order of price | |
# we sort by prices to access the best bid first, then the second best bid. | |
# if bid book looks like this: | |
# ID size price | |
# cc 2 32 | |
# ac 25 12 | |
# we would buy 2 shares of IDA cc (as it has the highest price, best bid first) | |
# we would then get 8 from price 12 (if target size = 10). | |
# by sorting we can go through the order book in order. | |
# taking out the volume you need | |
# we'll see a WHILE LOOP FORMULATION OF DOING THIS | |
# this type of code uses the fact that the order book is ordered by price | |
# so we can go through rows to get best price first | |
# if seller u want to sell at best price possible | |
avgPriceMarketOrder = function(bidBook, k){ | |
bidBook = bidBook[order(bidBook[, "Prices"], decreasing = TRUE),] | |
# sorts book ^^ | |
available = sum(bidBook[, "Volumes"]) # check how much is available | |
traded = min(available, k) # how many lots will be traded | |
i = 1 | |
totalPrice = 0 | |
leftToTrade = traded | |
while (leftToTrade > 0){ | |
tradedAtThisPrice = min(leftToTrade, bidBook[i, "Volumes"]) | |
totalPrice = totalPrice + tradedAtThisPrice * bidBook[i, "Prices"] | |
leftToTrade = leftToTrade - tradedAtThisPrice | |
i = i + 1 | |
} | |
averagePrice = as.numeric(totalPrice / traded) | |
} | |
# when you get a reduce message, you want to remove size from order book | |
# if you're careful and made sure everything is a string | |
# you've got these 2 books | |
# "i promise you if an ID appears, it appears in at most one of the book | |
# it might not come from both of the books | |
# if you dont find it in the first book, check second book | |
# if you find it in the first one, dont even bother for the second one | |
# look for rows which have id equal to what you're looking for | |
# if it returns one row, its fine | |
# if it returns no rows, move on. | |
book[book$id=="cc",] | |
# when you know it has a row, change it | |
book[book$id=="cc", "size"] = 0 | |
# DONT SET IOT TO 0, take the old size and subtract the reduce amount | |
# but he doesnt want to give us the final solution | |
# output is always 2 decimal places after dot | |
price = 100 | |
cat(file="test.out", "the price is ", price, "\n") | |
# cat stands for concatenate string, but can also append to a file | |
# some people every year just store all the results in a dataframe and dump it at the end | |
# rahul suggests doing it incrementally | |
# make sure the file is empty when you write to it, flush it out | |
cat(append=TRUE) | |
# makes it append to the end | |
# the first one doesnt append ,it just re-writes it all | |
# R sucks, do this: | |
isTRUE(all.equal(A, B)) | |
# instead of | |
A == B | |
# the above can give you errors, because floating point imprecision | |
# USE THIS CODE TO FORMAT!!! | |
prices = c(800.5, 800.25, 800) | |
cat(prices, '\n') | |
cat(format(prices, nsmall=2), '\n') | |
# some people only change income / expense after add order | |
# just like an add row can change and create output, so can a reduce row | |
# dont check if both income / expense has changed for each row, because at most one of them has changed | |
# if a new bid hs come in, the only thing that has changed is the income or a sell market order | |
# DO CHECK WHETHER THEY CHANGE!!! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment