Last active
December 17, 2022 15:13
-
-
Save kumeS/e8dca57f2ba101b2ef9714ce59400460 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#パッケージ・インストール | |
pack <- c("rvest", "quantmod", "magrittr", "tidyr", "tidyverse", "plotly", "gapminder", "dygraphs", "htmltools") | |
install.packages(pack[!(pack %in% unique(rownames(installed.packages())))]) | |
#ロード | |
for(n in 1:length(pack)){ eval(parse(text = paste0("library(", pack[n], ")"))) }; rm("n", "pack") | |
#Get list | |
getSP100_list <- function(nodes1=3){ | |
SP100_url <- "https://en.wikipedia.org/wiki/S%26P_100" | |
#browseURL(SP100_url) | |
SP100 <- SP100_url %>% | |
read_html() %>% | |
html_nodes("table") %>% | |
.[[nodes1]] %>% | |
html_table | |
SP100.d <- data.frame(SP100[,c("Name", "Symbol", "Sector")]) | |
colnames(SP100.d) <- c("Company", "Ticker", "Sector") | |
return(SP100.d) | |
} | |
SP100_ChartData <- function(Dat, term=c("2022-01-01", "2022-12-31")){ | |
#Dat=SP100List; term=c("2022-01-01", "2022-12-31") | |
Dat <- Dat[!grepl("GOOG$", Dat$Ticker),] | |
Dat$Ticker <- sub("[.]B$", "-B", Dat$Ticker) | |
Ticker <- Dat$Ticker | |
#Tickerリストの定義 | |
List <- as.character(Ticker) | |
#実行 | |
quantmod::getSymbols(List, src = "yahoo", verbose = T, from = term[1], to=term[2]) | |
#quantmod::getSymbols("BRK-B") | |
#空のデータフレームの作成 | |
m <- max(c(nrow(get(List[1])), nrow(get(List[2])), nrow(get(List[3])), nrow(get(List[4])))) | |
stock <- data.frame(matrix(NA, nrow=m, ncol=length(List))) | |
rownames(stock) <- rownames(data.frame(get(List[1]))) | |
colnames(stock) <- List | |
#head(stock) | |
#データの代入 | |
for(n in seq_len(length(List))){ | |
a <- as.numeric(get(List[n])[,4]) | |
if(length(a) == m){ | |
stock[,n] <- a | |
} | |
} | |
#変数削除 | |
rm(list = List) | |
#NA列を除く | |
stock0 <- stock[,apply(stock, 2, function(x){sum(is.na(x))}) == 0] | |
List01 <- Dat[apply(stock, 2, function(x){sum(is.na(x))}) == 0,] | |
#データ取得完了 | |
#head(stock0) | |
#head(List01) | |
#年初時の株価を 100 に補正 | |
stock.c <- stock0 | |
for(n in 1:ncol(stock0)){ | |
stock.c[,n] <- as.numeric(stock0[,n])/as.numeric(stock0[1,n])*100 | |
} | |
#head(stock.c) | |
#データの行列を入れ替える | |
stock.t <- t(stock.c) | |
#head(stock.t) | |
#stock01 <- data.frame(Ticker=List01$Company, Sector=List01$Sector, stock.t) | |
#stock01 <- data.frame(Ticker=paste0(List01$Company, "(", List01$Ticker, ")"), Sector=List01$Sector, stock.t) | |
stock01 <- data.frame(Company=List01$Company, Ticker=List01$Ticker, Sector=List01$Sector, stock.t) | |
rownames(stock01) <- 1:nrow(stock01) | |
#head(stock01) | |
stock02 <- stock01 | |
stock03 <- gather(stock02, key="date", value="close", -c(Company, Ticker, Sector)) | |
stock03$date <- sub("X", "", stock03$date) | |
stock03$date <- gsub("\\.", "/", stock03$date) | |
stock03$date <- paste0(stock03$date, "-16-00-00") | |
#head(stock03) | |
#日時列に変える | |
stock03$date <- as.Date(stock03$date) | |
#株価の変動幅から、カラーを決める | |
#head(stock03) | |
stock03$dclose <- stock03$close - 100 | |
stock03$dclose2 <- NA | |
colfunc <- grDevices::colorRampPalette(c("brown3", "white", "darkgreen")) | |
a <- colfunc(18) | |
b1 <- seq(min(stock03$dclose)-10, 0, length.out=9) | |
b2 <- seq(0, max(stock03$dclose)+10, length.out=9) | |
b3 <- c(b1, b2[-1]) | |
for(n in length(b3):1){ | |
stock03$dclose2[stock03$dclose < b3[n]] <- a[n] | |
} | |
#ranking | |
stock03$ranking <- NA | |
#head(stock03) | |
ab <- unique(stock03$date) | |
for(l in 1:length(ab)){ | |
#l <- 1 | |
if(l == 1){ | |
bc <- stock03$date == ab[l] | |
stock03[bc,]$ranking <- 1:nrow(List01) | |
}else{ | |
#l <- 2 | |
bc <- stock03$date == ab[l] | |
cd <- order(stock03[bc,]$close, decreasing = T) | |
for(k in 1:nrow(List01)){ | |
stock03[bc,]$ranking[cd[k]] <- k | |
} | |
} | |
} | |
return(stock03) | |
#head(stock03) | |
#table(stock03$ranking) | |
#table(stock03$date) | |
} | |
SP100_ChartData_mod <- function(Dat, dig=1, top=100){ | |
#Dat=ChartData; dig=1; top=100 | |
Dat$CompanyTicker <- paste0(Dat$Company, " (", Dat$Ticker, ")") | |
Dat$CompanySector <- paste0(Dat$Company, " (", Dat$Sector, ")") | |
Dat <- Dat[order(Dat$CompanyTicker, decreasing = F),] | |
Dat$close <- round(Dat$close, digits=dig) | |
Dat$dclose <- round(Dat$dclose, digits=dig) | |
if(top > 0){ | |
Dat0 <- Dat[Dat$ranking <= top,] | |
}else{ | |
Dat0 <- Dat[c(Dat$ranking >= max(Dat$ranking) + top),] | |
} | |
#head(Dat) | |
Dat0 <- Dat0[,c("Company", "Ticker", "Sector", "CompanyTicker","CompanySector", | |
"date", "close", "dclose", "dclose2", "ranking")] | |
rownames(Dat0) <- 1:nrow(Dat0) | |
#head(Dat0) | |
return(Dat0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment