Files
wickra/examples/r/_common.R
T
kingchenc b7ef63400d Add the R binding over the C ABI hub (#230)
Adds an R binding (`bindings/r`) over the C ABI hub — the third language stecker after C# and Go, reaching the hub through R's native `.Call` interface (not extendr).

## What's here
- **`bindings/r`** — an R package exposing all 514 indicators as constructors that return a `wickra_indicator` object with generic `update`/`batch`/`reset` methods. The C glue (`src/wickra.c`) and R wrappers (`R/indicators.R`) are generated from `bindings/c/include/wickra.h` (same archetype taxonomy as the C#/Go generators: scalar/batch, multi-output, bars, profile, profile-values, array-input). The opaque handle is an R external pointer freed by a registered finalizer; multi-output returns a named vector (`NA` at warmup), bars a matrix, profiles a list.
- **`examples/r`** — the full example suite mirroring C/C#/Go: streaming, backtest, multi_timeframe, parallel_assets (`mclapply`), three strategies, and `fetch_btcusdt`/`live_binance`.
- **CI** — an `r` job builds the C ABI library, installs the package, runs the `testthat` suite and the offline examples on Linux, macOS and Windows (`R CMD check` is clean: 0 warnings, 0 notes).
- **Docs** — R added to the README languages table, project layout, building/testing, CONTRIBUTING binding table + regenerate note, ARCHITECTURE, examples index, issue/PR templates, the About-description template, and the other binding READMEs.

## Linking / distribution
The package compiles a thin `.Call` glue layer against the prebuilt C ABI library (header via `WICKRA_INCLUDE_DIR`, library via `WICKRA_LIB_DIR`). On Windows the package's own `wickra.dll` would collide with the C ABI's `wickra.dll`, so `configure.win` stages a renamed copy (`wickra_abi.dll`) and builds an import library referencing it; `install.libs.R` bundles the DLL and `.onLoad` puts it on the load path. On Linux/macOS the rpath locates the shared library. No `release.yml` change — R is distributed via r-universe / source install (gated).

No Rust crate or `Cargo.toml` change — the R package is standalone and additive.
2026-06-09 19:18:40 +02:00

55 lines
1.9 KiB
R

# Shared helpers for the offline Wickra R examples: deterministic synthetic
# market data, a small OHLCV CSV loader, and an equity-curve summary. Mirrors the
# helpers used by the C, C# and Go example suites.
synthetic_prices <- function(count, start = 100) {
i <- seq_len(count) - 1
start + 12 * sin(i * 0.05) + 5 * sin(i * 0.013) + i * 0.01
}
synthetic_candles <- function(count, start_ts = 0, step_ms = 3600000) {
prices <- synthetic_prices(count + 1)
k <- seq_len(count)
i <- k - 1
open <- prices[k]
close <- prices[k + 1]
data.frame(
open = open,
high = pmax(open, close) + 0.5 + abs(sin(i * 0.7)),
low = pmin(open, close) - 0.5 - abs(cos(i * 0.7)),
close = close,
volume = 1000 + 500 * (1 + sin(i * 0.1)),
timestamp = start_ts + i * step_ms
)
}
load_ohlcv_csv <- function(path) {
df <- utils::read.csv(path, header = TRUE, stringsAsFactors = FALSE)
if (ncol(df) >= 6) {
data.frame(open = df[[2]], high = df[[3]], low = df[[4]],
close = df[[5]], volume = df[[6]], timestamp = df[[1]])
} else {
data.frame(open = df[[1]], high = df[[2]], low = df[[3]],
close = df[[4]], volume = df[[5]], timestamp = seq_len(nrow(df)))
}
}
summarize_equity <- function(returns, trades, periods_per_year = 252) {
equity <- 1; peak <- 1; maxdd <- 0
for (r in returns) {
equity <- equity * (1 + r)
peak <- max(peak, equity)
maxdd <- max(maxdd, (peak - equity) / peak)
}
mean_r <- if (length(returns)) mean(returns) else 0
sd_r <- if (length(returns) > 1) stats::sd(returns) else 0
sharpe <- if (sd_r > 1e-12) mean_r / sd_r * sqrt(periods_per_year) else 0
list(total_return_pct = (equity - 1) * 100, sharpe = sharpe,
max_dd_pct = maxdd * 100, trades = trades)
}
print_equity <- function(name, r) {
cat(sprintf("%-26s return=%8.2f%% sharpe=%6.2f maxDD=%6.2f%% trades=%d\n",
name, r$total_return_pct, r$sharpe, r$max_dd_pct, r$trades))
}