Skip to content

Commit

Permalink
Fix #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Emanuele Guidotti committed Dec 24, 2024
1 parent 881e9e7 commit 8234e7c
Show file tree
Hide file tree
Showing 27 changed files with 785 additions and 316 deletions.
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Efficient Estimation of Bid-Ask Spreads from Open, High, Low, and Close Prices

This [repository](https://github.com/eguidotti/bidask/) implements an efficient estimator of the effective bid-ask spread from open, high, low, and close prices as described in:
This [repository](https://github.com/eguidotti/bidask/) implements the efficient estimator of the effective bid-ask spread from open, high, low, and close prices described in:

> Ardia, D., Guidotti, E., Kroencke, T.A. (2024). Efficient Estimation of Bid-Ask Spreads from Open, High, Low, and Close Prices. *Journal of Financial Economics*, 161, 103916. [doi: 10.1016/j.jfineco.2024.103916](https://doi.org/10.1016/j.jfineco.2024.103916)
Expand Down Expand Up @@ -33,10 +33,7 @@ You can browse publications related to the paper [here](https://scholar.google.c

## Terms of use

All code is released under the [GPL-3.0](https://github.com/eguidotti/bidask/?tab=GPL-3.0-1-ov-file#GPL-3.0-1-ov-file) license. All data are released under the [CC BY 4.0](http://creativecommons.org/licenses/by/4.0) license. When using any data or code from this repository, you agree to:

- cite [Ardia, Guidotti, & Kroencke (2024)](https://doi.org/10.1016/j.jfineco.2024.103916) as indicated below
- place the link [https://github.com/eguidotti/bidask](https://github.com/eguidotti/bidask) in a footnote to help others find this repository
All code is released under the [MIT](https://github.com/eguidotti/bidask?tab=MIT-1-ov-file#readme) license. All data are released under the [CC BY 4.0](http://creativecommons.org/licenses/by/4.0) license. When using any data or code from this repository, please cite the reference indicated below.

## Cite as

Expand Down
17 changes: 10 additions & 7 deletions r/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ Authors@R: c(
person(given = "David", family = "Ardia", role = c("ctb"), comment = c(ORCID = "0000-0003-2823-782X")),
person(given = "Tim", family = "Kroencke", role = c("ctb"), comment = c(ORCID = "0000-0001-8700-356X"))
)
Description: Implements an efficient estimator of bid-ask spreads from open, high, low, and close prices
as described in Ardia, Guidotti, & Kroencke (2024) <doi:10.1016/j.jfineco.2024.103916>.
Description: Implements the efficient estimator of bid-ask spreads from open, high, low, and close prices
described in Ardia, Guidotti, & Kroencke (JFE, 2024) <doi:10.1016/j.jfineco.2024.103916>.
It also provides an implementation of the estimators described in
Roll (1984) <doi:10.1111/j.1540-6261.1984.tb03897.x>,
Corwin & Schultz (2012) <doi:10.1111/j.1540-6261.2012.01729.x>,
and Abdi & Ranaldo (2017) <doi:10.1093/rfs/hhx084>.
License: GPL-3
Roll (JF, 1984) <doi:10.1111/j.1540-6261.1984.tb03897.x>,
Corwin & Schultz (JF, 2012) <doi:10.1111/j.1540-6261.2012.01729.x>,
and Abdi & Ranaldo (RFS, 2017) <doi:10.1093/rfs/hhx084>.
License: MIT + file LICENSE
URL: https://github.com/eguidotti/bidask
BugReports: https://github.com/eguidotti/bidask/issues
Encoding: UTF-8
Imports: xts, zoo
Imports: data.table
RoxygenNote: 7.2.3
Suggests:
xts,
zoo,
dplyr,
crypto2,
quantmod,
ggplot2,
knitr,
rmarkdown,
testthat (>= 3.0.0)
Expand Down
2 changes: 2 additions & 0 deletions r/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
YEAR: 2024
COPYRIGHT HOLDER: Emanuele Guidotti
5 changes: 3 additions & 2 deletions r/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Generated by roxygen2: do not edit by hand

export(edge)
export(edge_expanding)
export(edge_rolling)
export(sim)
export(spread)
import(xts)
importFrom(stats,lag)
import(data.table)
importFrom(stats,rbinom)
importFrom(stats,rnorm)
29 changes: 16 additions & 13 deletions r/R/ar.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,40 @@
#'
#' @keywords internal
#'
AR <- function(x, width = nrow(x), method, sign, na.rm){
AR <- function(high, low, close, width, method, sign, na.rm){

ok <- c("AR","AR2")
if(length(ko <- setdiff(method, ok)))
stop(sprintf("Method(s) '%s' not available. The available methods are '%s'.",
paste(ko, collapse = "', '"), paste(ok, collapse = "', '")))

x <- log(x)
h <- log(high)
l <- log(low)
c <- log(close)

M2 <- (x$HIGH+x$LOW)/2
M1 <- lag(M2, 1)[-1,]
C1 <- lag(x$CLOSE, 1)
m2 <- (h + l) / 2
m1 <- shift(m2, 1)
c1 <- shift(c, 1)

S2 <- 4*(C1-M1)*(C1-M2)
s2 <- 4 * (c1 - m1) * (c1 - m2)

shift <- 1
ar <- ar2 <- NULL

if("AR" %in% method) {
ar <- rmean(S2, width = width-1, na.rm = na.rm)
ar <- rmean(s2, width = width, shift = shift, na.rm = na.rm)
ar <- sign(ar) * sqrt(abs(ar))
if(!sign) ar <- abs(ar)
colnames(ar) <- "AR"
ar <- list("AR" = ar)
}

if("AR2" %in% method){
S2[S2<0] <- 0
S <- sqrt(S2)
ar2 <- rmean(S, width = width-1, na.rm = na.rm)
colnames(ar2) <- "AR2"
s2[s2 < 0] <- 0
s <- sqrt(s2)
ar2 <- rmean(s, width = width, shift = shift, na.rm = na.rm)
ar2 <- list("AR2" = ar2)
}

return(cbind(ar, ar2))
return(c(ar, ar2))

}
42 changes: 21 additions & 21 deletions r/R/cs.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@
#'
#' @keywords internal
#'
CS <- function(x, width = nrow(x), method, sign, na.rm){
CS <- function(high, low, close, width, method, sign, na.rm){

ok <- c("CS","CS2")
if(length(ko <- setdiff(method, ok)))
stop(sprintf("Method(s) '%s' not available. The available methods are '%s'.",
paste(ko, collapse = "', '"), paste(ok, collapse = "', '")))

x <- log(x)
h <- log(high)
l <- log(low)
c <- log(close)

H <- x$HIGH[-1]
L <- x$LOW[-1]
c1 <- shift(c, 1)
h1 <- shift(h, 1)
l1 <- shift(l, 1)

C1 <- lag(x$CLOSE, 1)[-1]
H1 <- lag(x$HIGH, 1)[-1]
L1 <- lag(x$LOW, 1)[-1]

GAP <- pmax(0, C1-H) + pmin(0, C1-L)
AH <- H + GAP
AL <- L + GAP
gap <- pmax(0, c1 - h) + pmin(0, c1 - l)
ah <- h + gap
al <- l + gap

B <- (H-L)^2 + (H1-L1)^2
G <- (pmax(AH, H1) - pmin(AL, L1))^2
b <- (h - l)^2 + (h1 - l1)^2
g <- (pmax(ah, h1) - pmin(al, l1))^2

A <- (sqrt(2*B)-sqrt(B))/(3-2*sqrt(2)) - sqrt(G/(3-2*sqrt(2)))
S <- 2*(exp(A)-1)/(1+exp(A))
a <- (sqrt(2*b) - sqrt(b)) / (3 - 2*sqrt(2)) - sqrt(g / (3 - 2*sqrt(2)))
s <- 2*(exp(a) - 1) / (1 + exp(a))

shift <- 1
cs <- cs2 <- NULL

if("CS" %in% method) {
cs <- rmean(S, width = width-1, na.rm = na.rm)
cs <- rmean(s, width = width, shift = shift, na.rm = na.rm)
if(!sign) cs <- abs(cs)
colnames(cs) <- "CS"
cs <- list("CS" = cs)
}

if("CS2" %in% method){
S[S<0] <- 0
cs2 <- rmean(S, width = width-1, na.rm = na.rm)
colnames(cs2) <- "CS2"
s[s < 0] <- 0
cs2 <- rmean(s, width = width, shift = shift, na.rm = na.rm)
cs2 <- list("CS2" = cs2)
}

return(cbind(cs, cs2))
return(c(cs, cs2))

}
Loading

0 comments on commit 8234e7c

Please sign in to comment.