feat(data-layer): TickAggregator (tick-to-candle) in all 10 languages (#309)
* feat(data-layer): TickAggregator in Node, WASM, Python + C ABI hub First data-layer feature (F2): roll trade ticks up into fixed-timeframe OHLCV candles, exposed natively and over the C ABI. - wickra-data wired as a binding dependency (workspace dep; its wickra-core dep is default-features=false so it never forces rayon into the rayon-free WASM build — native bindings re-enable parallel through their own dependency). - Node `TickAggregator(bucket, gapFill?)` -> `push(price, size, ts): Candle[]`; WASM the same (array of objects); Python `push(...) -> list[tuple]`. - C ABI: `WickraCandle` struct + `wickra_tick_aggregator_new/push/free` (push writes candles into a caller buffer and returns the count), generated via the capi generator's new DATA_LAYER section; cbindgen now parses wickra-data so `TickAggregator` is a forward-declared opaque; header vendored to bindings/go. Verified bit-identical across Node/WASM/Python/C/C++ (o=100 h=101 l=100 c=101 v=3 ts=0 for the shared 3-tick probe). WIP: Go/C#/Java/R generated bindings and the cross-language golden are still pending. * feat(data-layer): TickAggregator in Go, C#, Java, R (lossless push/drain) Complete F2 across all 10 languages: the C-ABI tick aggregator now uses a two-step push/drain so gap-fill candles are never lost, and the four generated bindings expose it idiomatically. - C ABI redesigned: opaque TickAggregator handle (inner aggregator + pending buffer); push consumes a tick and returns the closed-candle count, drain copies them into a count-sized caller buffer. - Go: NewTickAggregator + Push(price,size,ts) []Candle; C#: TickAggregator + Candle[] Push(...); Java: TickAggregator + Candle[] push(...); R: TickAggregator constructor + push() S3 generic returning an (n x 6) numeric matrix. - Candle output record generated per language from WickraCandle. Verified bit-identical to the native bindings (o=100 h=101 l=100 c=101 v=3 ts=0) in Go, C#, Java, and R at runtime; R passes R CMD check (pre-existing doc warnings only). WIP: cross-language data-layer golden + CHANGELOG still pending. * test(data-layer): cross-language golden for the tick aggregator + CHANGELOG gen_golden emits a deterministic tick stream (testdata/golden/data_ticks.csv) and the reference candle streams with and without gap filling (data_candles.csv, data_candles_gap.csv). Every binding replays the shared ticks through its TickAggregator and checks the candles bit-for-bit (fp tolerance) against the Rust reference: - Node / WASM / Python / Go / C# / Java / R: a dedicated parity test each. - C / C++: data_layer_test.c (compiled as both, run as ctest). The gap-fill fixture closes several candles from a single push, exercising the lossless push/drain path. Records the feature under CHANGELOG [Unreleased]. * fix(examples): rename the CSV-loader candle to WickraBar The example CSV helper (wickra_csv.h) defined its own struct WickraCandle, which now collides with the public C ABI WickraCandle (the tick aggregator output) in any example that includes both headers (backtest, multi_timeframe, the strategy examples). The public type owns the name; rename the example loader's bar to WickraBar. The generated golden_test.c is untouched (its only match was the unrelated WickraCandleVolumeOutput).
This commit is contained in:
+119
-3
@@ -98,9 +98,9 @@ use wickra_core::{
|
||||
TdDWave, TdDeMarker, TdDifferential, TdLines, TdMovingAverage, TdOpen, TdPressure,
|
||||
TdPropulsion, TdRangeProjection, TdRei, TdRiskLevel, TdSequential, TdSetup, TdTrap, Tema,
|
||||
TermStructureBasis, ThreeDrives, ThreeInside, ThreeLineBreak, ThreeLineBreakBars,
|
||||
ThreeLineStrike, ThreeOutside, ThreeSoldiersOrCrows, ThreeStarsInSouth, Thrusting, TickBars,
|
||||
TickIndex, Tii, TimeBasedStop, TimeOfDayReturnProfile, TowerTopBottom, TpoProfile, Trade,
|
||||
TradeImbalance, TradeQuote, TradeSignAutocorrelation, TradeVolumeIndex, TrendLabel,
|
||||
ThreeLineStrike, ThreeOutside, ThreeSoldiersOrCrows, ThreeStarsInSouth, Thrusting, Tick,
|
||||
TickBars, TickIndex, Tii, TimeBasedStop, TimeOfDayReturnProfile, TowerTopBottom, TpoProfile,
|
||||
Trade, TradeImbalance, TradeQuote, TradeSignAutocorrelation, TradeVolumeIndex, TrendLabel,
|
||||
TrendStrengthIndex, Trendflex, TreynorRatio, Triangle, Trima, Trin, TripleTopBottom, Tristar,
|
||||
Trix, TrueRange, Tsf, TsfOscillator, Tsi, Tsv, TtmSqueeze, TtmTrend, TurnOfMonth, Tweezer,
|
||||
TwiggsMoneyFlow, TwoCrows, TypicalPrice, UlcerIndex, UltimateOscillator, UniqueThreeRiver,
|
||||
@@ -114,6 +114,8 @@ use wickra_core::{
|
||||
T3,
|
||||
};
|
||||
|
||||
use wickra_data::aggregator::{TickAggregator as DataTickAggregator, Timeframe};
|
||||
|
||||
// ===== Scalar indicators (f64 -> f64) =====
|
||||
|
||||
/// Create a `AdaptiveCycle` indicator.
|
||||
@@ -68072,6 +68074,120 @@ pub unsafe extern "C" fn wickra_footprint_free(handle: *mut Footprint) {
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Data layer (tick aggregation; caller buffer + count) =====
|
||||
|
||||
/// C-ABI view of an OHLCV candle (the tick aggregator's output).
|
||||
#[repr(C)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct WickraCandle {
|
||||
pub open: f64,
|
||||
pub high: f64,
|
||||
pub low: f64,
|
||||
pub close: f64,
|
||||
pub volume: f64,
|
||||
pub timestamp: i64,
|
||||
}
|
||||
|
||||
/// Opaque tick aggregator: the streaming aggregator plus the buffer of candles
|
||||
/// the most recent `push` closed, awaiting `drain`. Named `TickAggregator` (the
|
||||
/// public C-ABI handle); the inner `wickra-data` type is aliased to avoid the
|
||||
/// name clash.
|
||||
#[derive(Debug)]
|
||||
pub struct TickAggregator {
|
||||
inner: DataTickAggregator,
|
||||
pending: Vec<Candle>,
|
||||
}
|
||||
|
||||
/// Create a tick aggregator with the given bucket size (the same unit as the
|
||||
/// tick timestamps). `gap_fill` emits a flat placeholder candle for every
|
||||
/// skipped bucket. Returns `NULL` on a non-positive bucket; release with
|
||||
/// `wickra_tick_aggregator_free`.
|
||||
#[no_mangle]
|
||||
pub extern "C" fn wickra_tick_aggregator_new(bucket: i64, gap_fill: bool) -> *mut TickAggregator {
|
||||
match Timeframe::new(bucket) {
|
||||
Ok(timeframe) => Box::into_raw(Box::new(TickAggregator {
|
||||
inner: DataTickAggregator::new(timeframe).with_gap_fill(gap_fill),
|
||||
pending: Vec::new(),
|
||||
})),
|
||||
Err(_) => ptr::null_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Push one trade tick. Buffers the candles it closed inside the handle and
|
||||
/// returns the count (`0` if the open bar merely grew), or `-1` on a `NULL`
|
||||
/// handle / invalid tick / malformed timestamp. Read the candles with
|
||||
/// `wickra_tick_aggregator_drain`; the next `push` replaces the buffer.
|
||||
///
|
||||
/// # Safety
|
||||
/// `handle` must be valid (from `wickra_tick_aggregator_new`, not freed), or `NULL`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wickra_tick_aggregator_push(
|
||||
handle: *mut TickAggregator,
|
||||
price: f64,
|
||||
size: f64,
|
||||
timestamp: i64,
|
||||
) -> isize {
|
||||
let Some(aggregator) = handle.as_mut() else {
|
||||
return -1;
|
||||
};
|
||||
let Ok(tick) = Tick::new(price, size, timestamp) else {
|
||||
return -1;
|
||||
};
|
||||
let Ok(candles) = aggregator.inner.push(tick) else {
|
||||
return -1;
|
||||
};
|
||||
aggregator.pending = candles;
|
||||
isize::try_from(aggregator.pending.len()).unwrap_or(isize::MAX)
|
||||
}
|
||||
|
||||
/// Copy up to `cap` buffered candles (from the last `push`) into `out`, remove
|
||||
/// them from the buffer, and return the number written. Returns `0` on a `NULL`
|
||||
/// handle / `out`. Call with `cap` equal to the last `push` return to drain the
|
||||
/// whole batch losslessly.
|
||||
///
|
||||
/// # Safety
|
||||
/// `handle` (from `wickra_tick_aggregator_new`, not freed) and `out` must be valid
|
||||
/// or `NULL`; when non-`NULL`, `out` must cover `cap` `WickraCandle` elements.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wickra_tick_aggregator_drain(
|
||||
handle: *mut TickAggregator,
|
||||
out: *mut WickraCandle,
|
||||
cap: usize,
|
||||
) -> isize {
|
||||
let Some(aggregator) = handle.as_mut() else {
|
||||
return 0;
|
||||
};
|
||||
if out.is_null() {
|
||||
return 0;
|
||||
}
|
||||
let count = aggregator.pending.len().min(cap);
|
||||
let slots = slice::from_raw_parts_mut(out, count);
|
||||
for (slot, candle) in slots.iter_mut().zip(aggregator.pending.drain(..count)) {
|
||||
*slot = WickraCandle {
|
||||
open: candle.open,
|
||||
high: candle.high,
|
||||
low: candle.low,
|
||||
close: candle.close,
|
||||
volume: candle.volume,
|
||||
timestamp: candle.timestamp,
|
||||
};
|
||||
}
|
||||
isize::try_from(count).unwrap_or(isize::MAX)
|
||||
}
|
||||
|
||||
/// Destroy a tick aggregator created by `wickra_tick_aggregator_new`. No-op if
|
||||
/// `handle` is `NULL`.
|
||||
///
|
||||
/// # Safety
|
||||
/// `handle` must have been returned by `wickra_tick_aggregator_new` and not
|
||||
/// previously freed, or `NULL`.
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn wickra_tick_aggregator_free(handle: *mut TickAggregator) {
|
||||
if !handle.is_null() {
|
||||
drop(Box::from_raw(handle));
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
Reference in New Issue
Block a user