Skip to content

Commit

Permalink
3rd ver
Browse files Browse the repository at this point in the history
  • Loading branch information
kumeS committed Feb 19, 2024
1 parent 9bb1de4 commit f065194
Show file tree
Hide file tree
Showing 9 changed files with 761 additions and 6 deletions.
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ Imports: magrittr,
grDevices,
graphics,
zoo,
lubridate
lubridate,
crypto2,
TTR,
assertthat
Suggests: testthat
License: Artistic-2.0
Encoding: UTF-8
LazyData: true
URL: https://github.com/kumeS/seasonalityPlot, https://kumes.github.io/seasonalityPlot/
BugReports: https://github.com/kumeS/seasonalityPlot/issues
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

export(CryptoRSIheatmap)
export(seasonPlot)
import(dygraphs)
import(grDevices)
Expand All @@ -8,7 +9,12 @@ import(htmltools)
import(magrittr)
import(plotrix)
import(quantmod)
importFrom(TTR,RSI)
importFrom(assertthat,assert_that)
importFrom(crypto2,crypto_history)
importFrom(crypto2,crypto_list)
importFrom(lubridate,year)
importFrom(magrittr,"%>%")
importFrom(stats,median)
importFrom(utils,askYesNo)
importFrom(zoo,index)
168 changes: 168 additions & 0 deletions R/CryptoRSIheatmap.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
#' CryptoRSI Heatmap
#'
#' Generates a heatmap of the Relative Strength Index (RSI) for a randomly selected
#' subset of cryptocurrencies. This function leverages the `crypto2` and `TTR`
#' packages to fetch cryptocurrency data and calculate RSI values, respectively.
#' The heatmap visualizes RSI values to identify potential overbought or oversold
#' conditions in the crypto market.
#'
#' @title CryptoRSI Heatmap Function
#' @description This function provides a heatmap visualization of RSI values for
#' a specified number of cryptocurrencies. Selected randomly based on their market
#' cap ranking, it aims to offer insights into the current market sentiment.
#' @param coin_num An integer specifying the number of coins to display in the heatmap.
#' @param useRank An integer defining the range within which coins are randomly
#' selected based on their market cap ranking.
#' @param n An integer indicating the number of periods for calculating moving
#' averages in the RSI computation.
#' @param useRankPlot A boolean that determines if the x-axis should plot ranks
#' instead of sequential numbers.
#' @param OutputData A boolean that decides if the function should return the final
#' plot data table.
#'
#' @importFrom crypto2 crypto_list crypto_history
#' @importFrom TTR RSI
#' @importFrom assertthat assert_that
#' @importFrom stats median
#'
#' @return If `OutputData` is TRUE, returns a data frame with symbols,
#' ranks (or sequential numbers), RSI values, and colors for plotting.
#' Otherwise, displays a heatmap plot.
#'
#' @export CryptoRSIheatmap
#' @author Satoshi Kume
#'
#' @examples
#' \dontrun{
#' CryptoRSIheatmap(coin_num = 200, useRank = 1000, n = 21,
#' useRankPlot = TRUE, OutputData = FALSE)
#'}
#'

CryptoRSIheatmap <- function(coin_num = 200, useRank = 1000, n = 21, useRankPlot = TRUE,
OutputData = FALSE){

assertthat::assert_that(coin_num < useRank, msg = "coin_num must be less than useRank")

cat("Obtaining the crypto_list... \n")
coins <- crypto2::crypto_list(only_active=TRUE)

#str(coins)
coins <- coins[order(coins$rank),]
coins <- coins[1:useRank,]

res <- sample(nrow(coins), size = coin_num, replace = FALSE)
coins.res <- coins[res[order(res)],]

cat("Obtaining the crypto_history... \n")
coin_hist <- crypto2::crypto_history(coins.res,
start_date=gsub("-", "", lubridate::date(Sys.Date())-n-10),
end_date=gsub("-", "", lubridate::date(Sys.Date())-1))
coin.df <- data.frame(coin_hist)
#head(coin.df); str(coin.df)

results <- data.frame(matrix(NA, nrow=coin_num, ncol=4))
results$X1 <- coins.res$symbol
results$X2 <- seq_len(coin_num)
results$X3 <- coins.res$rank

for(k in seq_len(nrow(coins.res))){
#k <- 1
a <- coins.res$symbol[k]
b <- coin.df[coin.df$symbol == a,]
if(nrow(b) >= n+5){
d <- round(TTR::RSI(as.numeric(b$close), n = n), 2)
results[grepl(coins.res$symbol[k],results$X1),4] <- d[length(d)]
}
}

#Remove NA
#table(is.na(results$X4))
results <- results[!is.na(results$X4),]
colnames(results) <- c("Symbol", "No", "Rank", "ClosePrice")

if(useRankPlot){
results.u <- results[,-2]
XYlab <- c("Rank", "ClosePrice")
}else{
results.u <- results[,-3]
XYlab <- c("No", "ClosePrice")
}
colnames(results.u) <- c("Symbol", "X", "Y")

#Setting
UpperLimit <- 85
OVERBOUGHT <- 70
STRONG <- 60
NEUTRAL <- 50
WEAK <- 40
OVEZRSOLD <- 30
LowerLimit <- 15
x1 <- 0
x2 <- max(results.u$X)+1
p <- 3

#Normalized
results.u$Y[results.u$Y > UpperLimit] <- UpperLimit
results.u$Y[results.u$Y < LowerLimit] <- LowerLimit

#Color
results.u$COL <- NA
#head(results.u)
results.u$COL <- "#FF000095"
results.u$COL[results.u$Y < OVERBOUGHT] <- "#FF000040"
results.u$COL[results.u$Y < STRONG] <- "#7E7E7E30"
results.u$COL[results.u$Y < WEAK] <- "#0072FF40"
results.u$COL[results.u$Y < OVEZRSOLD] <- "#0072FF95"

#Plot
oldpar <- graphics::par(no.readonly = TRUE)
on.exit(graphics::par(oldpar))
pp <- round(x2*0.01,0)

par(family= "HiraKakuPro-W3", xpd =F, mar=c(5,4,3,2))
plot(results.u$X, results.u$Y,
ylim = c(LowerLimit-p, UpperLimit+p),
xlim = c(x1, x2+pp),
xlab = XYlab[1], ylab = XYlab[2],
type = "b", col = "white",
#xaxt = "n",
yaxt = "n",
cex.lab=1.25,
main = paste0("CryptoRSI Heatmap: ", nrow(results.u), " coins"),
xaxs="i", yaxs="i")

#X-axis: No-setting
#Y-axis
lab <- c(LowerLimit, OVEZRSOLD, WEAK, NEUTRAL, STRONG, OVERBOUGHT, UpperLimit)
axis(2, at = lab, labels = F)
text(par("usr")[1]-5, lab, labels = lab, srt = 0, pos = 2, xpd = TRUE, cex=1)

#Polygon zone
polygon( c(x1:(x2+pp), rev(x1:(x2+pp))),
c(rep(LowerLimit-p, length(x1:(x2+pp))), rep(OVEZRSOLD, length(x1:(x2+pp)))),
col="#0072FF30", border = NA)
polygon( c(x1:(x2+pp), rev(x1:(x2+pp))),
c(rep(OVEZRSOLD, length(x1:(x2+pp))), rep(WEAK, length(x1:(x2+pp)))),
col="#0072FF15", border = NA)
polygon( c(x1:(x2+pp), rev(x1:(x2+pp))),
c(rep(STRONG, length(x1:(x2+pp))), rep(OVERBOUGHT, length(x1:(x2+pp)))),
col="#FF000010", border = NA)
polygon( c(x1:(x2+pp), rev(x1:(x2+pp))),
c(rep(OVERBOUGHT, length(x1:(x2+pp))), rep(UpperLimit+p, length(x1:(x2+pp)))),
col="#FF000020", border = NA)

#Median line
abline(h=stats::median(results.u$Y), lwd=2, lty=2, col="grey")

#Plot points
points(results.u$X, results.u$Y,
col=results.u$COL, bg=results.u$COL, pch=21, cex=1.25)

#Output
if(OutputData){
return(results.u)
}

}

498 changes: 498 additions & 0 deletions README.html

Large diffs are not rendered by default.

19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [seasonalityPlot package (version 1.1.1)](https://github.com/kumeS/seasonalityPlot)
# [seasonalityPlot package](https://github.com/kumeS/seasonalityPlot)
<img src="https://github.com/kumeS/seasonalityPlot/blob/main/inst/images/hexSticker_seasonalityPlot.png" align="right" height="139" />

[![CRAN](https://www.r-pkg.org/badges/version/seasonalityPlot)](https://CRAN.R-project.org/package=seasonalityPlot)
Expand All @@ -13,7 +13,9 @@ R package for Creating Seasonality Plots of Stock Prices and Cryptocurrencies

# Version

1.1.1: Update Figures
1.2.1: Update seasonPlot & Add new function "CryptoRSIheatmap".

1.1.1: Update Figures.

1.1.0: CRAN 3rd version.

Expand Down Expand Up @@ -59,6 +61,9 @@ library(seasonalityPlot)

#Plot an averaging seasonality of SPDR S&P500 ETF (SPY) between 2012 and 2022.
seasonPlot(Symbols="SPY")

#useAdjusted = TRUE
seasonPlot(Symbols="SPY", useAdjusted = TRUE)
```

<div style="text-align: center">
Expand Down Expand Up @@ -101,6 +106,16 @@ seasonPlot(Symbols="ETH-USD")
<img src="inst/images/SeasonalityPlot_ETH-USD_StartYear2017_EndYear2022.png" width="750px">
</div>

```r
#CryptoRSI Heatmap Function provides a heatmap visualization of RSI values for a specified number of cryptocurrencies.

CryptoRSIheatmap(coin_num = 200, useRank = 1000, n = 21, useRankPlot = TRUE, OutputData = FALSE)
```

<div style="text-align: center">
<img src="inst/images/CryptoRSIheatmap.png" width="750px">
</div>

# License

Copyright (c) 2021 Satoshi Kume
Expand Down
Binary file added inst/images/CryptoRSIheatmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions man/CryptoRSIheatmap.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions man/seasonPlot.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f065194

Please sign in to comment.