Skip to content

Instantly share code, notes, and snippets.

@kumeS
Last active November 9, 2022 15:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kumeS/cf479609eb312db4c7f3b3a002dda3df to your computer and use it in GitHub Desktop.
Save kumeS/cf479609eb312db4c7f3b3a002dda3df to your computer and use it in GitHub Desktop.
BTC Log plot + SMA
SMA_LogPlot <- function(Dat, term=c("2020-01-01", as.character(lubridate::today())),
SMA_periods=c(1200, 1400, 1600, 1800, 2000, 2200),
lag=10, xax = 0.01, yax = 0.999, xcex=0.9, ycex=0.8){
oldpar <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(oldpar))
if(!all(colnames(Dat) == c("Date", "Close", "Month"))){
stop("Incorrect format of Dat object")
}
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]))*0.1
m <- min(Dat$Close[aa])*0.9
par(family= "HiraKakuPro-W3", xpd =F)
layout(matrix(c(1), 1, 1, byrow = TRUE),
widths=c(1),
heights=c(1)
)
par(mar = c(4, 4, 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,
log="y",
xaxt = "n",
yaxt = "n",
main="Log price",
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 <- as.numeric(c(c(1,1.5,2,3,4, 5, 6, 8, 10) %o% 10^(-2:7)))
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)
a <- as.numeric(Dat$Date)
Dat$SMA1200 <- dd <- SMA(Dat$Close, n = SMA_periods[1])
Dat$SMA1200t <- Dat$Close < Dat$SMA1200
lines(a, dd, col="#709bf860", lwd=2)
Dat$SMA1400 <- dd <- SMA(Dat$Close, n = SMA_periods[2])
lines(a, dd, col="#709bf870", lwd=2)
Dat$SMA1600 <- dd <- SMA(Dat$Close, n = SMA_periods[3])
lines(a, dd, col="#709bf880", lwd=2)
Dat$SMA1800 <- dd <- SMA(Dat$Close, n = SMA_periods[4])
lines(a, dd, col="#709bf890", lwd=2)
Dat$SMA2200 <- dd <- SMA(Dat$Close, n = SMA_periods[6])
lines(a, dd, col="#709bf890", lwd=2)
Dat$SMA2000 <- dd <- SMA(Dat$Close, n = SMA_periods[5])
Dat$SMA2000_4t <- Dat$Close > Dat$SMA2000*4
lines(a, dd, col="#709bf890", lwd=2)
lines(a, dd*4, col="#56bc8260", lwd=2)
lines(a, dd*5, col="#56bc8270", lwd=2)
lines(a, dd*6, col="#56bc8280", lwd=2)
lines(a, dd*7, col="#56bc8290", lwd=2)
lines(a, dd*8, col="#56bc82", lwd=2)
#Line Plot
lines(Dat$Date, Dat$Close, type = "l", lwd = 1, col="grey")
dd <- Dat$SMA1200t
for(n in 2:length(dd)){
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$Close[(n-1):n], type="l", col="#709bf8")
}}}
dd <- Dat$SMA2000_4t
for(n in 2:length(dd)){
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$Close[(n-1):n], type="l", col="#56bc82")
}}}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment