Skip to content

Commit

Permalink
Version 2 of cloud.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jul 8, 2024
1 parent ed2e3e5 commit a2b06a0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ BEGIN_RCPP
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 @@ -149,6 +166,7 @@ static const R_CallMethodDef CallEntries[] = {
{"_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
81 changes: 80 additions & 1 deletion src/ichimoku-cloud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,83 @@ Rcpp::List ichimoku_cloud(
_["senkou_span_a"] = senkou_span_a,
_["senkou_span_b"] = senkou_span_b_future
);
}
}


// -----

using namespace Rcpp;

// Helper function to calculate the middle point of high and low over a period
std::vector<double> calculate_midpoint(const std::vector<double>& high, const std::vector<double>& low, int period) {
std::vector<double> result(high.size(), NA_REAL);
for (int i = period - 1; i < high.size(); ++i) {
double period_high = *std::max_element(high.begin() + i - period + 1, high.begin() + i + 1);
double period_low = *std::min_element(low.begin() + i - period + 1, low.begin() + i + 1);
result[i] = (period_high + period_low) / 2.0;
}
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);
std::copy(vec.begin(), vec.end() - shift, result.begin() + shift);
return result;
}

//' Ichimoku Cloud
//'
//' @param high A numeric vector of high prices
//' @param low A numeric vector of low prices
//' @param close A numeric vector of closing prices
//' @param tenkan_period The period for Tenkan-sen (Conversion Line)
//' @param kijun_period The period for Kijun-sen (Base Line)
//' @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,
std::vector<double> close,
int tenkan_period = 9,
int kijun_period = 26,
int senkou_span_b_period = 52,
int chikou_shift = 26
) {

int n = high.size();

// Calculate Tenkan-sen (Conversion Line)
std::vector<double> tenkan_sen = calculate_midpoint(high, low, tenkan_period);

// Calculate Kijun-sen (Base Line)
std::vector<double> kijun_sen = calculate_midpoint(high, low, kijun_period);

// Calculate Senkou Span A (Leading Span A)
std::vector<double> senkou_span_a(n, NA_REAL);
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);

// 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);

// Calculate Chikou Span (Lagging Span)
std::vector<double> chikou_span = shift_vector(close, -chikou_shift);

// Create and return the list
List result = List::create(
_["tenkan_sen"] = tenkan_sen,
_["kijun_sen"] = kijun_sen,
_["senkou_span_a"] = senkou_span_a,
_["senkou_span_b"] = senkou_span_b,
_["chikou_span"] = chikou_span
);

return result;
}
9 changes: 9 additions & 0 deletions src/ichimoku-cloud.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,13 @@ Rcpp::List ichimoku_cloud(
int senkou_period = 52
);

Rcpp::List ichimoku_cloud2(
NumericVector high,
NumericVector low,
NumericVector close,
int tenkan_period = 9,
int kijun_period = 26,
int senkou_period = 52
);

#endif

0 comments on commit a2b06a0

Please sign in to comment.