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:
@@ -16043,3 +16043,61 @@ impl WasmTickAggregator {
|
||||
self.inner.fills_gaps()
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Data layer: resampling (candle -> higher-timeframe candle) =====
|
||||
|
||||
fn candle_object(c: wc::Candle) -> Object {
|
||||
let obj = Object::new();
|
||||
Reflect::set(&obj, &"open".into(), &c.open.into()).ok();
|
||||
Reflect::set(&obj, &"high".into(), &c.high.into()).ok();
|
||||
Reflect::set(&obj, &"low".into(), &c.low.into()).ok();
|
||||
Reflect::set(&obj, &"close".into(), &c.close.into()).ok();
|
||||
Reflect::set(&obj, &"volume".into(), &c.volume.into()).ok();
|
||||
Reflect::set(&obj, &"timestamp".into(), &(c.timestamp as f64).into()).ok();
|
||||
obj
|
||||
}
|
||||
|
||||
/// Resample candles into a higher timeframe (e.g. 1m -> 5m).
|
||||
#[wasm_bindgen(js_name = Resampler)]
|
||||
pub struct WasmResampler {
|
||||
inner: wickra_data::resample::Resampler,
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_class = Resampler)]
|
||||
impl WasmResampler {
|
||||
/// Construct a resampler aggregating inputs into `timeframe`-sized candles.
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(timeframe: f64) -> Result<WasmResampler, JsError> {
|
||||
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 a `{ open, high, low, close, volume, timestamp }`
|
||||
/// object on a bucket boundary, otherwise `null`.
|
||||
pub fn update(
|
||||
&mut self,
|
||||
open: f64,
|
||||
high: f64,
|
||||
low: f64,
|
||||
close: f64,
|
||||
volume: f64,
|
||||
timestamp: f64,
|
||||
) -> Result<JsValue, JsError> {
|
||||
let candle =
|
||||
wc::Candle::new(open, high, low, close, volume, timestamp as i64).map_err(map_err)?;
|
||||
match self.inner.push(candle).map_err(map_data_err)? {
|
||||
Some(c) => Ok(candle_object(c).into()),
|
||||
None => Ok(JsValue::NULL),
|
||||
}
|
||||
}
|
||||
|
||||
/// Emit the final, still-open candle (or `null` if none is pending).
|
||||
pub fn flush(&mut self) -> Result<JsValue, JsError> {
|
||||
match self.inner.flush().map_err(map_data_err)? {
|
||||
Some(c) => Ok(candle_object(c).into()),
|
||||
None => Ok(JsValue::NULL),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,3 +49,25 @@ test('wasm tick aggregator matches the golden candles', () => {
|
||||
test('wasm 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 W.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('wasm resampler matches the golden candles', () => {
|
||||
assertCandles(runResample(), readCsv('data_resampled'), 'resample');
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user