business-science / tidyquant

Bringing financial analysis to the tidyverse
https://business-science.github.io/tidyquant/
Other
852 stars 175 forks source link

tq_performance test fails under PerformanceAnalytics 2.0.6 #223

Closed joshuaulrich closed 1 year ago

joshuaulrich commented 2 years ago

This test fails under the development version of PerformanceAnalytics (2.0.6; CRAN is currently 2.0.4).

Ra <- c("AAPL", "GOOG", "NFLX") |>
    tq_get(get  = "stock.prices",
           from = "2010-01-01",
           to   = "2015-12-31") |>
    dplyr::group_by(symbol) |>
    tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "Ra")

# Get returns for SP500 as baseline
Rb <- "^GSPC" |>
    tq_get(get  = "stock.prices",
           from = "2010-01-01",
           to   = "2015-12-31") |>
    tq_transmute(adjusted, periodReturn, period = "monthly", col_rename = "Rb")

# Merge stock returns with baseline
RaRb <- dplyr::left_join(Ra, Rb, by = c("date" = "date"))
test1 <- tq_performance(RaRb, Ra = Ra, performance_fun = SharpeRatio, p = 0.95)

testthat::expect_equal(ncol(test1), 4)  # ncol(test1) == 5

This is because 2.0.6 added another row of output to SharpeRatio().

data(managers)
SharpeRatio(managers[,1,drop=FALSE], Rf=.035/12)
                                     HAM1
StdDev Sharpe (Rf=0.3%, p=95%): 0.3201889
VaR Sharpe (Rf=0.3%, p=95%):    0.2397362
ES Sharpe (Rf=0.3%, p=95%):     0.1345817
SemiSD Sharpe (Rf=0.3%, p=95%): 0.3041254

I'd change the test to be explicit about the types of Sharpe ratio you want returned (currently c("StdDev", "VaR", "ES")). That way the test won't fail when 2.0.6 hits CRAN.

I would also avoid pulling data from the internet for tests, unless you're testing the data import functionality. You can use data that comes with packages instead (e.g. the managers data in PerformanceAnalytics). Then your tests don't depend on an internet connection.

mdancho84 commented 2 years ago

Thanks for this. I will fix.

mdancho84 commented 1 year ago

This fix is implemented.