feat(data): expose CandleReader (CSV) natively in all 10 languages (#311)
Add the data-layer CSV candle reader to every binding so loading OHLCV candles from a CSV no longer needs a per-language CSV/dataframe dependency. - C ABI: wickra_candle_reader_new(bytes, len) / _count / _read / _free over an opaque CandleReader handle (parse the whole buffer up front, then drain). - Native: Node/WASM CandleReader.read() -> Candle[], Python read() -> list[tuple]. - C-ABI languages: Go Read() []Candle, C# Candle[] Read(), Java Candle[] read(), R read() S3 generic (n x 6 matrix); C / C++ call the C ABI directly. - Cross-language golden testdata/golden/data_csv*.csv pins the parsed candles bit-for-bit across every binding. Verified locally across Rust (test+clippy+fmt), Node, WASM, Python, C#, Go, Java, R, and the C/C++ cmake parity suite.
This commit is contained in:
@@ -16101,3 +16101,34 @@ impl WasmResampler {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ===== Data layer: CSV candle reader =====
|
||||
|
||||
/// Parse OHLCV candles from a CSV string (header `timestamp,open,high,low,close,
|
||||
/// volume`; a leading UTF-8 BOM is stripped).
|
||||
#[wasm_bindgen(js_name = CandleReader)]
|
||||
pub struct WasmCandleReader {
|
||||
candles: Vec<wc::Candle>,
|
||||
}
|
||||
|
||||
#[wasm_bindgen(js_class = CandleReader)]
|
||||
impl WasmCandleReader {
|
||||
/// Parse the whole CSV up front; throws on a malformed header or row.
|
||||
#[wasm_bindgen(constructor)]
|
||||
pub fn new(csv: &str) -> Result<WasmCandleReader, JsError> {
|
||||
let mut reader =
|
||||
wickra_data::csv::CandleReader::from_reader(csv.as_bytes()).map_err(map_data_err)?;
|
||||
let candles = reader.read_all().map_err(map_data_err)?;
|
||||
Ok(Self { candles })
|
||||
}
|
||||
|
||||
/// Return every parsed candle as a `{ open, high, low, close, volume,
|
||||
/// timestamp }` object.
|
||||
pub fn read(&self) -> Array {
|
||||
let arr = Array::new();
|
||||
for &c in &self.candles {
|
||||
arr.push(&candle_object(c));
|
||||
}
|
||||
arr
|
||||
}
|
||||
}
|
||||
|
||||
@@ -50,6 +50,13 @@ test('wasm tick aggregator gap-fill matches the golden candles', () => {
|
||||
assertCandles(run(true), readCsv('data_candles_gap'), 'gap');
|
||||
});
|
||||
|
||||
test('wasm candle reader matches the golden candles', () => {
|
||||
const csv = fs.readFileSync(path.join(GOLDEN, 'data_csv.csv'), 'utf8');
|
||||
const reader = new W.CandleReader(csv);
|
||||
const got = reader.read().map((c) => [c.open, c.high, c.low, c.close, c.volume, c.timestamp]);
|
||||
assertCandles(got, readCsv('data_csv_candles'), 'candle-reader');
|
||||
});
|
||||
|
||||
const INPUT = readCsv('input'); // open,high,low,close,volume (timestamp = row index)
|
||||
|
||||
function runResample() {
|
||||
|
||||
Reference in New Issue
Block a user