Last active
November 12, 2022 17:07
-
-
Save kumeS/f4813db3049e95db9dd53d528afddf72 to your computer and use it in GitHub Desktop.
BTC treands plot + SMA
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
trends_SMA_plot <- function(Dat, term=c("2020-01-01", as.character(lubridate::today())), | |
lag=10, xax = 0.1, yax = 0.999, xcex=0.9, ycex=0.8, rou=0, lcex=0.35, | |
M1=0.15, m1=0.9, legend_xy="topright"){ | |
oldpar <- graphics::par(no.readonly = TRUE) | |
on.exit(graphics::par(oldpar)) | |
aa <- c(sum(Dat$Date <= term[1]):sum(Dat$Date <= term[2])) | |
M <- max(Dat$Close[aa]) + abs(max(Dat$Close[aa]) - min(Dat$Close[aa]))*M1 | |
m <- min(Dat$Close[aa])*m1 | |
a <- as.numeric(Dat$Date) | |
#head(Dat) | |
Dat$SMA14 <- SMA(Dat$Close, n = 14) | |
Dat$SMA14d <- Dat$Close - Dat$SMA14 | |
Dat$SMA14t <- Dat$Close > Dat$SMA14 | |
Dat$SMA25 <- SMA(Dat$Close, n = 25) | |
Dat$SMA25d <- Dat$Close - Dat$SMA25 | |
Dat$SMA25t <- Dat$Close > Dat$SMA25 | |
Dat$SMA50 <- SMA(Dat$Close, n = 50) | |
Dat$SMA50d <- Dat$Close - Dat$SMA50 | |
Dat$SMA50t <- Dat$Close > Dat$SMA50 | |
Dat$SMA100 <- SMA(Dat$Close, n = 100) | |
Dat$SMA100d <- Dat$Close - Dat$SMA100 | |
Dat$SMA100t <- Dat$Close > Dat$SMA100 | |
Dat$SMA150 <- SMA(Dat$Close, n = 150) | |
Dat$SMA150d <- Dat$Close - Dat$SMA150 | |
Dat$SMA150t <- Dat$Close > Dat$SMA150 | |
Dat$SMA200 <- SMA(Dat$Close, n = 200) | |
Dat$SMA200d <- Dat$Close - Dat$SMA200 | |
Dat$SMA200t <- Dat$Close > Dat$SMA200 | |
Dat$SMA250 <- SMA(Dat$Close, n = 250) | |
Dat$SMA250d <- Dat$Close - Dat$SMA250 | |
Dat$SMA250t <- Dat$Close > Dat$SMA250 | |
Dat$SMA300 <- SMA(Dat$Close, n = 300) | |
Dat$SMA300d <- Dat$Close - Dat$SMA300 | |
Dat$SMA300t <- Dat$Close > Dat$SMA300 | |
Dat$SMA400 <- SMA(Dat$Close, n = 400) | |
Dat$SMA400d <- Dat$Close - Dat$SMA400 | |
Dat$SMA400t <- Dat$Close > Dat$SMA400 | |
Dat$SMA500 <- SMA(Dat$Close, n = 500) | |
Dat$SMA500d <- Dat$Close - Dat$SMA500 | |
Dat$SMA500t <- Dat$Close > Dat$SMA500 | |
Dat$SMA600 <- SMA(Dat$Close, n = 600) | |
Dat$SMA600d <- Dat$Close - Dat$SMA600 | |
Dat$SMA600t <- Dat$Close > Dat$SMA600 | |
#Range | |
dd <- c(Dat$SMA14d[aa], Dat$SMA25d[aa], | |
Dat$SMA50d[aa], Dat$SMA100d[aa], | |
Dat$SMA150d[aa],Dat$SMA200d[aa], | |
Dat$SMA250d[aa],Dat$SMA300d[aa], | |
Dat$SMA400d[aa],Dat$SMA500d[aa], | |
Dat$SMA600d[aa]) | |
dd01 <- dd[!is.na(dd)] | |
par(family= "HiraKakuPro-W3", xpd =F) | |
layout(matrix(c(1,2), 2, 1, byrow = TRUE), | |
widths=c(1,1), | |
heights=c(1,4)) | |
par(mar = c(0.2, 4, 1, 2)) | |
plot(Dat$Date, Dat$Close, | |
ylim = c(min(dd01)*1.05, max(dd01)*1.05), | |
xlim = c(min(as.numeric(Dat$Date)[aa]), | |
max(as.numeric(Dat$Date)[aa])+as.numeric(lag)), | |
xlab = "", ylab = "", | |
type = "l", col = "white", | |
lwd = 1, | |
xaxt = "n", | |
yaxt = "n", | |
xaxs="i", yaxs="i") | |
dd <- Dat$SMA14d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA14d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA14d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA25d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA25d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA25d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA50d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA50d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA50d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA100d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA100d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA100d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA150d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA150d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA150d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA200d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA200d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA200d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA250d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA250d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA250d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA300d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA300d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA300d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA400d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA400d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA400d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA500d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA500d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA500d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA600d > 0 | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA600d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA600d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
abline(h=0) | |
par(mar = c(4, 4, 0.2, 2)) | |
plot(Dat$Date, Dat$Close, | |
ylim = c(m, M), | |
xlim = c(min(as.numeric(Dat$Date)[aa]), | |
max(as.numeric(Dat$Date)[aa])+as.numeric(lag)), | |
xlab = "", ylab = "", | |
type = "l", col = "white", | |
lwd = 1, | |
xaxt = "n", | |
yaxt = "n", | |
main="", | |
xaxs="i", yaxs="i") | |
#X-axis | |
par(family= "HiraKakuPro-W3", xpd =F) | |
lab <- seq.Date(as.Date(term[1]), as.Date(term[2]), length.out = 5, format="%Y年%B") | |
axis.Date(1, at=lab, labels = FALSE, format="%Y年%B", cex=0.7) | |
text(lab, m - (M-m)*xax, | |
labels = format(lab, "%Y年%B"), srt = 35, pos = 1, xpd = TRUE, cex=xcex) | |
#Y-axis | |
par(family= "HiraKakuPro-W3", xpd =F) | |
lab <- round(seq(signif(m, 1), signif(M, 1), length.out = 7),rou) | |
lab <- lab[lab > m] | |
lab <- lab[lab < M] | |
axis(2, at = lab, labels = F) | |
text(par("usr")[1]*yax, lab, labels = lab, srt = 0, pos = 2, xpd = TRUE, cex=ycex) | |
#Plot | |
lines(Dat$Date, Dat$Close, type = "l", lwd = 1, col="grey") | |
dd <- Dat$SMA14t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA14[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA14[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA25t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA25[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA25[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA50t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA50[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA50[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA100t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA100[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA100[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA150t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA150[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA150[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA200t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA200[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA200[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA250t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA250[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA250[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA300t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA300[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA300[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA400t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA400[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA400[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA500t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA500[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA500[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$SMA600t | |
for(n in 2:length(dd)){ | |
#n <- 2 | |
if(!any(c(is.na(dd[n-1]), is.na(dd[n])))){ | |
if(any(c(dd[n-1], dd[n]))){ | |
lines(a[(n-1):n], Dat$SMA600[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$SMA600[(n-1):n], type="l", col="#e87d72") | |
}}} | |
#legend | |
dat0 <- Dat[aa[length(aa)],] | |
dat1 <- dat0[,grep("d$", colnames(dat0))] | |
dat2 <- dat1[,order(as.numeric(dat1), decreasing = F)] | |
legend(legend_xy, | |
legend=paste0(colnames(dat2), " "),cex=lcex, pch=NA, | |
text.col = as.character(ifelse(dat2 < 0, "#e87d72", "#56bc82")), | |
x.intersp = 0.25) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment