Skip to content

Commit

Permalink
Vignette adjustments and fib example in vignette.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jul 9, 2024
1 parent 8f9883d commit 7ca365d
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 114 deletions.
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export(bb)
export(cite_package)
export(ema)
export(fib)
export(ichimoku_cloud)
export(macd)
export(mom)
export(monte_carlo)
Expand Down
14 changes: 0 additions & 14 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
output: github_document
---

```{r, include = FALSE}
```{r, include=FALSE}
knitr::opts_chunk$set(
warning = FALSE,
message = FALSE,
fig.path = "./man/figures/README-",
fig.align = "center",
fig.width = 12,
fig.height = 10,
dpi = 100,
dpi = 150,
collapse = TRUE,
comment = "#>"
)
Expand Down Expand Up @@ -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"
Expand Down
36 changes: 0 additions & 36 deletions man/ichimoku_cloud.Rd

This file was deleted.

35 changes: 0 additions & 35 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> high, std::vector<double> low, std::vector<double> 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<double> >::type high(highSEXP);
Rcpp::traits::input_parameter< std::vector<double> >::type low(lowSEXP);
Rcpp::traits::input_parameter< std::vector<double> >::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<double> price, int s, int l, int k, bool percent);
RcppExport SEXP _dmplot_macd(SEXP priceSEXP, SEXP sSEXP, SEXP lSEXP, SEXP kSEXP, SEXP percentSEXP) {
Expand Down Expand Up @@ -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},
Expand Down
33 changes: 20 additions & 13 deletions src/ichimoku-cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ std::vector<double> 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,
Expand Down Expand Up @@ -87,18 +85,30 @@ std::vector<double> calculate_midpoint(const std::vector<double>& high, const st
return result;
}

// Helper function to shift a vector forward (into the future)
std::vector<double> shift_vector(const std::vector<double>& vec, int shift) {
std::vector<double> result(vec.size(), NA_REAL);
// Helper function to shift a vector
std::vector<double> shift_vector(const std::vector<double>& vec, int shift, bool fill_na = true) {
int n = vec.size();
std::vector<double> 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
Expand All @@ -108,8 +118,6 @@ std::vector<double> shift_vector(const std::vector<double>& 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<double> high,
std::vector<double> low,
Expand All @@ -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)
Expand All @@ -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<double> 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<double> chikou_span = shift_vector(close, -chikou_shift);
Expand Down
6 changes: 3 additions & 3 deletions vignettes/cpp-high-performance-technical-indicators.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "#>"
)
Expand Down
6 changes: 3 additions & 3 deletions vignettes/getting-started-with-the-dmplot-framwork.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "#>"
)
Expand Down
62 changes: 56 additions & 6 deletions vignettes/intro-to-financial-indicators-with-dmplot.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "#>"
)
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.

Expand Down

0 comments on commit 7ca365d

Please sign in to comment.