feat(data-layer): Resampler (candle resampling) in all 10 languages (#310)

* feat(data-layer): Resampler (candle resampling) in all 10 languages

Second data-layer feature (F3): resample candles into a higher timeframe.

- Native (Node.js/WASM): new Resampler(timeframe) -> update(o,h,l,c,v,ts):
  Candle|null + flush(): Candle|null. Python the same -> tuple|None.
- C ABI: wickra_resampler_new/update/flush/free (update has the multi-output
  shape so the generators auto-emit it; flush is bespoke). Go Update -> (Candle,
  bool) + Flush; C# Candle? Update/Flush; Java Candle update/flush; R update()
  generic + a flush() S3 method (extends base::flush); C/C++ direct.
- Cross-language golden (testdata/golden/data_resampled.csv): the shared input
  candles resampled into 5-unit buckets, the final partial bucket via flush,
  pinned bit-for-bit across every binding.

Verified locally in all 10 (3 candles for the 5-unit smoke; 16 for the golden).
The WickraCandle output record is shared with the tick aggregator (deduped).

* test(node): exclude data-layer types from the indicator completeness contract

The Resampler exposes update(), so the completeness test flagged it as an
indicator and required batch/reset/isReady/warmupPeriod, which a data-layer type
does not have. Exclude TickAggregator and Resampler like the bar builders.
This commit is contained in:
kingchenc
2026-06-15 22:36:16 +02:00
committed by GitHub
parent 8a103ef920
commit cb6da4d737
30 changed files with 901 additions and 4 deletions
+17
View File
@@ -678,6 +678,8 @@ typedef struct RenkoBars RenkoBars;
typedef struct RenkoTrailingStop RenkoTrailingStop;
typedef struct Resampler Resampler;
typedef struct RickshawMan RickshawMan;
typedef struct RisingThreeMethods RisingThreeMethods;
@@ -13719,6 +13721,21 @@ intptr_t wickra_tick_aggregator_drain(struct TickAggregator *handle,
void wickra_tick_aggregator_free(struct TickAggregator *handle);
struct Resampler *wickra_resampler_new(int64_t timeframe);
bool wickra_resampler_update(struct Resampler *handle,
double open,
double high,
double low,
double close,
double volume,
int64_t timestamp,
struct WickraCandle *out);
bool wickra_resampler_flush(struct Resampler *handle, struct WickraCandle *out);
void wickra_resampler_free(struct Resampler *handle);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+98
View File
@@ -116,6 +116,8 @@ use wickra_core::{
use wickra_data::aggregator::{TickAggregator as DataTickAggregator, Timeframe};
use wickra_data::resample::Resampler;
// ===== Scalar indicators (f64 -> f64) =====
/// Create a `AdaptiveCycle` indicator.
@@ -68188,6 +68190,102 @@ pub unsafe extern "C" fn wickra_tick_aggregator_free(handle: *mut TickAggregator
}
}
/// Convert a core candle into the C-ABI view.
fn candle_to_c(candle: Candle) -> WickraCandle {
WickraCandle {
open: candle.open,
high: candle.high,
low: candle.low,
close: candle.close,
volume: candle.volume,
timestamp: candle.timestamp,
}
}
/// Create a resampler aggregating input candles into `timeframe`-sized candles
/// (the same unit as the candle timestamps). Returns `NULL` on a non-positive
/// timeframe; release with `wickra_resampler_free`.
#[no_mangle]
pub extern "C" fn wickra_resampler_new(timeframe: i64) -> *mut Resampler {
match Timeframe::new(timeframe) {
Ok(tf) => Box::into_raw(Box::new(Resampler::new(tf))),
Err(_) => ptr::null_mut(),
}
}
/// Push one candle. On `true` the completed higher-timeframe candle is written to
/// `*out`; `false` means none closed yet (still aggregating), a `NULL` handle /
/// `out`, or an invalid input candle.
///
/// # Safety
/// `handle` (from `wickra_resampler_new`, not freed) and `out` must be valid or `NULL`.
#[no_mangle]
pub unsafe extern "C" fn wickra_resampler_update(
handle: *mut Resampler,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
timestamp: i64,
out: *mut WickraCandle,
) -> bool {
let Some(resampler) = handle.as_mut() else {
return false;
};
if out.is_null() {
return false;
}
let Ok(candle) = Candle::new(open, high, low, close, volume, timestamp) else {
return false;
};
match resampler.push(candle) {
Ok(Some(emitted)) => {
*out = candle_to_c(emitted);
true
}
_ => false,
}
}
/// Flush the final, still-open candle. On `true` it is written to `*out`; `false`
/// means nothing was pending or `handle` / `out` is `NULL`.
///
/// # Safety
/// `handle` (from `wickra_resampler_new`, not freed) and `out` must be valid or `NULL`.
#[no_mangle]
pub unsafe extern "C" fn wickra_resampler_flush(
handle: *mut Resampler,
out: *mut WickraCandle,
) -> bool {
let Some(resampler) = handle.as_mut() else {
return false;
};
if out.is_null() {
return false;
}
match resampler.flush() {
Ok(Some(emitted)) => {
*out = candle_to_c(emitted);
true
}
_ => false,
}
}
/// Destroy a resampler created by `wickra_resampler_new`. No-op if `handle` is
/// `NULL`.
///
/// # Safety
/// `handle` must have been returned by `wickra_resampler_new` and not previously
/// freed, or `NULL`.
#[no_mangle]
pub unsafe extern "C" fn wickra_resampler_free(handle: *mut Resampler) {
if !handle.is_null() {
drop(Box::from_raw(handle));
}
}
#[cfg(test)]
mod tests {
use super::*;