# COSC/MATH 201 - Random Walks in Practice (Module 9.5)
# A toy stock-price random walk: watch three companies, add a sell rule with break,
# and simulate many shares. The first loop animates, so it takes about 12 seconds.

days <- 250   # about one year of trading days
x <- 1:days
y1 <- numeric(days)
y2 <- numeric(days)
y3 <- numeric(days)
y1[1] <- 10
y2[1] <- 10
y3[1] <- 10

for (i in 2:days) {
  y1[i] <- y1[i - 1] + runif(1, -0.2, 0.2)
  y2[i] <- y2[i - 1] + runif(1, -0.2, 0.2)
  y3[i] <- y3[i - 1] + runif(1, -0.2, 0.2)

  plot(x[1:i], y1[1:i], type = "l", col = "red",
       xlim = c(1, days), ylim = c(4, 16),
       xlab = "Trading day", ylab = "Price ($)")
  lines(x[1:i], y2[1:i], col = "blue")
  lines(x[1:i], y3[1:i], col = "darkgreen")

  Sys.sleep(0.05)
}

set.seed(10)
days <- 250
x <- 1:days
y1 <- numeric(days)
y2 <- numeric(days)
y3 <- numeric(days)
y1[1] <- 10
y2[1] <- 10
y3[1] <- 10

for (i in 2:days) {
  y1[i] <- y1[i - 1] + runif(1, -0.2, 0.2)
  y2[i] <- y2[i - 1] + runif(1, -0.2, 0.2)
  y3[i] <- y3[i - 1] + runif(1, -0.2, 0.2)
}

plot(x, y1, type = "l", col = "red", ylim = c(4, 16),
     xlab = "Trading day", ylab = "Price ($)",
     main = "Three companies, one rule")
lines(x, y2, col = "blue")
lines(x, y3, col = "darkgreen")
abline(h = 10, lty = 3, col = "gray50")

set.seed(15)
target <- 13
stop_price <- 8
days <- 250

price <- numeric(days + 1)
price[1] <- 10
outcome <- "holding"

for (i in 2:(days + 1)) {
  price[i] <- price[i - 1] + runif(1, -0.2, 0.2)
  if (price[i] >= target) {
    outcome <- "target"
    break
  }
  if (price[i] <= stop_price) {
    outcome <- "stop"
    break
  }
}

days_held <- i - 1
outcome
days_held

plot(1:i, price[1:i], type = "l", ylim = c(7, 14),
     xlab = "Day (1 is the day we bought)", ylab = "Price ($)",
     main = "One share with a sell rule")
abline(h = target, col = "darkgreen", lwd = 2)
abline(h = stop_price, col = "red", lwd = 2)

set.seed(2)
shares <- 1000
outcome <- character(shares)

for (share in 1:shares) {
  price <- 10
  outcome[share] <- "holding"
  for (day in 1:days) {
    price <- price + runif(1, -0.2, 0.2)
    if (price >= target) {
      outcome[share] <- "target"
      break
    }
    if (price <= stop_price) {
      outcome[share] <- "stop"
      break
    }
  }
}

c(target = mean(outcome == "target"),
  stop = mean(outcome == "stop"),
  holding = mean(outcome == "holding"))

set.seed(3)
outcome <- character(shares)

for (share in 1:shares) {
  price <- 10
  outcome[share] <- "holding"
  for (day in 1:days) {
    price <- price + runif(1, -0.2, 0.2)
    if (price >= target) {
      outcome[share] <- "target"
      break
    }
    if (price <= stop_price) {
      outcome[share] <- "stop"
      break
    }
  }
}

c(target = mean(outcome == "target"),
  stop = mean(outcome == "stop"),
  holding = mean(outcome == "holding"))

set.seed(2)
outcome <- character(shares)

for (share in 1:shares) {
  price <- 10
  outcome[share] <- "holding"
  for (day in 1:days) {
    price <- price + runif(1, -0.2, 0.21)
    if (price >= target) {
      outcome[share] <- "target"
      break
    }
    if (price <= stop_price) {
      outcome[share] <- "stop"
      break
    }
  }
}

c(target = mean(outcome == "target"),
  stop = mean(outcome == "stop"),
  holding = mean(outcome == "holding"))
