Files
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

51 lines
1.4 KiB
C#

using Wickra;
using Wickra.Examples;
// Resample a 1-minute series into higher timeframes and run an indicator per timeframe.
var oneMinute = MarketData.SyntheticCandles(1200, startTimestamp: 0, stepMs: 60_000);
Console.WriteLine("EMA(20) of close across timeframes (resampled from 1-minute bars):");
foreach (var factor in new[] { 1, 5, 15 })
{
var bars = Resample(oneMinute, factor);
using var ema = new Ema(20);
double last = 0;
foreach (var b in bars)
{
last = ema.Update(b.Close);
}
Console.WriteLine($" {factor,2}m: {bars.Length,5} bars EMA(20) last = {last:F4}");
}
static Bar[] Resample(Bar[] source, int factor)
{
if (factor <= 1)
{
return source;
}
// Native Resampler: bucket by an absolute timeframe (the synthetic bars step
// 60_000 ms, so factor minutes == factor*60_000 ms). No hand-written bucketing.
using var r = new Resampler((long)factor * 60_000);
var output = new List<Bar>();
foreach (var b in source)
{
var c = r.Update(b.Open, b.High, b.Low, b.Close, b.Volume, b.Timestamp);
if (c is not null)
{
output.Add(ToBar(c.Value));
}
}
var last = r.Flush();
if (last is not null)
{
output.Add(ToBar(last.Value));
}
return output.ToArray();
}
static Bar ToBar(Candle c) => new(c.Open, c.High, c.Low, c.Close, c.Volume, (long)c.Timestamp);