Goal of the project:
Main tabs in the dashboard:
Key features:
Process overview:
Download data → Compute returns → Simulate price paths → Analyze risk metrics → Visual output
simulate_montecarlo <- function(prices, days = 252, paths = 1000) {
if (is.null(prices) || length(prices) < 2) {
stop("Not enough data to simulate.")
}
# Calculate log returns - choice of log returns will be explained on next slide
returns <- diff(log(prices))
# Metrics needed to simulate market moves based on normal distribution
mu <- mean(returns, na.rm = TRUE)
sigma <- sd(returns, na.rm = TRUE)
# Last known price = starting point
S0 <- tail(prices, 1)
# Simulate random normal price moves
sim <- matrix(rnorm(days * paths, mean = mu, sd = sigma), ncol = paths)
# Convert to cumulative prices
sim_prices <- S0 * exp(apply(sim, 2, cumsum))
# We use cumulative sums of log-returns because log transforms
# multiplication of price factors into simple addition.
return(sim_prices)\[ r_t = \ln\left(\frac{P_t}{P_{t-1}}\right) \]
Why log-returns?
| Price Change | Simple Return | Log Return |
|---|---|---|
| +10% | +0.10 | +0.0953 |
| −10% | −0.0909 | −0.0953 |
Interpretation:
\[r_{\text{down}} = -r_{\text{up}}\] This makes statistical modeling more stable and mathematically consistent.
# Fetching data from stooq.pl is based on creating a proper URL
get_polish_data <- function(symbol, start_date = Sys.Date() - 365*3) {
# --- Building required URL ---
d1 <- format(as.Date(start_date), "%Y%m%d") # Changing format from 2000-01-01 to 20000101
d2 <- format(as.Date(Sys.Date()), "%Y%m%d")
url <- paste0("https://stooq.pl/q/d/l/?s=", symbol,
"&d1=", d1,
"&d2=", d2,
"&i=d")
# Data is obtained as a csv file
dane <- read.csv(url, header = TRUE, sep = ",", dec = ".")
dane <- na.omit(dane)
return(dane)
}
Simulation using Student-t distribution:
The parameter df controls how heavy the
tails of the distribution are.
| df | Market Type | Interpretation |
|---|---|---|
| 3–5 | Highly volatile | Crisis or strong sentiment periods |
| 10–20 | Moderate | Typical daily market behaviour |
| 30+ | Stable | Near-normal market behaviour |
Interpretation:
The simulation results demonstrate that market prediction is highly dependent on the historical period used for parameter estimation. Since the model assumes that future returns follow the same statistical properties as past data, its predictive power is significantly limited. Nevertheless, this program makes it easy to explore key risk metrics such as VaR and ES which are essential for assessing tail risk.