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
@@ -0,0 +1,73 @@
// Generated from bindings/c/include/wickra.h. Do not edit by hand.
package org.wickra;
import org.wickra.internal.NativeMethods;
import org.wickra.internal.WickraNative;
import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Cleaner;
import static java.lang.foreign.ValueLayout.*;
/** Streaming Resampler indicator over the Wickra C ABI. Not thread-safe; close when done. */
public final class Resampler implements AutoCloseable {
private final MemorySegment handle;
private final Cleaner.Cleanable cleanable;
public Resampler(long timeframe) {
MemorySegment h;
try {
h = (MemorySegment) NativeMethods.WICKRA_RESAMPLER_NEW.invokeExact(timeframe);
} catch (Throwable t) {
throw WickraNative.rethrow(t);
}
if (h.address() == 0L) {
throw new IllegalArgumentException("invalid Resampler parameters");
}
this.handle = h;
this.cleanable = WickraNative.register(this, h, NativeMethods.WICKRA_RESAMPLER_FREE);
}
/** Push one observation; returns the result, or null during warmup. */
public Candle update(double open, double high, double low, double close, double volume, long timestamp) {
try (Arena a = Arena.ofConfined()) {
MemorySegment out = a.allocate(48L);
byte ok = (byte) NativeMethods.WICKRA_RESAMPLER_UPDATE.invokeExact(handle, open, high, low, close, volume, timestamp, out);
if (ok == 0) {
return null;
}
return new Candle(
out.get(JAVA_DOUBLE, 0L),
out.get(JAVA_DOUBLE, 8L),
out.get(JAVA_DOUBLE, 16L),
out.get(JAVA_DOUBLE, 24L),
out.get(JAVA_DOUBLE, 32L),
(double) out.get(JAVA_LONG, 40L));
} catch (Throwable t) {
throw WickraNative.rethrow(t);
}
}
/** Emit the final, still-open candle (null if none is pending). */
public Candle flush() {
try (Arena a = Arena.ofConfined()) {
MemorySegment out = a.allocate(48L);
byte ok = (byte) NativeMethods.WICKRA_RESAMPLER_FLUSH.invokeExact(handle, out);
if (ok == 0) {
return null;
}
return new Candle(
out.get(JAVA_DOUBLE, 0L),
out.get(JAVA_DOUBLE, 8L),
out.get(JAVA_DOUBLE, 16L),
out.get(JAVA_DOUBLE, 24L),
out.get(JAVA_DOUBLE, 32L),
(double) out.get(JAVA_LONG, 40L));
} catch (Throwable t) {
throw WickraNative.rethrow(t);
}
}
@Override public void close() {
cleanable.clean();
}
}
@@ -3951,6 +3951,10 @@ public final class NativeMethods {
public static MethodHandle WICKRA_TICK_AGGREGATOR_PUSH;
public static MethodHandle WICKRA_TICK_AGGREGATOR_DRAIN;
public static MethodHandle WICKRA_TICK_AGGREGATOR_FREE;
public static MethodHandle WICKRA_RESAMPLER_NEW;
public static MethodHandle WICKRA_RESAMPLER_UPDATE;
public static MethodHandle WICKRA_RESAMPLER_FLUSH;
public static MethodHandle WICKRA_RESAMPLER_FREE;
static {
init0();
@@ -8023,6 +8027,10 @@ public final class NativeMethods {
WICKRA_TICK_AGGREGATOR_PUSH = h("wickra_tick_aggregator_push", FunctionDescriptor.of(JAVA_LONG, ADDRESS, JAVA_DOUBLE, JAVA_DOUBLE, JAVA_LONG));
WICKRA_TICK_AGGREGATOR_DRAIN = h("wickra_tick_aggregator_drain", FunctionDescriptor.of(JAVA_LONG, ADDRESS, ADDRESS, JAVA_LONG));
WICKRA_TICK_AGGREGATOR_FREE = h("wickra_tick_aggregator_free", FunctionDescriptor.ofVoid(ADDRESS));
WICKRA_RESAMPLER_NEW = h("wickra_resampler_new", FunctionDescriptor.of(ADDRESS, JAVA_LONG));
WICKRA_RESAMPLER_UPDATE = h("wickra_resampler_update", FunctionDescriptor.of(JAVA_BYTE, ADDRESS, JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE, JAVA_DOUBLE, JAVA_LONG, ADDRESS));
WICKRA_RESAMPLER_FLUSH = h("wickra_resampler_flush", FunctionDescriptor.of(JAVA_BYTE, ADDRESS, ADDRESS));
WICKRA_RESAMPLER_FREE = h("wickra_resampler_free", FunctionDescriptor.ofVoid(ADDRESS));
}
}
@@ -46,6 +46,33 @@ class DataLayerTest {
return rows.toArray(new double[0][]);
}
@Test
void resamplerMatchesGolden() throws IOException {
double[][] input = read("input"); // open,high,low,close,volume (timestamp = row index)
try (Resampler r = new Resampler(5)) {
List<double[]> got = new ArrayList<>();
for (int i = 0; i < input.length; i++) {
Candle c = r.update(input[i][0], input[i][1], input[i][2], input[i][3], input[i][4], i);
if (c != null) {
got.add(new double[]{c.open(), c.high(), c.low(), c.close(), c.volume(), (double) c.timestamp()});
}
}
Candle f = r.flush();
if (f != null) {
got.add(new double[]{f.open(), f.high(), f.low(), f.close(), f.volume(), (double) f.timestamp()});
}
double[][] want = read("data_resampled");
assertEquals(want.length, got.size(), "resample candle count");
for (int i = 0; i < got.size(); i++) {
for (int j = 0; j < 6; j++) {
double w = want[i][j];
assertTrue(Math.abs(got.get(i)[j] - w) <= 1e-9 * Math.max(1, Math.abs(w)),
"resample row " + i + " col " + j + ": " + got.get(i)[j] + " vs " + w);
}
}
}
}
@Test
void tickAggregatorMatchesGolden() throws IOException {
double[][] ticks = read("data_ticks");