--豢豌:
http://henryquant.blogspot.com/2018/11/r.html
library(lubridate)
library(httr)
library(rvest)
library(xts)
tick = "069500"
url = paste0("https://fchart.stock.naver.com/sise.nhn?symbol=",tick,"&timeframe=day&count=10000&requestType=0")
data = GET(url) %>%
read_html %>%
html_nodes("item") %>%
html_attr("data") %>%
strsplit("\\|")
data = lapply(data, function(x) {
x[c(1, 5)] %>% t() %>% data.frame()
})
data = do.call(rbind, data)
data[,2] = as.numeric(as.character(data[,2]))
KODEX200 <- xts(data[2], order.by=as.Date(ymd(data[,1])))
colnames(KODEX200) <-"KODEX200.Adjusted"
head(KODEX200)
if(1==1){
ticker <- c("SPY", "IEF")
#ticker_names <- c("KOSPI200", "S&P500", "IEF", "IEF.KO", "Dallar", "SHY.KO")
#symbols <- ticker_names
symbols <- ticker
getSymbols(symbols, from="2000-01-01", to=today())
getSymbols('DEXKOUS', src='FRED')
#KODEX200 <- tqk_get(x="069500", from="2000-01-01", to=today())
tmp <- data.frame(Ad=c(coredata(DEXKOUS)), dt=index(DEXKOUS))
DEXKOUS <- xts(tmp$Ad, order.by=tmp$dt)
colnames(DEXKOUS) <-"DEXKOUS.Adjusted"
#KODEX200 <- xts(KODEX200$adjusted, order.by=KODEX200$date)
#colnames(KODEX200) <-"KODEX200.Adjusted"
#head(KODEX200)
tick = "069500"
url = paste0("https://fchart.stock.naver.com/sise.nhn?symbol=",tick,"&timeframe=day&count=10000&requestType=0")
data = GET(url) %>%
read_html %>%
html_nodes("item") %>%
html_attr("data") %>%
strsplit("\\|")
data = lapply(data, function(x) {
x[c(1, 5)] %>% t() %>% data.frame()
})
data = do.call(rbind, data)
data[,2] = as.numeric(as.character(data[,2]))
KODEX200 <- xts(data[2], order.by=as.Date(ymd(data[,1])))
colnames(KODEX200) <-"KODEX200.Adjusted"
head(KODEX200)
ticker <- c("KODEX200", ticker, "DEXKOUS")
symbols <- ticker
ticker_names <- ticker
#getSymbols(symbols, from= today()-365*2, to = today(), src = 'yahoo')
prices <- do.call(cbind, lapply(symbols, function(x) Ad(get(x))))
prices <- na.omit(prices)
names(prices) <- symbols
head(prices);tail(prices,20)
tmp <- prices
tmp$SPY <- tmp$SPY * tmp$DEXKOUS
tmp$IEF <- tmp$IEF * tmp$DEXKOUS
head(tmp)
#prices2 <- prices
#prices <- window(prices, start=as.Date("2019-01-01"), end=today())
rets <- Return.calculate(tmp) %>% na.omit()
names(rets) <- symbols
head(rets)
stock_cnt <- length(symbols)
# rets鐃緒申 prices鐃緒申 鐃緒申促鐃緒申 鐃緒申鐃緒申鐃緒申.
prices <- window(prices, start=index(head(rets, 1)), end=index(tail(rets, 1)))
#head(rets);head(prices);tail(prices)
ep = endpoints(prices, on = 'months')
lookback = 13
wt_zero = rep(0, stock_cnt) %>% setNames(colnames(rets))
}