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
+31
View File
@@ -20,6 +20,37 @@ func dataParseF(t *testing.T, s string) float64 {
return v
}
func TestResamplerGolden(t *testing.T) {
input := readGolden(t, "input") // open,high,low,close,volume (timestamp = row index)
r, err := NewResampler(5)
if err != nil {
t.Fatalf("new: %v", err)
}
var got [][6]float64
for i, row := range input {
o, h, l, c, v := dataParseF(t, row[0]), dataParseF(t, row[1]), dataParseF(t, row[2]), dataParseF(t, row[3]), dataParseF(t, row[4])
if k, ok := r.Update(o, h, l, c, v, int64(i)); ok {
got = append(got, [6]float64{k.Open, k.High, k.Low, k.Close, k.Volume, float64(k.Timestamp)})
}
}
if k, ok := r.Flush(); ok {
got = append(got, [6]float64{k.Open, k.High, k.Low, k.Close, k.Volume, float64(k.Timestamp)})
}
r.Close()
want := readGolden(t, "data_resampled")
if len(got) != len(want) {
t.Fatalf("resample: %d candles vs %d", len(got), len(want))
}
for i := range got {
for j := 0; j < 6; j++ {
w := dataParseF(t, want[i][j])
if math.Abs(got[i][j]-w) > 1e-9*math.Max(1, math.Abs(w)) {
t.Errorf("resample row %d col %d: %v vs %v", i, j, got[i][j], w)
}
}
}
}
func TestTickAggregatorGolden(t *testing.T) {
ticks := readGolden(t, "data_ticks")
cases := []struct {
+17
View File
@@ -678,6 +678,8 @@ typedef struct RenkoBars RenkoBars;
typedef struct RenkoTrailingStop RenkoTrailingStop;
typedef struct Resampler Resampler;
typedef struct RickshawMan RickshawMan;
typedef struct RisingThreeMethods RisingThreeMethods;
@@ -13719,6 +13721,21 @@ intptr_t wickra_tick_aggregator_drain(struct TickAggregator *handle,
void wickra_tick_aggregator_free(struct TickAggregator *handle);
struct Resampler *wickra_resampler_new(int64_t timeframe);
bool wickra_resampler_update(struct Resampler *handle,
double open,
double high,
double low,
double close,
double volume,
int64_t timestamp,
struct WickraCandle *out);
bool wickra_resampler_flush(struct Resampler *handle, struct WickraCandle *out);
void wickra_resampler_free(struct Resampler *handle);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
+50
View File
@@ -28057,6 +28057,56 @@ func (ind *RenkoTrailingStop) Close() {
}
}
// Resampler wraps the Resampler indicator over the Wickra C ABI.
type Resampler struct {
handle *C.struct_Resampler
}
// NewResampler constructs a Resampler. It returns ErrInvalidParams when the
// native constructor rejects the arguments.
func NewResampler(timeframe int64) (*Resampler, error) {
ptr := C.wickra_resampler_new(C.int64_t(timeframe))
if ptr == nil {
return nil, ErrInvalidParams
}
obj := &Resampler{handle: ptr}
runtime.SetFinalizer(obj, (*Resampler).Close)
return obj, nil
}
// Update feeds one observation. The bool reports whether a value is
// available yet (false during warmup).
func (ind *Resampler) Update(open float64, high float64, low float64, close float64, volume float64, timestamp int64) (Candle, bool) {
var out C.struct_WickraCandle
ok := bool(C.wickra_resampler_update(ind.handle, C.double(open), C.double(high), C.double(low), C.double(close), C.double(volume), C.int64_t(timestamp), &out))
runtime.KeepAlive(ind)
if !ok {
return Candle{}, false
}
return Candle{float64(out.open), float64(out.high), float64(out.low), float64(out.close), float64(out.volume), int64(out.timestamp)}, true
}
// Flush emits the final, still-open candle (ok is false if none is pending).
func (ind *Resampler) Flush() (Candle, bool) {
var out C.struct_WickraCandle
ok := bool(C.wickra_resampler_flush(ind.handle, &out))
runtime.KeepAlive(ind)
if !ok {
return Candle{}, false
}
return Candle{float64(out.open), float64(out.high), float64(out.low), float64(out.close), float64(out.volume), int64(out.timestamp)}, true
}
// Close frees the native handle. It is idempotent and safe to call
// alongside the finalizer.
func (ind *Resampler) Close() {
if ind.handle != nil {
C.wickra_resampler_free(ind.handle)
ind.handle = nil
runtime.SetFinalizer(ind, nil)
}
}
// RickshawMan wraps the RickshawMan indicator over the Wickra C ABI.
type RickshawMan struct {
handle *C.struct_RickshawMan