examples: migrate to the native data layer (drop ws/coder-websocket/jackson/jsonlite) (#316)
Stacked on #315 (the native Binance REST fetcher). Retarget to `main` once #315 merges. Migrates the runnable examples off third-party data-I/O packages onto Wickra's native data layer (`CandleReader`, `Resampler`, `BinanceFeed`, `fetch_*klines`). ## Third-party packages removed (the zero-dep selling point) - **Node**: `ws` (live feed → BinanceFeed) — dropped from package.json + lockfile - **Go**: `github.com/coder/websocket` — dropped from go.mod / go.sum (`go mod tidy`) - **Java**: `jackson-databind` (live feed + REST fetch) — dropped from pom.xml - **R**: `jsonlite` + `websocket` + `later` — dropped from the README notes Each language's CSV loading now goes through `CandleReader`, manual resampling through `Resampler`, the live feed through `BinanceFeed`, and (Java/R) the REST download through the native fetcher. ## Verification Ran the offline examples per language against the bundled data — backtest and multi_timeframe produce identical output across Python / Node / Go / Java / R (e.g. ATR(14) last 345.1010; 1h→5m resamples to 240 bars, →15m to 80 bars). C# / C / WASM (stdlib-only, no third-party deps to remove) follow in this branch. Note: the streaming `strategy_*` examples have pre-existing candle-indicator runtime bugs (CI only syntax-smokes them); the CSV migration preserves their shape and leaves those bugs for a separate fix.
This commit is contained in:
+55
-44
@@ -1,13 +1,14 @@
|
||||
/* Shared OHLCV CSV loader for the Wickra C examples.
|
||||
*
|
||||
* The C ABI exposes only the indicators, not the wickra-data IO layer, so the
|
||||
* examples read CSV themselves. This header-only helper is the C counterpart of
|
||||
* `wickra_data::csv::CandleReader` used by the Rust examples: it parses the
|
||||
* standard `timestamp,open,high,low,close,volume` files shipped under
|
||||
* `examples/data/`.
|
||||
* Thin wrapper over the native C-ABI `wickra_candle_reader_*` (the same
|
||||
* `wickra-data::csv::CandleReader` the Rust examples use): it reads the whole
|
||||
* file into memory and parses the standard `timestamp,open,high,low,close,volume`
|
||||
* files shipped under `examples/data/` through the library — no hand-written CSV
|
||||
* parsing.
|
||||
*
|
||||
* Header-only: define WICKRA_CSV_IMPL in exactly one translation unit (each
|
||||
* example is a single .c file, so it just defines it before including this).
|
||||
* `wickra.h` must be included before this header.
|
||||
*/
|
||||
#ifndef WICKRA_CSV_H
|
||||
#define WICKRA_CSV_H
|
||||
@@ -26,8 +27,7 @@ typedef struct WickraBar {
|
||||
|
||||
/* Load an OHLCV CSV into a malloc'd array. Returns the candle count and stores
|
||||
* the array in *out (caller frees with free()). Returns 0 and leaves *out NULL
|
||||
* on any error (missing file, no parseable rows). A leading header line whose
|
||||
* first field is non-numeric is skipped. */
|
||||
* on any error (missing file, malformed header or row). */
|
||||
size_t wickra_load_csv(const char *path, WickraBar **out);
|
||||
|
||||
#ifdef WICKRA_CSV_IMPL
|
||||
@@ -35,55 +35,66 @@ size_t wickra_load_csv(const char *path, WickraBar **out);
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "wickra.h"
|
||||
|
||||
size_t wickra_load_csv(const char *path, WickraBar **out) {
|
||||
*out = NULL;
|
||||
FILE *f = fopen(path, "r");
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (f == NULL) {
|
||||
fprintf(stderr, "wickra_load_csv: cannot open %s\n", path);
|
||||
return 0;
|
||||
}
|
||||
|
||||
size_t cap = 1024;
|
||||
size_t n = 0;
|
||||
WickraBar *rows = (WickraBar *)malloc(cap * sizeof(*rows));
|
||||
if (rows == NULL) {
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (size <= 0) {
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
char line[512];
|
||||
while (fgets(line, (int)sizeof(line), f) != NULL) {
|
||||
WickraBar c;
|
||||
long long ts = 0;
|
||||
/* sscanf returns the number of fields successfully matched. A header
|
||||
* row ("timestamp,...") matches 0 and is skipped. */
|
||||
int matched = sscanf(line, "%lld,%lf,%lf,%lf,%lf,%lf", &ts, &c.open,
|
||||
&c.high, &c.low, &c.close, &c.volume);
|
||||
if (matched != 6) {
|
||||
continue;
|
||||
}
|
||||
c.timestamp = (int64_t)ts;
|
||||
|
||||
if (n == cap) {
|
||||
cap *= 2;
|
||||
WickraBar *grown = (WickraBar *)realloc(rows, cap * sizeof(*rows));
|
||||
if (grown == NULL) {
|
||||
free(rows);
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
rows = grown;
|
||||
}
|
||||
rows[n++] = c;
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
if (n == 0) {
|
||||
free(rows);
|
||||
char *buf = (char *)malloc((size_t)size);
|
||||
if (buf == NULL) {
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
size_t read_bytes = fread(buf, 1, (size_t)size, f);
|
||||
fclose(f);
|
||||
|
||||
/* Parse the whole buffer with the native reader. */
|
||||
struct CandleReader *reader =
|
||||
wickra_candle_reader_new((const uint8_t *)buf, (uintptr_t)read_bytes);
|
||||
free(buf);
|
||||
if (reader == NULL) {
|
||||
return 0;
|
||||
}
|
||||
size_t n = (size_t)wickra_candle_reader_count(reader);
|
||||
if (n == 0) {
|
||||
wickra_candle_reader_free(reader);
|
||||
return 0;
|
||||
}
|
||||
struct WickraCandle *candles = (struct WickraCandle *)malloc(n * sizeof(*candles));
|
||||
if (candles == NULL) {
|
||||
wickra_candle_reader_free(reader);
|
||||
return 0;
|
||||
}
|
||||
size_t got = (size_t)wickra_candle_reader_read(reader, candles, (uintptr_t)n);
|
||||
wickra_candle_reader_free(reader);
|
||||
|
||||
WickraBar *rows = (WickraBar *)malloc(got * sizeof(*rows));
|
||||
if (rows == NULL) {
|
||||
free(candles);
|
||||
return 0;
|
||||
}
|
||||
for (size_t i = 0; i < got; ++i) {
|
||||
rows[i].timestamp = candles[i].timestamp;
|
||||
rows[i].open = candles[i].open;
|
||||
rows[i].high = candles[i].high;
|
||||
rows[i].low = candles[i].low;
|
||||
rows[i].close = candles[i].close;
|
||||
rows[i].volume = candles[i].volume;
|
||||
}
|
||||
free(candles);
|
||||
*out = rows;
|
||||
return n;
|
||||
return got;
|
||||
}
|
||||
|
||||
#endif /* WICKRA_CSV_IMPL */
|
||||
|
||||
Reference in New Issue
Block a user