Skip to content

Commit

Permalink
rename locate_code to stock_locate
Browse files Browse the repository at this point in the history
  • Loading branch information
DavZim committed Dec 17, 2020
1 parent 0734103 commit 28cc0e8
Show file tree
Hide file tree
Showing 12 changed files with 151 additions and 150 deletions.
2 changes: 1 addition & 1 deletion R/download_stock_directory.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ download_stock_directory <- function(exchange, date, quiet = FALSE) {
url <- paste0(base_url, exchange, "_stocklocate_", format(date, "%Y%m%d"), ".txt")

d <- data.table::fread(url, showProgress = !quiet)
data.table::setnames(d, c("ticker", "locate_code"))
data.table::setnames(d, c("ticker", "stock_locate"))
d[, ':=' (exchange = toupper(exchange), date = date)]
}

Expand Down
6 changes: 3 additions & 3 deletions R/read_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#' tries to find the stock_locate based on the \code{stock_directory} argument,
#' if this is not found, it will try to extract the stock directory from the file,
#' else an error is thrown.
#' @param stock_directory A data.frame containing the stock-locate_code relationship.
#' @param stock_directory A data.frame containing the stock-locate code relationship.
#' As outputted by \code{\link{read_stock_directory}}.
#' Only used if \code{filter_stock} is set. To download the stock directory from
#' NASDAQs FTP server, use \code{\link{download_stock_directory}}.
Expand Down Expand Up @@ -213,7 +213,7 @@ read_ITCH <- function(file, type, skip = 0, n_max = -1,
if (!(length(filter_stock) == 1 && is.na(filter_stock))) {
if (length(stock_directory) == 1 && is.na(stock_directory)) {
warning("filter_stock is given, but no stock_directory is specified. Trying to extract stock directory from file\n")
stock_directory <- RITCH::read_stock_directory(file, quiet = TRUE)
stock_directory <- read_stock_directory(file, quiet = TRUE)
}

if (!all(filter_stock %chin% stock_directory$stock)) {
Expand All @@ -224,7 +224,7 @@ read_ITCH <- function(file, type, skip = 0, n_max = -1,
}
# extend locate code by the stocks:
filter_stock_locate <- c(filter_stock_locate,
stock_directory[stock %chin%filter_stock, locate_code])
stock_directory[stock %chin%filter_stock, stock_locate])
}

if (!quiet && length(filter_stock_locate) > 0)
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ while (n_parsed < n_messages) {
}
```

You can also filter a dataset directly while reading messages for `msg_type`, `locate_code`, `timestamp` range, as well as `stock`. Note that filtering for a specific stock, is just a shorthand lookup for the stocks `locate_code`s, therefore a `stock_directory` needs to be supplied (either by providing the output from `read_stock_directory()` or `download_locate_code()`) or the function will try to extract the stock directory from the file.
You can also filter a dataset directly while reading messages for `msg_type`, `stock_locate`, `timestamp` range, as well as `stock`. Note that filtering for a specific stock, is just a shorthand lookup for the stocks `stock_locate`s, therefore a `stock_directory` needs to be supplied (either by providing the output from `read_stock_directory()` or `download_stock_locate()`) or the function will try to extract the stock directory from the file.

```{r}
# read in the stock directory as we filter for stock names later on
Expand Down
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ orders <- read_orders(file)
str(orders)
#> Classes 'data.table' and 'data.frame': 5000 obs. of 13 variables:
#> $ msg_type : chr "A" "A" "F" "A" ...
#> $ locate_code : int 2 2 2 2 2 2 2 2 2 2 ...
#> $ stock_locate : int 2 2 2 2 2 2 2 2 2 2 ...
#> $ tracking_number: int 0 0 0 0 0 0 0 0 0 0 ...
#> $ timestamp :integer64 31139052372053 31141354532167 32813425752711 32826656500150 32827351405783 32893988026867 33067242028997 33300886636321 ...
#> $ order_ref :integer64 0 100 84836 87020 87040 93032 105532 121012 ...
Expand All @@ -96,7 +96,7 @@ trades <- read_trades(file, n_max = 100)
str(trades)
#> Classes 'data.table' and 'data.frame': 100 obs. of 14 variables:
#> $ msg_type : chr "P" "P" "P" "P" ...
#> $ locate_code : int 2 2 2 2 2 3 3 3 3 3 ...
#> $ stock_locate : int 2 2 2 2 2 3 3 3 3 3 ...
#> $ tracking_number: int 2 2 2 2 2 2 4 2 2 2 ...
#> $ timestamp :integer64 34210128591201 34210355475120 34210767188977 34211127433476 34212046014088 34235711475708 34239928637481 34239928703094 ...
#> $ order_ref :integer64 0 0 0 0 0 0 0 0 ...
Expand Down Expand Up @@ -256,11 +256,11 @@ while (n_parsed < n_messages) {
```

You can also filter a dataset directly while reading messages for
`msg_type`, `locate_code`, `timestamp` range, as well as `stock`. Note
`msg_type`, `stock_locate`, `timestamp` range, as well as `stock`. Note
that filtering for a specific stock, is just a shorthand lookup for the
stocks `locate_code`s, therefore a `stock_directory` needs to be
stocks `stock_locate`s, therefore a `stock_directory` needs to be
supplied (either by providing the output from `read_stock_directory()`
or `download_locate_code()`) or the function will try to extract the
or `download_stock_locate()`) or the function will try to extract the
stock directory from the file.

``` r
Expand All @@ -274,11 +274,12 @@ od <- read_orders(
max_timestamp = 55800000000000, # end at 15:30:00.000000
filter_stock_locate = 1, # take only stock with code 1
filter_stock = "CHAR", # but also take stock CHAR
stock_directory = sdir # provide the stock_directory to match stock names to locate_codes
stock_directory = sdir # provide the stock_directory to match stock names to stock_locates
)
#> [Filter] 'msg_type': 'A'
#> [Filter] 'timestamp': 43200000000000 - 55800000000000
#> [Filter] 'stock_locate': '1', '3'
#> [Filter] msg_type: 'A'
#> [Filter] timestamp: 43200000000000 - 55800000000000
#> [Filter] stock_locate: '1', '3'
#> NOTE: as filter arguments were given, the number of messages may be off
#> [Counting] 5,000 messages found
#> [Loading] .
#> [Converting] to data.table
Expand All @@ -290,10 +291,10 @@ od[, .(n = .N), by = msg_type]
range(od$timestamp)
#> integer64
#> [1] 43235810473334 55792143963723
od[, .(n = .N), by = .(locate_code, stock)]
#> locate_code stock n
#> 1: 3 CHAR 574
#> 2: 1 ALC 508
od[, .(n = .N), by = .(stock_locate, stock)]
#> stock_locate stock n
#> 1: 3 CHAR 574
#> 2: 1 ALC 508
```

If you are interested in writing `ITCH_50` files or gaining a better
Expand Down
2 changes: 1 addition & 1 deletion debug/README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ dbg_hex_to_orders(hex_string)
```{r}
od <- data.table::data.table(
msg_type = "F",
locate_code = 8236L,
stock_locate = 8236L,
tracking_number = 0L,
timestamp = bit64::as.integer64(25200002107428),
order_ref = bit64::as.integer64(4),
Expand Down
52 changes: 26 additions & 26 deletions debug/debug_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ uint64_t parse_orders_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::NumericVector order_ref = df["order_ref"];
Expand All @@ -549,7 +549,7 @@ uint64_t parse_orders_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -572,7 +572,7 @@ uint64_t parse_trades_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::NumericVector order_ref = df["order_ref"];
Expand All @@ -588,7 +588,7 @@ uint64_t parse_trades_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand Down Expand Up @@ -636,7 +636,7 @@ uint64_t parse_modifications_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::NumericVector order_ref = df["order_ref"];
Expand All @@ -651,7 +651,7 @@ uint64_t parse_modifications_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand Down Expand Up @@ -697,7 +697,7 @@ uint64_t parse_system_events_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector event_code = df["event_code"];
Expand All @@ -707,7 +707,7 @@ uint64_t parse_system_events_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -722,7 +722,7 @@ uint64_t parse_stock_directory_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector stock = df["stock"];
Expand All @@ -745,7 +745,7 @@ uint64_t parse_stock_directory_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand Down Expand Up @@ -774,7 +774,7 @@ uint64_t parse_trading_status_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector stock = df["stock"];
Expand All @@ -789,7 +789,7 @@ uint64_t parse_trading_status_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand Down Expand Up @@ -820,7 +820,7 @@ uint64_t parse_reg_sho_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector stock = df["stock"];
Expand All @@ -831,7 +831,7 @@ uint64_t parse_reg_sho_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -847,7 +847,7 @@ uint64_t parse_market_participants_states_at(unsigned char * buf, Rcpp::DataFram
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector mpid = df["mpid"];
Expand All @@ -861,7 +861,7 @@ uint64_t parse_market_participants_states_at(unsigned char * buf, Rcpp::DataFram
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -880,7 +880,7 @@ uint64_t parse_mwcb_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::NumericVector level1 = df["level1"];
Expand All @@ -893,7 +893,7 @@ uint64_t parse_mwcb_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand Down Expand Up @@ -921,7 +921,7 @@ uint64_t parse_ipo_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector stock = df["stock"];
Expand All @@ -934,7 +934,7 @@ uint64_t parse_ipo_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -952,7 +952,7 @@ uint64_t parse_luld_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector stock = df["stock"];
Expand All @@ -966,7 +966,7 @@ uint64_t parse_luld_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -986,7 +986,7 @@ uint64_t parse_noii_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::NumericVector paired_shares = df["paired_shares"];
Expand All @@ -1004,7 +1004,7 @@ uint64_t parse_noii_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand All @@ -1030,7 +1030,7 @@ uint64_t parse_rpii_at(unsigned char * buf, Rcpp::DataFrame df,
uint64_t msg_num) {

Rcpp::CharacterVector msg_type = df["msg_type"];
Rcpp::IntegerVector locate_code = df["locate_code"];
Rcpp::IntegerVector stock_locate = df["stock_locate"];
Rcpp::IntegerVector tracking_number = df["tracking_number"];
Rcpp::NumericVector timestamp = df["timestamp"];
Rcpp::CharacterVector stock = df["stock"];
Expand All @@ -1041,7 +1041,7 @@ uint64_t parse_rpii_at(unsigned char * buf, Rcpp::DataFrame df,
const char msg = Rcpp::as<char>(msg_type[msg_num]);
buf[i++] = msg;

i += set2bytes(&buf[i], locate_code[msg_num]);
i += set2bytes(&buf[i], stock_locate[msg_num]);
i += set2bytes(&buf[i], tracking_number[msg_num]);

std::memcpy(&val64, &(timestamp[msg_num]), sizeof(int64_t));
Expand Down
Loading

0 comments on commit 28cc0e8

Please sign in to comment.