diff --git a/DESCRIPTION b/DESCRIPTION index 50bdd0a..6d7c9a8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 diff --git a/NAMESPACE b/NAMESPACE index 2a57c75..8bb2f5f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,5 +1,6 @@ # Generated by roxygen2: do not edit by hand +export(CryptoRSIheatmap) export(seasonPlot) import(dygraphs) import(grDevices) @@ -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) diff --git a/R/CryptoRSIheatmap.R b/R/CryptoRSIheatmap.R new file mode 100644 index 0000000..a7f8efc --- /dev/null +++ b/R/CryptoRSIheatmap.R @@ -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) +} + +} + diff --git a/README.html b/README.html new file mode 100644 index 0000000..a57470a --- /dev/null +++ b/README.html @@ -0,0 +1,498 @@ + + + + + + + + + + + + + +README + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+

seasonalityPlot +package

+

+

CRAN CRAN_latest_release_date CRAN CRAN downloads last month CRAN downloads last week

+

R package for Creating Seasonality Plots of Stock Prices and +Cryptocurrencies

+

GitHub/seasonalityPlot

+
+
+

Version

+

1.2.1: Update seasonPlot & Add new function +“CryptoRSIheatmap”.

+

1.1.1: Update Figures.

+

1.1.0: CRAN 3rd version.

+

1.0.1: CRAN 2nd version.

+

0.99.3: CRAN version.

+

0.99.1: Newly Published the GitHub.

+
+
+

Installation

+
+

install from CRAN

+
install.packages("seasonalityPlot", repos="http://cran.r-project.org")
+
+
+

install the latest from GitHub

+

type the code below in the R console window

+
install.packages("devtools", repos="http://cran.r-project.org")
+library(devtools)
+devtools::install_github("kumeS/seasonalityPlot")
+

or install from the source file with sh commands

+
git clone https://github.com/kumeS/seasonalityPlot.git
+R CMD INSTALL seasonalityPlot
+
+
+
+

Function

+ +
+
+

Usage of seasonPlot function

+
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)
+
+

+
+
#Plot an averaging seasonality of Dow Jones Industrial Average (^DJI) between 2012 and 2022.
+seasonPlot(Symbols="^DJI")
+
+

+
+
#Plot an averaging seasonality of NASDAQ Composite (^IXIC) between 2012 and 2022.
+seasonPlot(Symbols="^IXIC")
+
+

+
+
#Plot an averaging seasonality of Bitcoin (BTC-USD) between 2017 and 2022.
+seasonPlot(Symbols="BTC-USD")
+
+

+
+
#Plot an averaging seasonality of Ethereum (ETH-USD) between 2017 and 2022.
+seasonPlot(Symbols="ETH-USD")
+
+

+
+
#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)
+
+

+
+
+
+

License

+

Copyright (c) 2021 Satoshi Kume

+

Released under the Artistic +License 2.0.

+
+
+

Authors

+ +
+ + + + +
+ + + + + + + + + + + + + + + diff --git a/README.md b/README.md index 47ac12a..75b05a4 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# [seasonalityPlot package (version 1.1.1)](https://github.com/kumeS/seasonalityPlot) +# [seasonalityPlot package](https://github.com/kumeS/seasonalityPlot) [![CRAN](https://www.r-pkg.org/badges/version/seasonalityPlot)](https://CRAN.R-project.org/package=seasonalityPlot) @@ -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. @@ -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) ```
@@ -101,6 +106,16 @@ seasonPlot(Symbols="ETH-USD")
+```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) +``` + +
+ +
+ # License Copyright (c) 2021 Satoshi Kume diff --git a/inst/images/CryptoRSIheatmap.png b/inst/images/CryptoRSIheatmap.png new file mode 100644 index 0000000..f06bf53 Binary files /dev/null and b/inst/images/CryptoRSIheatmap.png differ diff --git a/inst/images/SeasonalityPlot_Adjusted_BTC-USD_StartYear2014_EndYear2023.png b/inst/images/SeasonalityPlot_Adjusted_BTC-USD_StartYear2014_EndYear2023.png new file mode 100644 index 0000000..ebec605 Binary files /dev/null and b/inst/images/SeasonalityPlot_Adjusted_BTC-USD_StartYear2014_EndYear2023.png differ diff --git a/man/CryptoRSIheatmap.Rd b/man/CryptoRSIheatmap.Rd new file mode 100644 index 0000000..0ebac3c --- /dev/null +++ b/man/CryptoRSIheatmap.Rd @@ -0,0 +1,58 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/CryptoRSIheatmap.R +\name{CryptoRSIheatmap} +\alias{CryptoRSIheatmap} +\title{CryptoRSI Heatmap Function} +\usage{ +CryptoRSIheatmap( + coin_num = 200, + useRank = 1000, + n = 21, + useRankPlot = TRUE, + OutputData = FALSE +) +} +\arguments{ +\item{coin_num}{An integer specifying the number of coins to display in the heatmap.} + +\item{useRank}{An integer defining the range within which coins are randomly +selected based on their market cap ranking.} + +\item{n}{An integer indicating the number of periods for calculating moving +averages in the RSI computation.} + +\item{useRankPlot}{A boolean that determines if the x-axis should plot ranks +instead of sequential numbers.} + +\item{OutputData}{A boolean that decides if the function should return the final +plot data table.} +} +\value{ +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. +} +\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. +} +\details{ +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. +} +\examples{ +\dontrun{ +CryptoRSIheatmap(coin_num = 200, useRank = 1000, n = 21, + useRankPlot = TRUE, OutputData = FALSE) +} + +} +\author{ +Satoshi Kume +} diff --git a/man/seasonPlot.Rd b/man/seasonPlot.Rd index 164b00c..3021289 100644 --- a/man/seasonPlot.Rd +++ b/man/seasonPlot.Rd @@ -8,6 +8,7 @@ seasonPlot( Symbols, StartYear = lubridate::year(Sys.Date()) - 11, EndYear = lubridate::year(Sys.Date()) - 1, + useAdjusted = FALSE, LineColor = 1, xlab = "Month", BackgroundMode = TRUE, @@ -29,6 +30,10 @@ BTC-USD (Bitcoin), ETH-USD (Ethereum), and XRP-USD (Ripple).} \item{EndYear}{a numeric of end year (Common Er). The default is the last year.} +\item{useAdjusted}{Choose whether to use the closing price adjusted for dividends. +If FALSE, normal close price is used. In the case of cryptocurrencies, +the useAdjusted option is expected to return the same result.} + \item{LineColor}{a numeric between 1 and 4; The value 1 is to select red1, the value 2 is to select blue1, the value 3 is to select green1, and the value 4 is to select black. @@ -38,7 +43,8 @@ When BackgroundMode is TRUE, this argument is disabled.} \item{BackgroundMode}{a logical; draw a background color by react.} -\item{alpha}{a numeric; The alpha parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).} +\item{alpha}{a numeric; The alpha parameter is a number between 0.0 (fully transparent) +and 1.0 (fully opaque).} \item{OutputData}{a logical; output as a data.frame type or not.} @@ -67,11 +73,12 @@ The positive and negative monthly changes are shown in green and red background } \examples{ ## Plot seasonality of NASDAQ Composite Index (^IXIC) -seasonPlot(Symbols = "^IXIC") +seasonPlot(Symbols = "^IXIC", useAdjusted = TRUE) ## Plot seasonality of Bitcoin (BTC-USD) seasonPlot(Symbols = "BTC-USD", StartYear=2015, EndYear=2020) + } \author{ Satoshi Kume