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
@@ -28,6 +28,37 @@ public class DataLayerTests
return rows.ToArray();
}
[Fact]
public void ResamplerMatchesGolden()
{
var input = Read("input"); // open,high,low,close,volume (timestamp = row index)
using var r = new Resampler(5);
var got = new List<double[]>();
for (var i = 0; i < input.Length; i++)
{
var c = r.Update(input[i][0], input[i][1], input[i][2], input[i][3], input[i][4], i);
if (c.HasValue)
{
got.Add(new[] { c.Value.Open, c.Value.High, c.Value.Low, c.Value.Close, c.Value.Volume, (double)c.Value.Timestamp });
}
}
var f = r.Flush();
if (f.HasValue)
{
got.Add(new[] { f.Value.Open, f.Value.High, f.Value.Low, f.Value.Close, f.Value.Volume, (double)f.Value.Timestamp });
}
var want = Read("data_resampled");
Assert.Equal(want.Length, got.Count);
for (var i = 0; i < got.Count; i++)
{
for (var j = 0; j < 6; j++)
{
var tol = 1e-9 * Math.Max(1, Math.Abs(want[i][j]));
Assert.True(Math.Abs(got[i][j] - want[i][j]) <= tol, $"resample row {i} col {j}: {got[i][j]} vs {want[i][j]}");
}
}
}
[Theory]
[InlineData(false, "data_candles")]
[InlineData(true, "data_candles_gap")]
@@ -26580,6 +26580,50 @@ public sealed class RenkoTrailingStop : IDisposable
public void Dispose() => _handle.Dispose();
}
public sealed class Resampler : IDisposable
{
private readonly WickraHandle _handle;
public Resampler(long timeframe)
{
var ptr = NativeMethods.wickra_resampler_new(timeframe);
if (ptr == nint.Zero)
{
throw new ArgumentException("invalid Resampler parameters");
}
_handle = new WickraHandle(ptr, NativeMethods.wickra_resampler_free);
}
public Candle? Update(double open, double high, double low, double close, double volume, long timestamp)
{
WickraCandle native;
bool ok;
unsafe
{
ok = NativeMethods.wickra_resampler_update(_handle.DangerousGetHandle(), open, high, low, close, volume, timestamp, &native);
}
GC.KeepAlive(_handle);
return ok ? new Candle(native.open, native.high, native.low, native.close, native.volume, native.timestamp) : null;
}
/// <summary>Emit the final, still-open candle (null if none is pending).</summary>
public Candle? Flush()
{
WickraCandle native;
bool ok;
unsafe
{
ok = NativeMethods.wickra_resampler_flush(_handle.DangerousGetHandle(), &native);
}
GC.KeepAlive(_handle);
return ok ? new Candle(native.open, native.high, native.low, native.close, native.volume, native.timestamp) : null;
}
public void Dispose() => _handle.Dispose();
}
public sealed class RickshawMan : IDisposable
{
private readonly WickraHandle _handle;
@@ -12416,6 +12416,20 @@ internal static partial class NativeMethods
[LibraryImport(WickraNative.LibraryName)]
internal static partial void wickra_tick_aggregator_free(nint handle);
[LibraryImport(WickraNative.LibraryName)]
internal static partial nint wickra_resampler_new(long timeframe);
[LibraryImport(WickraNative.LibraryName)]
[return: MarshalAs(UnmanagedType.U1)]
internal static unsafe partial bool wickra_resampler_update(nint handle, double open, double high, double low, double close, double volume, long timestamp, WickraCandle* @out);
[LibraryImport(WickraNative.LibraryName)]
[return: MarshalAs(UnmanagedType.U1)]
internal static unsafe partial bool wickra_resampler_flush(nint handle, WickraCandle* @out);
[LibraryImport(WickraNative.LibraryName)]
internal static partial void wickra_resampler_free(nint handle);
}
[StructLayout(LayoutKind.Sequential)]