Files
wickra/examples/c/wickra_csv.h
T
kingchenc 677ea37402 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.
2026-06-17 01:49:11 +02:00

102 lines
2.9 KiB
C

/* Shared OHLCV CSV loader for the Wickra C examples.
*
* 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
#include <stddef.h>
#include <stdint.h>
typedef struct WickraBar {
int64_t timestamp;
double open;
double high;
double low;
double close;
double volume;
} 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, malformed header or row). */
size_t wickra_load_csv(const char *path, WickraBar **out);
#ifdef WICKRA_CSV_IMPL
#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, "rb");
if (f == NULL) {
fprintf(stderr, "wickra_load_csv: cannot open %s\n", path);
return 0;
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
if (size <= 0) {
fclose(f);
return 0;
}
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 got;
}
#endif /* WICKRA_CSV_IMPL */
#endif /* WICKRA_CSV_H */