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
+54
View File
@@ -94,12 +94,66 @@ static int check(const char *fixture, bool gap, const double *ticks, int nt) {
return fails;
}
static int check_resample(void) {
double input[MAXROWS * 5];
int ni = read_csv("input", input, 5);
double want[MAXROWS * 6];
int nw = read_csv("data_resampled", want, 6);
struct Resampler *r = wickra_resampler_new(5);
if (!r) {
printf("FAIL resample: new returned NULL\n");
return 1;
}
double got[MAXROWS * 6];
int ng = 0;
struct WickraCandle out;
for (int i = 0; i < ni; i++) {
if (wickra_resampler_update(r, input[i * 5 + 0], input[i * 5 + 1], input[i * 5 + 2],
input[i * 5 + 3], input[i * 5 + 4], (int64_t)i, &out)) {
got[ng * 6 + 0] = out.open;
got[ng * 6 + 1] = out.high;
got[ng * 6 + 2] = out.low;
got[ng * 6 + 3] = out.close;
got[ng * 6 + 4] = out.volume;
got[ng * 6 + 5] = (double)out.timestamp;
ng++;
}
}
if (wickra_resampler_flush(r, &out)) {
got[ng * 6 + 0] = out.open;
got[ng * 6 + 1] = out.high;
got[ng * 6 + 2] = out.low;
got[ng * 6 + 3] = out.close;
got[ng * 6 + 4] = out.volume;
got[ng * 6 + 5] = (double)out.timestamp;
ng++;
}
wickra_resampler_free(r);
if (ng != nw) {
printf("FAIL resample: %d candles vs %d\n", ng, nw);
return 1;
}
int fails = 0;
for (int i = 0; i < ng; i++) {
for (int j = 0; j < 6; j++) {
double w = want[i * 6 + j];
double tol = 1e-9 * fmax(1.0, fabs(w));
if (fabs(got[i * 6 + j] - w) > tol) {
printf("FAIL resample row %d col %d: %g vs %g\n", i, j, got[i * 6 + j], w);
fails++;
}
}
}
return fails;
}
int main(int argc, char **argv) {
GDIR = (argc > 1) ? argv[1] : "testdata/golden";
double ticks[MAXROWS * 3];
int nt = read_csv("data_ticks", ticks, 3);
int fails = check("data_candles", false, ticks, nt);
fails += check("data_candles_gap", true, ticks, nt);
fails += check_resample();
if (fails == 0) {
printf("C/C++ data layer: OK (%d ticks)\n", nt);
}
+29
View File
@@ -20,6 +20,7 @@ use wickra::{
Indicator, IntradayIntensity, MacdIndicator, Rsi, Sma, Tick,
};
use wickra_data::aggregator::{TickAggregator, Timeframe};
use wickra_data::resample::Resampler;
const N: usize = 80;
@@ -185,9 +186,37 @@ fn main() {
emit_profiles(dir, &candles);
emit_bars(dir, &candles);
emit_data_layer(dir);
emit_resampler(dir, &candles);
println!("golden fixtures written to {}", dir.display());
}
/// Data layer: the resampler. Resamples the shared input candles (timestamp =
/// row index) into 5-unit buckets; the final partial bucket comes out of flush.
fn emit_resampler(dir: &Path, candles: &[Candle]) {
let mut resampler = Resampler::new(Timeframe::new(5).unwrap());
let mut rows = Vec::new();
for &candle in candles {
if let Some(out) = resampler.push(candle).expect("valid resample push") {
rows.push(format!(
"{},{},{},{},{},{}",
out.open, out.high, out.low, out.close, out.volume, out.timestamp
));
}
}
if let Some(out) = resampler.flush().expect("valid resample flush") {
rows.push(format!(
"{},{},{},{},{},{}",
out.open, out.high, out.low, out.close, out.volume, out.timestamp
));
}
write_csv(
dir,
"data_resampled",
"open,high,low,close,volume,timestamp",
&rows,
);
}
/// Deterministic trade tick `i`: price on the shared varied path, a small
/// repeating size, and a timestamp that places roughly three ticks per
/// 1000-unit bucket. A deliberate jump at `i == 36` opens a multi-bucket gap so