Skip to content

Commit

Permalink
Clean up and use convention of basic Rcpp package.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereckmezquita committed Jul 8, 2024
1 parent 1f1c350 commit a3a0f8b
Show file tree
Hide file tree
Showing 31 changed files with 209 additions and 111 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ jobs:
sudo apt-get update
sudo apt-get install -y libcurl4-openssl-dev libssl-dev libxml2-dev libgit2-dev
- name: Fix DESCRIPTION file
run: |
sed -i -e '$a\' DESCRIPTION
cat DESCRIPTION
- name: Install R dependencies
run: |
install.packages(c("devtools", "renv", "Rcpp", "pkgdown", "rcmdcheck", "remotes"))
Expand Down
4 changes: 0 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ deprecated/
.RData
.Ruserdata

# RcppExports
RcppExports.cpp
RcppExports.R

# History files
.Rhistory
.Rapp.history
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Depends: R (>= 4.1.0)
LinkingTo: Rcpp
Imports:
ggplot2 (>= 3.4.0),
data.table,
Rcpp
Rcpp (>= 1.0.12),
data.table
License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Expand All @@ -32,4 +32,4 @@ Suggests:
testthat (>= 3.0.0)
Remotes:
dereckmezquita/kucoin
Config/testthat/edition: 3
Config/testthat/edition: 3
2 changes: 0 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,4 @@ export(theme_dereck_dark)
export(theme_dereck_light)
import(data.table)
import(ggplot2)
importFrom(Rcpp,evalCpp)
importFrom(ggplot2,'%+replace%')
useDynLib(dmplot, .registration = TRUE)
10 changes: 4 additions & 6 deletions R/cpp.R → R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# Wrapper functions for C++ code in dmplot package

#' @useDynLib dmplot, .registration = TRUE
#' @importFrom Rcpp evalCpp
NULL
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Bollinger Bands
#'
Expand All @@ -11,7 +8,7 @@ NULL
#' @param sd The number of standard deviations for the bands
#' @return A list containing the lower band, moving average, upper band, and percentage B
#' @export
bb <- function(price, n, sd = 2) {
bb <- function(price, n, sd = 2L) {
.Call('_dmplot_bb', PACKAGE = 'dmplot', price, n, sd)
}

Expand Down Expand Up @@ -92,3 +89,4 @@ rsi <- function(price, n, method = 'e') {
sma <- function(price, n) {
.Call('_dmplot_sma', PACKAGE = 'dmplot', price, n)
}

4 changes: 2 additions & 2 deletions man/bb.Rd

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

2 changes: 1 addition & 1 deletion man/ema.Rd

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

2 changes: 1 addition & 1 deletion man/macd.Rd

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

2 changes: 1 addition & 1 deletion man/mom.Rd

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

2 changes: 1 addition & 1 deletion man/monte_carlo.Rd

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

2 changes: 1 addition & 1 deletion man/roc.Rd

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

2 changes: 1 addition & 1 deletion man/rsi.Rd

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

2 changes: 1 addition & 1 deletion man/sma.Rd

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

2 changes: 0 additions & 2 deletions src/Makevars.in

This file was deleted.

117 changes: 117 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Generated by using Rcpp::compileAttributes() -> do not edit by hand
// Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#include <Rcpp.h>

using namespace Rcpp;

#ifdef RCPP_USE_GLOBAL_ROSTREAM
Rcpp::Rostream<true>& Rcpp::Rcout = Rcpp::Rcpp_cout_get();
Rcpp::Rostream<false>& Rcpp::Rcerr = Rcpp::Rcpp_cerr_get();
#endif

// bb
Rcpp::List bb(std::vector<double> price, int n, int sd);
RcppExport SEXP _dmplot_bb(SEXP priceSEXP, SEXP nSEXP, SEXP sdSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
Rcpp::traits::input_parameter< int >::type sd(sdSEXP);
rcpp_result_gen = Rcpp::wrap(bb(price, n, sd));
return rcpp_result_gen;
END_RCPP
}
// ema
std::vector<double> ema(std::vector<double> price, int n, bool wilder);
RcppExport SEXP _dmplot_ema(SEXP priceSEXP, SEXP nSEXP, SEXP wilderSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
Rcpp::traits::input_parameter< bool >::type wilder(wilderSEXP);
rcpp_result_gen = Rcpp::wrap(ema(price, n, wilder));
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) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type s(sSEXP);
Rcpp::traits::input_parameter< int >::type l(lSEXP);
Rcpp::traits::input_parameter< int >::type k(kSEXP);
Rcpp::traits::input_parameter< bool >::type percent(percentSEXP);
rcpp_result_gen = Rcpp::wrap(macd(price, s, l, k, percent));
return rcpp_result_gen;
END_RCPP
}
// mom
std::vector<double> mom(std::vector<double> price, int n);
RcppExport SEXP _dmplot_mom(SEXP priceSEXP, SEXP nSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
rcpp_result_gen = Rcpp::wrap(mom(price, n));
return rcpp_result_gen;
END_RCPP
}
// monte_carlo
Rcpp::List monte_carlo(double seed_price, double daily_vol, int num_sims, int num_days);
RcppExport SEXP _dmplot_monte_carlo(SEXP seed_priceSEXP, SEXP daily_volSEXP, SEXP num_simsSEXP, SEXP num_daysSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< double >::type seed_price(seed_priceSEXP);
Rcpp::traits::input_parameter< double >::type daily_vol(daily_volSEXP);
Rcpp::traits::input_parameter< int >::type num_sims(num_simsSEXP);
Rcpp::traits::input_parameter< int >::type num_days(num_daysSEXP);
rcpp_result_gen = Rcpp::wrap(monte_carlo(seed_price, daily_vol, num_sims, num_days));
return rcpp_result_gen;
END_RCPP
}
// roc
std::vector<double> roc(std::vector<double> price, int n, char type);
RcppExport SEXP _dmplot_roc(SEXP priceSEXP, SEXP nSEXP, SEXP typeSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
Rcpp::traits::input_parameter< char >::type type(typeSEXP);
rcpp_result_gen = Rcpp::wrap(roc(price, n, type));
return rcpp_result_gen;
END_RCPP
}
// rsi
std::vector<double> rsi(std::vector<double> price, int n, char method);
RcppExport SEXP _dmplot_rsi(SEXP priceSEXP, SEXP nSEXP, SEXP methodSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
Rcpp::traits::input_parameter< char >::type method(methodSEXP);
rcpp_result_gen = Rcpp::wrap(rsi(price, n, method));
return rcpp_result_gen;
END_RCPP
}
// sma
std::vector<double> sma(std::vector<double> price, int n);
RcppExport SEXP _dmplot_sma(SEXP priceSEXP, SEXP nSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::vector<double> >::type price(priceSEXP);
Rcpp::traits::input_parameter< int >::type n(nSEXP);
rcpp_result_gen = Rcpp::wrap(sma(price, n));
return rcpp_result_gen;
END_RCPP
}
17 changes: 12 additions & 5 deletions src/bb-bollinger-bands.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include <Rcpp.h>
// [[Rcpp::depends(Rcpp)]]

#include <math.h>
#include "sma-simple-moving-average.h"

using namespace Rcpp;

#include "bb-bollinger-bands.h"
#include "sma-simple-moving-average.h"

// https://gallery.rcpp.org/articles/creating-a-datatable-in-rcpp/

//' Bollinger Bands
//'
//' @param price A numeric vector of prices
//' @param n The period for the moving average
//' @param sd The number of standard deviations for the bands
//' @return A list containing the lower band, moving average, upper band, and percentage B
//' @export
// [[Rcpp::export]]
Rcpp::List bb(std::vector<double> price, int n, int sd = 2) {
// calculate the simple moving average
Expand Down Expand Up @@ -45,5 +54,3 @@ Rcpp::List bb(std::vector<double> price, int n, int sd = 2) {

return result;
}

// https://gallery.rcpp.org/articles/creating-a-datatable-in-rcpp/
4 changes: 2 additions & 2 deletions src/bb-bollinger-bands.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KUCOIN_BB_BOLLINGER_BANDS_H
#define KUCOIN_BB_BOLLINGER_BANDS_H
#ifndef DMPLOT_BB_BOLLINGER_BANDS_H
#define DMPLOT_BB_BOLLINGER_BANDS_H

Rcpp::List bb(std::vector<double> price, int n, int sd);

Expand Down
7 changes: 7 additions & 0 deletions src/ema-exponential-moving-average.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ using namespace Rcpp;
// https://github.com/joshuaulrich/TTR/blob/master/src/moving_averages.c


//' Exponential Moving Average
//'
//' @param price A numeric vector of prices
//' @param n The period for the EMA
//' @param wilder Whether to use Wilder's smoothing
//' @return A numeric vector containing the EMA values
//' @export
// [[Rcpp::export]]
std::vector<double> ema(std::vector<double> price, int n, bool wilder = false) {
// define beta
Expand Down
4 changes: 2 additions & 2 deletions src/ema-exponential-moving-average.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KUCOIN_EMA_EXPONENTIAL_MOVING_AVEREAGE_H
#define KUCOIN_EMA_EXPONENTIAL_MOVING_AVEREAGE_H
#ifndef DMPLOT_EMA_EXPONENTIAL_MOVING_AVEREAGE_H
#define DMPLOT_EMA_EXPONENTIAL_MOVING_AVEREAGE_H

std::vector<double> ema(std::vector<double> price, int n, bool wilder = false);

Expand Down
9 changes: 9 additions & 0 deletions src/macd-moving-average-convergence-divergence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

using namespace Rcpp;

//' Moving Average Convergence Divergence (MACD)
//'
//' @param price A numeric vector of prices
//' @param s The short-term period
//' @param l The long-term period
//' @param k The signal line period
//' @param percent Whether to return the MACD as a percentage
//' @return A list containing the MACD and signal line values
//' @export
// [[Rcpp::export]]
List macd(std::vector<double> price, int s, int l, int k, bool percent = true) {
std::vector<double> mavg_fast = ema(price, s);
Expand Down
4 changes: 2 additions & 2 deletions src/macd-moving-average-convergence-divergence.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KUCOIN_MACD_MOVING_AVERAGE_CONVERGENCE_DIVERGENCE_H
#define KUCOIN_MACD_MOVING_AVERAGE_CONVERGENCE_DIVERGENCE_H
#ifndef DMPLOT_MACD_MOVING_AVERAGE_CONVERGENCE_DIVERGENCE_H
#define DMPLOT_MACD_MOVING_AVERAGE_CONVERGENCE_DIVERGENCE_H

Rcpp::List macd(std::vector<double> price, int s, int l, int k, bool percent);

Expand Down
6 changes: 6 additions & 0 deletions src/mom-momentum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ using namespace Rcpp;
// https://bookdown.org/kochiuyu/technical-analysis-with-r-second-edition2/momentum.html
// M_t(n) = P_t - P_(t-n)

//' Momentum
//'
//' @param price A numeric vector of prices
//' @param n The period for momentum calculation
//' @return A numeric vector containing the momentum values
//' @export
// [[Rcpp::export]]
std::vector<double> mom(std::vector<double> price, int n) {
std::vector<double> result(price.size(), NA_REAL);
Expand Down
4 changes: 2 additions & 2 deletions src/mom-momentum.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KUCOIN_MOM_MOMENTUM_H
#define KUCOIN_MOM_MOMENTUM_H
#ifndef DMPLOT_MOM_MOMENTUM_H
#define DMPLOT_MOM_MOMENTUM_H

std::vector<double> mom(std::vector<double> price, int n);

Expand Down
8 changes: 8 additions & 0 deletions src/monte_carlo.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
#include <Rcpp.h>

//' Monte Carlo Simulation
//'
//' @param seed_price The starting price
//' @param daily_vol The daily volatility
//' @param num_sims The number of simulations to run
//' @param num_days The number of days to simulate
//' @return A list containing the simulation results and end prices
//' @export
// [[Rcpp::export]]
Rcpp::List monte_carlo(double seed_price, double daily_vol, int num_sims, int num_days) {
int total_rows = num_sims * num_days;
Expand Down
7 changes: 7 additions & 0 deletions src/roc-rate-of-change.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ using namespace Rcpp;
// continuous type - TTR uses continuous by default
// ROC_t(K) = log(P_t) - log(P_(t-K))

//' Rate of Change (ROC)
//'
//' @param price A numeric vector of prices
//' @param n The period for ROC calculation
//' @param type The type of ROC calculation: 'c' for continuous (default) or 'd' for discrete
//' @return A numeric vector containing the ROC values
//' @export
// [[Rcpp::export]]
std::vector<double> roc(std::vector<double> price, int n, char type = 'c') {
std::vector<double> result(price.size(), NA_REAL);
Expand Down
4 changes: 2 additions & 2 deletions src/roc-rate-of-change.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef KUCOIN_ROC_RATE_OF_CHANGE_H
#define KUCOIN_ROC_RATE_OF_CHANGE_H
#ifndef DMPLOT_ROC_RATE_OF_CHANGE_H
#define DMPLOT_ROC_RATE_OF_CHANGE_H

std::vector<double> roc(std::vector<double> price, int n, char type);

Expand Down
Loading

0 comments on commit a3a0f8b

Please sign in to comment.