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
+8 -2
View File
@@ -27,9 +27,14 @@ const BAR_BUILDERS = new Set([
'ThreeLineBreakBars',
]);
// Data-layer types (tick aggregator, resampler) are not `Indicator`s: they
// transform raw market data into candles and have their own update/flush shape,
// so they are excluded from the streaming-indicator completeness contract.
const DATA_LAYER = new Set(['TickAggregator', 'Resampler']);
// An "indicator class" is an exported constructor whose prototype carries the
// streaming `update` method. This excludes `version` (a plain function), the bar
// builders, and any non-indicator export.
// builders, the data-layer types, and any non-indicator export.
function indicatorClasses() {
return Object.keys(wickra).filter((name) => {
const value = wickra[name];
@@ -37,7 +42,8 @@ function indicatorClasses() {
typeof value === 'function' &&
value.prototype &&
typeof value.prototype.update === 'function' &&
!BAR_BUILDERS.has(name)
!BAR_BUILDERS.has(name) &&
!DATA_LAYER.has(name)
);
});
}
+23 -1
View File
@@ -8,7 +8,7 @@ const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { TickAggregator } = require('..');
const { TickAggregator, Resampler } = require('..');
const GOLDEN = path.resolve(__dirname, '..', '..', '..', 'testdata', 'golden');
@@ -51,3 +51,25 @@ test('tick aggregator matches the golden candles', () => {
test('tick aggregator gap-fill matches the golden candles', () => {
assertCandles(run(true), readCsv('data_candles_gap'), 'gap');
});
const INPUT = readCsv('input'); // open,high,low,close,volume (timestamp = row index)
function runResample() {
const r = new Resampler(5);
const out = [];
INPUT.forEach(([o, h, l, c, v], i) => {
const candle = r.update(o, h, l, c, v, i);
if (candle) {
out.push([candle.open, candle.high, candle.low, candle.close, candle.volume, candle.timestamp]);
}
});
const f = r.flush();
if (f) {
out.push([f.open, f.high, f.low, f.close, f.volume, f.timestamp]);
}
return out;
}
test('resampler matches the golden candles', () => {
assertCandles(runResample(), readCsv('data_resampled'), 'resample');
});
+16
View File
@@ -5957,3 +5957,19 @@ export declare class TickAggregator {
/** Whether gap filling is enabled. */
fillsGaps(): boolean
}
export type ResamplerNode = Resampler
/** Resample candles into a higher timeframe (e.g. 1m -> 5m). */
export declare class Resampler {
/**
* Construct a resampler that aggregates inputs into `timeframe`-sized
* candles (same unit as the candle timestamps).
*/
constructor(timeframe: number)
/**
* Push one candle; returns the completed higher-timeframe candle when a
* bucket boundary is crossed, otherwise `null`.
*/
update(open: number, high: number, low: number, close: number, volume: number, timestamp: number): CandleValue | null
/** Emit the final, still-open candle (or `null` if none is pending). */
flush(): CandleValue | null
}
File diff suppressed because one or more lines are too long
+63
View File
@@ -21937,3 +21937,66 @@ impl TickAggregatorNode {
self.inner.fills_gaps()
}
}
// ===== Data layer: resampling (candle -> higher-timeframe candle) =====
fn candle_to_value(c: wc::Candle) -> CandleValue {
CandleValue {
open: c.open,
high: c.high,
low: c.low,
close: c.close,
volume: c.volume,
timestamp: c.timestamp as f64,
}
}
/// Resample candles into a higher timeframe (e.g. 1m -> 5m).
#[napi(js_name = "Resampler")]
pub struct ResamplerNode {
inner: wickra_data::resample::Resampler,
}
#[napi]
impl ResamplerNode {
/// Construct a resampler that aggregates inputs into `timeframe`-sized
/// candles (same unit as the candle timestamps).
#[napi(constructor)]
pub fn new(timeframe: f64) -> napi::Result<Self> {
let tf = wickra_data::aggregator::Timeframe::new(timeframe as i64).map_err(map_data_err)?;
Ok(Self {
inner: wickra_data::resample::Resampler::new(tf),
})
}
/// Push one candle; returns the completed higher-timeframe candle when a
/// bucket boundary is crossed, otherwise `null`.
#[napi]
pub fn update(
&mut self,
open: f64,
high: f64,
low: f64,
close: f64,
volume: f64,
timestamp: f64,
) -> napi::Result<Option<CandleValue>> {
let candle =
wc::Candle::new(open, high, low, close, volume, timestamp as i64).map_err(map_err)?;
Ok(self
.inner
.push(candle)
.map_err(map_data_err)?
.map(candle_to_value))
}
/// Emit the final, still-open candle (or `null` if none is pending).
#[napi]
pub fn flush(&mut self) -> napi::Result<Option<CandleValue>> {
Ok(self
.inner
.flush()
.map_err(map_data_err)?
.map(candle_to_value))
}
}