Skip to content

Instantly share code, notes, and snippets.

@Polkas
Last active August 4, 2022 11:53
Show Gist options
  • Select an option

  • Save Polkas/d1058d80e84c624cb2426c0010331008 to your computer and use it in GitHub Desktop.

Select an option

Save Polkas/d1058d80e84c624cb2426c0010331008 to your computer and use it in GitHub Desktop.
WIG20TR and WIG40TR returns
``` r
# A very simple script to simulate all possible investment windows for x years investment
# Investment in WIG20TR or WIG40TR
library(xts)
w20 <- read.csv(glue::glue("https://stooq.com/q/d/l/?s=wig20tr&i=d"))
w40 <- read.csv(glue::glue("https://stooq.com/q/d/l/?s=mwig40tr&i=d"))
w80 <- read.csv(glue::glue("https://stooq.com/q/d/l/?s=swig80tr&i=d"))
stooq <- merge(w20[, c("Date", "Close")], w40[, c("Date", "Close")], by = "Date")
stooq <- merge(stooq, w80[, c("Date", "Close")], by = "Date")
colnames(stooq) <- c("Date", "w20", "w40", "w80")
stooq_xts <- xts::xts(stooq[, -1], order.by = as.Date(stooq$Date))
return_fun <- function(x) (tail(x, 1) - head(x, 1)) / head(x, 1)
time_spans <- (50*5) * 1:10
res <- list(w20 = list(), w40 = list(), w80 = list())
for (iter in seq_along(time_spans)) {
t <- time_spans[iter]
poss_days <- 1:(nrow(stooq_xts) - t)
for (ind in c("w20", "w40", "w80")) {
for (d in poss_days) {
res[[ind]][[as.character(t)]] <- c(res[[ind]][[as.character(t)]],
apply(stooq_xts[d:(d + t), ind], 2, return_fun))
}
}
}
# market days were taken into account
# (50 * 5) market days could be treated as 365 calendar days
# WIG20TR
# WIG20TR average return in an x days investment
sapply(res[["w20"]], function(x) mean(x))
#> 250 500 750 1000 1250 1500 1750
#> 0.03475478 0.04224242 0.05723118 0.06688252 0.09773571 0.10344102 0.12679868
#> 2000 2250 2500
#> 0.13839124 0.15852289 0.14705359
# WIG20TR probability to earn anything in x days
sapply(res[["w20"]], function(x) mean(x > 0))
#> 250 500 750 1000 1250 1500 1750 2000
#> 0.5401863 0.5643639 0.6536057 0.7449977 0.7072143 0.6925409 0.7419585 0.8041775
#> 2250 2500
#> 0.7808676 0.8135593
# WIG40TR
# WIG40TR average return in an x days investment
sapply(res[["w40"]], function(x) mean(x))
#> 250 500 750 1000 1250 1500 1750 2000
#> 0.1050323 0.2000609 0.3012181 0.4146646 0.5764303 0.7287748 0.8648586 0.9535661
#> 2250 2500
#> 1.1354386 1.2586713
# WIG40TR probability to earn anything in x days
sapply(res[["w40"]], function(x) mean(x > 0))
#> 250 500 750 1000 1250 1500 1750 2000
#> 0.6291825 0.7180068 0.8674448 0.9539321 0.9452343 0.9951486 1.0000000 1.0000000
#> 2250 2500
#> 1.0000000 1.0000000
# WIG80TR
# WIG80TR average return in an x days investment
sapply(res[["w80"]], function(x) mean(x))
#> 250 500 750 1000 1250 1500 1750
#> 0.09249957 0.19185813 0.25221224 0.28024308 0.36267262 0.45295860 0.52639359
#> 2000 2250 2500
#> 0.60603491 0.85293241 1.05943678
# WIG80TR probability to earn anything in x days
sapply(res[["w80"]], function(x) mean(x > 0))
#> 250 500 750 1000 1250 1500 1750 2000
#> 0.6702311 0.6564741 0.7819925 0.8743602 0.9062665 0.9478472 1.0000000 1.0000000
#> 2250 2500
#> 0.9877642 0.9969183
```
<sup>Created on 2022-08-04 by the [reprex package](https://reprex.tidyverse.org) (v2.0.1)</sup>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment