diff --git a/NAMESPACE b/NAMESPACE index 113a182..e5684c6 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,7 +4,6 @@ export(bb) export(cite_package) export(ema) export(fib) -export(ichimoku_cloud) export(macd) export(mom) export(monte_carlo) diff --git a/R/RcppExports.R b/R/RcppExports.R index c46a0d6..ba93149 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -33,20 +33,6 @@ fib <- function(high, low) { .Call(`_dmplot_fib`, high, low) } -#' Ichimoku Cloud -#' Calculate the Ichimoku Cloud for a given price series -#' @param high The high prices -#' @param low The low prices -#' @param close The closing prices -#' @param tenkan_period The period for the Tenkan-sen (Conversion Line) -#' @param kijun_period The period for the Kijun-sen (Base Line) -#' @param senkou_period The period for the Senkou Span B (Leading Span B) -#' @return A list containing the Ichimoku Cloud components -#' @export -ichimoku_cloud <- function(high, low, close, tenkan_period = 9L, kijun_period = 26L, senkou_period = 52L) { - .Call(`_dmplot_ichimoku_cloud`, high, low, close, tenkan_period, kijun_period, senkou_period) -} - #' Moving Average Convergence Divergence (MACD) #' #' @param price A numeric vector of prices diff --git a/README.Rmd b/README.Rmd index 5ed9779..e344e79 100644 --- a/README.Rmd +++ b/README.Rmd @@ -2,7 +2,7 @@ output: github_document --- -```{r, include = FALSE} +```{r, include=FALSE} knitr::opts_chunk$set( warning = FALSE, message = FALSE, @@ -10,7 +10,7 @@ knitr::opts_chunk$set( fig.align = "center", fig.width = 12, fig.height = 10, - dpi = 100, + dpi = 150, collapse = TRUE, comment = "#>" ) @@ -56,7 +56,7 @@ box::use(dmplot) ### Get financial data -[`kucoin`](https://github.com/dereckmezquita/kucoin) is a package for interacting with the [`kucoin`](https://www.kucoin.com) api. You can use any source of financial data as long as you pass the variables to the `ggplot2` stat correctly. +[`kucoin`](https://github.com/dereckmezquita/kucoin) is a package for interacting with the [kucoin.com API](https://www.kucoin.com) api. You can use any source of financial data as long as you pass the variables to the `ggplot2` stat correctly. ```{r get-financial-data} ticker <- "BTC/USDT" diff --git a/man/ichimoku_cloud.Rd b/man/ichimoku_cloud.Rd deleted file mode 100644 index da443d6..0000000 --- a/man/ichimoku_cloud.Rd +++ /dev/null @@ -1,36 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/RcppExports.R -\name{ichimoku_cloud} -\alias{ichimoku_cloud} -\title{Ichimoku Cloud -Calculate the Ichimoku Cloud for a given price series} -\usage{ -ichimoku_cloud( - high, - low, - close, - tenkan_period = 9L, - kijun_period = 26L, - senkou_period = 52L -) -} -\arguments{ -\item{high}{The high prices} - -\item{low}{The low prices} - -\item{close}{The closing prices} - -\item{tenkan_period}{The period for the Tenkan-sen (Conversion Line)} - -\item{kijun_period}{The period for the Kijun-sen (Base Line)} - -\item{senkou_period}{The period for the Senkou Span B (Leading Span B)} -} -\value{ -A list containing the Ichimoku Cloud components -} -\description{ -Ichimoku Cloud -Calculate the Ichimoku Cloud for a given price series -} diff --git a/src/RcppExports.cpp b/src/RcppExports.cpp index 186e254..75f0013 100644 --- a/src/RcppExports.cpp +++ b/src/RcppExports.cpp @@ -48,39 +48,6 @@ BEGIN_RCPP return rcpp_result_gen; END_RCPP } -// ichimoku_cloud -Rcpp::List ichimoku_cloud(NumericVector high, NumericVector low, NumericVector close, int tenkan_period, int kijun_period, int senkou_period); -RcppExport SEXP _dmplot_ichimoku_cloud(SEXP highSEXP, SEXP lowSEXP, SEXP closeSEXP, SEXP tenkan_periodSEXP, SEXP kijun_periodSEXP, SEXP senkou_periodSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< NumericVector >::type high(highSEXP); - Rcpp::traits::input_parameter< NumericVector >::type low(lowSEXP); - Rcpp::traits::input_parameter< NumericVector >::type close(closeSEXP); - Rcpp::traits::input_parameter< int >::type tenkan_period(tenkan_periodSEXP); - Rcpp::traits::input_parameter< int >::type kijun_period(kijun_periodSEXP); - Rcpp::traits::input_parameter< int >::type senkou_period(senkou_periodSEXP); - rcpp_result_gen = Rcpp::wrap(ichimoku_cloud(high, low, close, tenkan_period, kijun_period, senkou_period)); - return rcpp_result_gen; -END_RCPP -} -// ichimoku_cloud2 -List ichimoku_cloud2(std::vector high, std::vector low, std::vector close, int tenkan_period, int kijun_period, int senkou_span_b_period, int chikou_shift); -RcppExport SEXP _dmplot_ichimoku_cloud2(SEXP highSEXP, SEXP lowSEXP, SEXP closeSEXP, SEXP tenkan_periodSEXP, SEXP kijun_periodSEXP, SEXP senkou_span_b_periodSEXP, SEXP chikou_shiftSEXP) { -BEGIN_RCPP - Rcpp::RObject rcpp_result_gen; - Rcpp::RNGScope rcpp_rngScope_gen; - Rcpp::traits::input_parameter< std::vector >::type high(highSEXP); - Rcpp::traits::input_parameter< std::vector >::type low(lowSEXP); - Rcpp::traits::input_parameter< std::vector >::type close(closeSEXP); - Rcpp::traits::input_parameter< int >::type tenkan_period(tenkan_periodSEXP); - Rcpp::traits::input_parameter< int >::type kijun_period(kijun_periodSEXP); - Rcpp::traits::input_parameter< int >::type senkou_span_b_period(senkou_span_b_periodSEXP); - Rcpp::traits::input_parameter< int >::type chikou_shift(chikou_shiftSEXP); - rcpp_result_gen = Rcpp::wrap(ichimoku_cloud2(high, low, close, tenkan_period, kijun_period, senkou_span_b_period, chikou_shift)); - return rcpp_result_gen; -END_RCPP -} // macd List macd(std::vector price, int s, int l, int k, bool percent); RcppExport SEXP _dmplot_macd(SEXP priceSEXP, SEXP sSEXP, SEXP lSEXP, SEXP kSEXP, SEXP percentSEXP) { @@ -165,8 +132,6 @@ static const R_CallMethodDef CallEntries[] = { {"_dmplot_bb", (DL_FUNC) &_dmplot_bb, 3}, {"_dmplot_ema", (DL_FUNC) &_dmplot_ema, 3}, {"_dmplot_fib", (DL_FUNC) &_dmplot_fib, 2}, - {"_dmplot_ichimoku_cloud", (DL_FUNC) &_dmplot_ichimoku_cloud, 6}, - {"_dmplot_ichimoku_cloud2", (DL_FUNC) &_dmplot_ichimoku_cloud2, 7}, {"_dmplot_macd", (DL_FUNC) &_dmplot_macd, 5}, {"_dmplot_mom", (DL_FUNC) &_dmplot_mom, 2}, {"_dmplot_monte_carlo", (DL_FUNC) &_dmplot_monte_carlo, 4}, diff --git a/src/ichimoku-cloud.cpp b/src/ichimoku-cloud.cpp index 9c2ca10..026ddb5 100644 --- a/src/ichimoku-cloud.cpp +++ b/src/ichimoku-cloud.cpp @@ -27,8 +27,6 @@ std::vector calculate_line(const NumericVector& high, const NumericVecto //' @param kijun_period The period for the Kijun-sen (Base Line) //' @param senkou_period The period for the Senkou Span B (Leading Span B) //' @return A list containing the Ichimoku Cloud components -//' @export -// [[Rcpp::export]] Rcpp::List ichimoku_cloud( NumericVector high, NumericVector low, @@ -87,18 +85,30 @@ std::vector calculate_midpoint(const std::vector& high, const st return result; } -// Helper function to shift a vector forward (into the future) -std::vector shift_vector(const std::vector& vec, int shift) { - std::vector result(vec.size(), NA_REAL); +// Helper function to shift a vector +std::vector shift_vector(const std::vector& vec, int shift, bool fill_na = true) { + int n = vec.size(); + std::vector result(n, NA_REAL); + if (shift >= 0) { - std::copy(vec.begin(), vec.end() - shift, result.begin() + shift); + for (int i = shift; i < n; ++i) { + result[i] = vec[i - shift]; + } } else { - std::copy(vec.begin() - shift, vec.end(), result.begin()); + for (int i = 0; i < n + shift; ++i) { + result[i] = vec[i - shift]; + } + if (!fill_na) { + for (int i = n + shift; i < n; ++i) { + result[i] = vec[n - 1]; // Repeat last value + } + } } + return result; } -//' Ichimoku Cloud +//' Ichimoku Cloud with Future Predictions //' //' @param high A numeric vector of high prices //' @param low A numeric vector of low prices @@ -108,8 +118,6 @@ std::vector shift_vector(const std::vector& vec, int shift) { //' @param senkou_span_b_period The period for Senkou Span B //' @param chikou_shift The shift for Chikou Span (typically 26) //' @return A list containing Tenkan-sen, Kijun-sen, Senkou Span A, Senkou Span B, and Chikou Span -//' @export -// [[Rcpp::export]] List ichimoku_cloud2( std::vector high, std::vector low, @@ -119,7 +127,6 @@ List ichimoku_cloud2( int senkou_span_b_period = 52, int chikou_shift = 26 ) { - int n = high.size(); // Calculate Tenkan-sen (Conversion Line) @@ -133,11 +140,11 @@ List ichimoku_cloud2( for (int i = kijun_period - 1; i < n; ++i) { senkou_span_a[i] = (tenkan_sen[i] + kijun_sen[i]) / 2.0; } - senkou_span_a = shift_vector(senkou_span_a, kijun_period); + senkou_span_a = shift_vector(senkou_span_a, kijun_period, false); // Calculate Senkou Span B (Leading Span B) std::vector senkou_span_b = calculate_midpoint(high, low, senkou_span_b_period); - senkou_span_b = shift_vector(senkou_span_b, kijun_period); + senkou_span_b = shift_vector(senkou_span_b, kijun_period, false); // Calculate Chikou Span (Lagging Span) std::vector chikou_span = shift_vector(close, -chikou_shift); diff --git a/vignettes/cpp-high-performance-technical-indicators.Rmd b/vignettes/cpp-high-performance-technical-indicators.Rmd index 91b582d..921e86f 100644 --- a/vignettes/cpp-high-performance-technical-indicators.Rmd +++ b/vignettes/cpp-high-performance-technical-indicators.Rmd @@ -12,9 +12,9 @@ knitr::opts_chunk$set( warning = FALSE, message = FALSE, fig.align = "center", - fig.width = 14, - fig.height = 12, - dpi = 100, + fig.width = 12, + fig.height = 10, + dpi = 150, collapse = TRUE, comment = "#>" ) diff --git a/vignettes/getting-started-with-the-dmplot-framwork.Rmd b/vignettes/getting-started-with-the-dmplot-framwork.Rmd index eeaed3e..adf3601 100644 --- a/vignettes/getting-started-with-the-dmplot-framwork.Rmd +++ b/vignettes/getting-started-with-the-dmplot-framwork.Rmd @@ -12,9 +12,9 @@ knitr::opts_chunk$set( warning = FALSE, message = FALSE, fig.align = "center", - fig.width = 14, - fig.height = 12, - dpi = 100, + fig.width = 12, + fig.height = 10, + dpi = 150, collapse = TRUE, comment = "#>" ) diff --git a/vignettes/intro-to-financial-indicators-with-dmplot.Rmd b/vignettes/intro-to-financial-indicators-with-dmplot.Rmd index d97b087..c2a0235 100644 --- a/vignettes/intro-to-financial-indicators-with-dmplot.Rmd +++ b/vignettes/intro-to-financial-indicators-with-dmplot.Rmd @@ -9,14 +9,14 @@ Author: Dereck Mezquita Date: "`r Sys.Date()`" --- -```{r, include = FALSE} +```{r, include=FALSE} knitr::opts_chunk$set( warning = FALSE, message = FALSE, fig.align = "center", fig.width = 12, fig.height = 10, - dpi = 100, + dpi = 150, collapse = TRUE, comment = "#>" ) @@ -279,7 +279,57 @@ bb_layer <- dmplot$stat_bollingerbands(data = data2, print(candle_plot + bb_layer) ``` -### 3. Moving Average Convergence Divergence (MACD) +### 3. Fibonacci Retracement (Fib) + +Fibonacci retracement is a popular tool used in technical analysis to identify potential support and resistance levels based on the Fibonacci sequence. + +**Mathematics:** + +The Fibonacci retracement levels are calculated as follows: + +- 0.000 (0.0%) +- 0.236 (23.6%) +- 0.382 (38.2%) +- 0.500 (50.0%) +- 0.618 (61.8%) +- 0.786 (78.6%) +- 1.000 (100.0%) + +```{r fib} +data2 <- dt$copy(data) + +high_price <- max(data2$high) +low_price <- min(data2$low) + +fib_levels <- dmplot$fib(high_price, low_price) + +print(fib_levels) +``` + +In the plot we overlay the Fibonacci retracement levels on the candlestick chart to identify potential support and resistance levels. + +```{r plot-fib} +fib_plot <- candle_plot + + ggplot2$geom_hline( + yintercept = fib_levels$prices, color = "yellow", linetype = "dashed" + ) + + ggplot2$scale_y_continuous( + name = "Price", + sec.axis = ggplot2$sec_axis( + ~ (. - low_price) / (high_price - low_price) * 100, + name = "Retracement %", + labels = \(x) paste0(round(x, 1), "%") + ) + ) + + ggplot2$labs( + title = "Price Chart with Fibonacci Retracement Levels", + x = "Date" + ) + +print(fib_plot) +``` + +### 4. Moving Average Convergence Divergence (MACD) MACD is a trend-following momentum indicator that shows the relationship between two moving averages of a security's price. @@ -328,7 +378,7 @@ macd_plot <- data2 |> print(macd_plot) ``` -### 4. Relative Strength Index (RSI) +### 5. Relative Strength Index (RSI) RSI is a momentum oscillator that measures the speed and change of price movements. @@ -383,7 +433,7 @@ rsi_plot <- na.omit(data2) |> print(rsi_plot) ``` -### 5. Rate of Change (ROC) / Momentum +### 6. Rate of Change (ROC) / Momentum The Rate of Change (ROC) indicator measures the percentage change in price between the current price and the price n periods ago. @@ -450,7 +500,7 @@ mom <- data2 |> print(mom) ``` -### 5. Stochastic Oscillator +### 7. Stochastic Oscillator The Stochastic Oscillator is a momentum indicator that shows the location of the close relative to the high-low range over a set number of periods.