Files
wickra/bindings/go/data_layer_test.go
T
kingchenc cb6da4d737 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.
2026-06-15 22:36:16 +02:00

90 lines
2.5 KiB
Go

package wickra
import (
"math"
"strconv"
"testing"
)
// Cross-language data-layer parity: replay the shared golden tick stream through
// the TickAggregator and assert the candles match the Rust reference, with and
// without gap filling. Fixtures are generated by
// `cargo run -p wickra-examples --bin gen_golden`.
func dataParseF(t *testing.T, s string) float64 {
t.Helper()
v, err := strconv.ParseFloat(s, 64)
if err != nil {
t.Fatalf("parse %q: %v", s, err)
}
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 {
gap bool
fixture string
}{
{false, "data_candles"},
{true, "data_candles_gap"},
}
for _, c := range cases {
agg, err := NewTickAggregator(1000, c.gap)
if err != nil {
t.Fatalf("new: %v", err)
}
var got [][6]float64
for _, r := range ticks {
price, size, ts := dataParseF(t, r[0]), dataParseF(t, r[1]), int64(dataParseF(t, r[2]))
for _, k := range agg.Push(price, size, ts) {
got = append(got, [6]float64{k.Open, k.High, k.Low, k.Close, k.Volume, float64(k.Timestamp)})
}
}
agg.Close()
want := readGolden(t, c.fixture)
if len(got) != len(want) {
t.Fatalf("%s: %d candles vs %d", c.fixture, 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("%s row %d col %d: %v vs %v", c.fixture, i, j, got[i][j], w)
}
}
}
}
}