Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require(quantmod)
- require(TTR)
- require(forecast)
- # Für welchen Wert sollen die Daten bezogen werden?
- # Lynas Australien
- symbolName <- "LYC.AX"
- # Lynas Frankfurt
- #symbolName <- "LYI.F"
- #
- # Molycorp
- # symbolName <- "9MO.F"
- #
- # China Rare Earth HK
- # symbolName <- "0769.HK"
- # symbolName <- "RAE.F"
- #
- # Andere Symbole von YAHOO
- # symbolName <- "SPY"
- # symbolName <- "DJIA"
- # symbolName <- "^GDAXI"
- # symbolName <- "^AORD"
- #
- # Um wieviele Tage in die Vergangenheit soll vorhergesagt werden?
- back <- 0
- # Wieviele Tage an Vorhersage?
- ahead <- 365
- # Order-Argumente für ar(..., order.max=....)
- ord_start <- 200
- ord_end <- 300
- ord_step <- 50
- histrows <- 1600
- # ####################
- ords <- seq(ord_start, ord_end, ord_step)
- #
- #
- # Chartdaten aus der Zeitreihe extrahieren
- z <- get( getSymbols( symbolName, from="1900-01-01") )
- names(z) <- c("Open", "High", "Low", "Close", "Volume", "Adjusted")
- # Anzahl der Zeilen
- nrows <- dim(z)[1]
- #
- # Einen Ausschnitt der gesamten Zeitreihe bilden
- cs <- tail(z, histrows)
- #
- # Donchian-Channels vorberechnen
- dc100 <- DonchianChannel(cs[,c("High","Low")], n=100)
- dc200 <- DonchianChannel(cs[,c("High","Low")], n=200)
- #
- # Gleitende Durchschnitte vorberechnen
- sma100 <- SMA(cs[, "Close"], n=100)
- sma200 <- SMA(cs[, "Close"], n=200)
- #
- # Chart nach links schieben (Zukunftsprojektion)
- cs_fut <- xts(, end(cs)+1:ahead)
- cs_fut$Open <- rep(last(dc100)$low, ahead)
- cs_fut$High <- rep(last(dc100)$low, ahead)
- cs_fut$Low <- rep(last(dc100)$low, ahead)
- cs_fut$Close <- rep(last(dc100)$low, ahead)
- cs_fut$Volume <- rep(0, ahead)
- cs_fut$Adjusted <- rep(last(dc100)$low, ahead)
- cs <- rbind(cs, cs_fut )
- remove(cs_fut)
- #
- # Vorhersagen vorberechnen
- # Beachte: Für die Vorhersage wird die komplette Zeitreihe
- # verwendet und nicht nur der Ausschnitt in "cs"
- #
- xp <- c()
- for(i in ords ) {
- # Herausschneiden der Samples
- model <- ar(Cl(head(z, (nrows - back) ) ), method="ols", aic=FALSE, order.max=i)
- df <- data.frame(predict(model, n.ahead=ahead+1)$pred)
- names(df) <- c( paste("pred", i, sep="") )
- pred_date <- time(last(z)-back)
- rownames(df) <- seq(pred_date, pred_date+ahead, 1)
- x <- as.xts(df, order.by=as.Date(rownames(df)))
- if (length(xp)==0) {
- xp <- x
- } else {
- xp <- merge(xp, x)
- }
- }
- #
- # Vorhersagen und Chartdaten zusammenführen
- full_cs <- rbind( rbind( rbind( cs$High, xp$pred200), xp$pred250), xp$pred300)
- #
- # Y-Range neu berechnen
- yr <- range(max( c( min( full_cs )*0.95, 0.001) ), max( full_cs )*1.05)
- #
- # Aufräumen
- remove(df)
- remove(x)
- remove(i)
- remove(full_cs)
- #
- # Chart mit den entsprechenden Indikatoren plotten
- #
- chartSeries(cs,
- name=symbolName,
- multi.col=TRUE,
- theme='white',
- log.scale=TRUE,
- TA=NULL,
- las=2,
- major.ticks='months',
- minor.ticks=FALSE,
- yrange=yr
- )
- addTA(sma100, col="blue", on=1)
- addTA(sma200, col="violet", on=1)
- addTA(dc100$high, col="green", on=1)
- addTA(dc200$high, col="darkgreen", on=1)
- addTA(dc100$low, col="red", on=1)
- addTA(dc200$low, col="darkred", on=1)
- addTA(xp$pred200, on=1, col="orange")
- addTA(xp$pred250, on=1, col="red")
- addTA(xp$pred300, on=1, col="darkred")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement