faultcode

Bayer price chart with SMI & TSO Kurtosis in R

Oct 17th, 2021
1,076
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Bayer price chart with SMI & TSO Kurtosis.R
  2. #
  3. # faultcode, 2021-10-17
  4. #
  5. # env.: R version 4.1.1 (2021-08-10) -- "Kick Things"
  6. #   Platform: x86_64-w64-mingw32/x64 (64-bit)
  7. #
  8. #
  9. # test: OK
  10. #
  11. #
  12.  
  13.  
  14. library(tidyquant)  # loads tidyquant, tidyverse, lubridate, xts, quantmod, TTR, %>%
  15. library(cowplot)
  16. library(scales)     # for scale_y_continuous()
  17. library(dplyr)      # load rename() -- For Renaming Columns
  18. library(ggplot2)    # load ggplot()
  19.  
  20.  
  21.  
  22. ###############################################
  23. ## user input:
  24. # how many years of daily quotes:
  25. noOfyears <- 10
  26. curr1 <- c('EUR')
  27. tckr1 <- c('BAYN.DE')
  28. security_name1 <- paste("Bayer AG, XETRA (", tckr1, ")", sep = "")
  29.  
  30.  
  31. MA1     <- 50
  32. MA2     <- 200
  33.  
  34. # Stochastic Momentum Index:
  35. n_1      <- 200
  36. nFast_1  <- 100
  37. nSlow_1  <- 200
  38. nSig_1   <- 100
  39.  
  40. # TSO Kurtosis:
  41. PeriodK   <- 45*5  # trading days
  42. PeriodFK  <- 52*5  # trading days
  43. PeriodFSK <- 45*5  # trading days
  44. #
  45. ###############################################
  46.  
  47.  
  48. end <- format(Sys.Date(),"%Y-%m-%d")  # get date of today in form: yyyy-mm-dd
  49. start <- format(Sys.Date() - 365*noOfyears,"%Y-%m-%d")  # start noOfyears years ago
  50.  
  51. # getSymbols("BLK", src = "google")
  52. dat1 <- tq_get(tckr1[1], get = "stock.prices", from = start, to = end)
  53.  
  54.  
  55. # tibble dat1 could have "NA" values:
  56. dat1 <- na.omit(dat1)
  57.  
  58.  
  59. # get the first date:
  60. first_date <- dat1[1, ]$date
  61.  
  62. # get the most recent date:
  63. last_date <- last(dat1$date)
  64.  
  65.  
  66. # calculate and add indicator values:
  67. dat1a <- dat1 %>%
  68.          tq_mutate(select = close,
  69.                       mutate_fun = EMA,
  70.                       n = MA1,
  71.                       col_rename = "EMA1") %>%
  72.  
  73.          tq_mutate(select = close,
  74.                       mutate_fun = EMA,
  75.                       n = MA2,
  76.                       col_rename = "EMA2") %>%
  77.  
  78.          tq_mutate(select = c(high, low, close),
  79.                    mutate_fun = SMI,  # Stochastic Momentum Index from TTR package (Technical Trading Rules)
  80.                    n = n_1,
  81.                    nFast = nFast_1,
  82.                    nSlow = nSlow_1,
  83.                    nSig = nSig_1,
  84.                    maType=list(list(EMA, wilder = TRUE),
  85.                                list(EMA, wilder = TRUE),
  86.                                list(EMA, wilder = TRUE))
  87.                    )
  88.  
  89.  
  90.  
  91. # calculate and add TSO Kurtosis value:
  92. #
  93. #  Mom(t) = Close(t) – Close(t - PeriodK):
  94. mom  <- dat1a %>% tq_mutate(select = close, mutate_fun = lag.xts, k = PeriodK, na.pad = TRUE)
  95. mom1 <- mom$close - mom$lag.xts
  96.  
  97. # K(t) = Mom(t) – Mom(t-1td), 1td fixed
  98. K <- mom1 - c(NA, head(mom1, -1))
  99.  
  100. dat1a$K <- K
  101.  
  102. # FastK(t) = EMA(K(t))|PeriodFK:
  103. dat1b <- dat1a %>%
  104.          tq_mutate(select = K,
  105.                    mutate_fun = EMA,
  106.                    n = PeriodFK,
  107.                    col_rename = "FastK")
  108.  
  109. # SlowK(t) = SMA(FastK(t))|PeriodFSK = TSO Kurtosis:
  110. dat1c <- dat1b %>%
  111.          tq_mutate(select = FastK,
  112.                    mutate_fun = SMA,
  113.                    n = PeriodFSK)
  114.  
  115. dat1c <- dat1c %>% rename(SlowK = SMA)  # rename(<new> = <old>)
  116.  
  117.  
  118.  
  119. # plot the multi chart:
  120. plot_title1 <- paste(security_name1, ": ", first_date, " - ", last_date, sep = "")
  121.  
  122. ylabel  <- paste("[", curr1, "]")
  123. ylabel2 <- paste("Stochastic Momentum Index")
  124. ylabel3 <- paste("TSO Kurtosis")
  125.  
  126.  
  127. # plot price:
  128. p1 <- dat1c %>%
  129.       ggplot(aes(x = date, y = close)) +
  130.  
  131.       # plot daily prices as a line:
  132.       geom_line() +
  133.       # geom_line(aes(y = EMA1), colour = "blue") +
  134.       # geom_line(aes(y = EMA2), colour = "red") +
  135.  
  136.       scale_y_continuous(
  137.         trans = "log",
  138.         limits = c(40, 140),
  139.         breaks = c(40, 60, 80, 100, 120, 140)
  140.       ) +
  141.  
  142.       scale_x_date(
  143.         date_breaks = "1 year",
  144.         date_labels = "%Y"
  145.       ) +
  146.  
  147.       labs(title = plot_title1,
  148.            # x = paste("EMA ", MA1, ": blue, EMA ", MA2, ": red", sep = ""),
  149.                      x = paste(""),
  150.            y = ylabel) +
  151.  
  152.       theme_tq()
  153.  
  154.       # theme(
  155.       #   axis.title.x = element_blank(),
  156.       #   axis.text.x = element_blank()
  157.       # )
  158.  
  159.  
  160. # plot first indicator:
  161. p2 <- dat1c %>%
  162.       ggplot(aes(x = date)) +
  163.  
  164.       # plot daily indicator values as a line
  165.       geom_line(aes(y = SMI), colour = "green") +
  166.       geom_line(aes(y = signal), colour = "grey") +
  167.  
  168.       scale_x_date(
  169.         date_breaks = "1 year",
  170.         date_labels = "%Y"
  171.       ) +
  172.  
  173.       labs(x = paste("periods=", n_1,
  174.                      ", ini.smooth.=", nFast_1,
  175.                      ", double smooth.=", nSlow_1,
  176.                      ", signal periods=", nSig_1,
  177.                      "; EMA|Wilder=on/on/on", sep=""),
  178.            y = ylabel2) +
  179.       # scale_y_continuous(label = comma) +
  180.  
  181.       theme_tq()
  182.  
  183.       # theme(
  184.       #   axis.title.x = element_blank()
  185.       # )
  186.  
  187.  
  188. # plot second indicator:
  189. p3 <- dat1c %>%
  190.       ggplot(aes(x = date)) +
  191.  
  192.       # plot daily indicator values as a line
  193.       geom_line(aes(y = SlowK), colour = "dodgerblue") +
  194.  
  195.       scale_x_date(
  196.         date_breaks = "1 year",
  197.         date_labels = "%Y"
  198.       ) +
  199.  
  200.       # geom_line(aes(y = 50), colour = "blue", linetype="dashed", size = 1) +
  201.  
  202.       labs(x = paste("PeriodK/PeriodFK/PeriodFSK=",
  203.                             PeriodK, "/", PeriodFK, "/", PeriodFSK, sep = ""),
  204.            y = ylabel3) +
  205.  
  206.       theme_tq()
  207.  
  208.  
  209. # plot_grid(p1, p2, p3, ncol = 1, align = 'v')
  210. plot_grid(p1, p3, ncol = 1, align = 'v')
  211.  
  212.  
  213. # end of Bayer price chart with SMI & TSO Kurtosis.R
  214.  
RAW Paste Data