Last active
November 12, 2022 16:59
-
-
Save kumeS/71cc142fe533d232fa51c985c2a345a7 to your computer and use it in GitHub Desktop.
BTC trends plot + WMA
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_WMA_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$WMA14 <- WMA(Dat$Close, n = 14) | |
Dat$WMA14d <- Dat$Close - Dat$WMA14 | |
Dat$WMA14t <- Dat$Close > Dat$WMA14 | |
Dat$WMA25 <- WMA(Dat$Close, n = 25) | |
Dat$WMA25d <- Dat$Close - Dat$WMA25 | |
Dat$WMA25t <- Dat$Close > Dat$WMA25 | |
Dat$WMA50 <- WMA(Dat$Close, n = 50) | |
Dat$WMA50d <- Dat$Close - Dat$WMA50 | |
Dat$WMA50t <- Dat$Close > Dat$WMA50 | |
Dat$WMA100 <- WMA(Dat$Close, n = 100) | |
Dat$WMA100d <- Dat$Close - Dat$WMA100 | |
Dat$WMA100t <- Dat$Close > Dat$WMA100 | |
Dat$WMA150 <- WMA(Dat$Close, n = 150) | |
Dat$WMA150d <- Dat$Close - Dat$WMA150 | |
Dat$WMA150t <- Dat$Close > Dat$WMA150 | |
Dat$WMA200 <- WMA(Dat$Close, n = 200) | |
Dat$WMA200d <- Dat$Close - Dat$WMA200 | |
Dat$WMA200t <- Dat$Close > Dat$WMA200 | |
Dat$WMA250 <- WMA(Dat$Close, n = 250) | |
Dat$WMA250d <- Dat$Close - Dat$WMA250 | |
Dat$WMA250t <- Dat$Close > Dat$WMA250 | |
Dat$WMA300 <- WMA(Dat$Close, n = 300) | |
Dat$WMA300d <- Dat$Close - Dat$WMA300 | |
Dat$WMA300t <- Dat$Close > Dat$WMA300 | |
Dat$WMA400 <- WMA(Dat$Close, n = 400) | |
Dat$WMA400d <- Dat$Close - Dat$WMA400 | |
Dat$WMA400t <- Dat$Close > Dat$WMA400 | |
Dat$WMA500 <- WMA(Dat$Close, n = 500) | |
Dat$WMA500d <- Dat$Close - Dat$WMA500 | |
Dat$WMA500t <- Dat$Close > Dat$WMA500 | |
Dat$WMA600 <- WMA(Dat$Close, n = 600) | |
Dat$WMA600d <- Dat$Close - Dat$WMA600 | |
Dat$WMA600t <- Dat$Close > Dat$WMA600 | |
#Range | |
dd <- c(Dat$WMA14d[aa], Dat$WMA25d[aa], | |
Dat$WMA50d[aa], Dat$WMA100d[aa], | |
Dat$WMA150d[aa],Dat$WMA200d[aa], | |
Dat$WMA250d[aa],Dat$WMA300d[aa], | |
Dat$WMA400d[aa],Dat$WMA500d[aa], | |
Dat$WMA600d[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$WMA14d > 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$WMA14d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA14d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA25d > 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$WMA25d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA25d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA50d > 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$WMA50d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA50d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA100d > 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$WMA100d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA100d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA150d > 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$WMA150d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA150d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA200d > 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$WMA200d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA200d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA250d > 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$WMA250d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA250d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA300d > 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$WMA300d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA300d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA400d > 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$WMA400d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA400d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA500d > 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$WMA500d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA500d[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA600d > 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$WMA600d[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA600d[(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$WMA14t | |
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$WMA14[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA14[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA25t | |
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$WMA25[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA25[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA50t | |
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$WMA50[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA50[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA100t | |
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$WMA100[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA100[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA150t | |
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$WMA150[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA150[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA200t | |
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$WMA200[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA200[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA250t | |
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$WMA250[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA250[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA300t | |
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$WMA300[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA300[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA400t | |
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$WMA400[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA400[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA500t | |
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$WMA500[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA500[(n-1):n], type="l", col="#e87d72") | |
}}} | |
dd <- Dat$WMA600t | |
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$WMA600[(n-1):n], type="l", col="#56bc82") | |
}else{ | |
lines(a[(n-1):n], Dat$WMA600[(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