ohlc method

List<OHLCPoint> ohlc({
  1. required DateTime start,
  2. required DateTime end,
  3. required Duration interval,
  4. double startPrice = 100,
  5. double volatility = 0.02,
})

Generates OHLC (Open-High-Low-Close) candlestick data.

Implementation

List<OHLCPoint> ohlc({
  required DateTime start,
  required DateTime end,
  required Duration interval,
  double startPrice = 100,
  double volatility = 0.02,
}) {
  final points = <OHLCPoint>[];
  var current = start;
  var previousClose = startPrice;

  while (!current.isAfter(end)) {
    final open = previousClose * (1 + _random.normal(0, volatility / 2));
    final close = open * (1 + _random.normal(0, volatility));

    final highOffset = _random.exponential(1 / volatility) * 0.5;
    final lowOffset = _random.exponential(1 / volatility) * 0.5;

    final high = math.max(open, close) * (1 + highOffset);
    final low = math.min(open, close) * (1 - lowOffset);

    final volume = _random.exponential(0.0001).toInt() + 1000;

    points.add(OHLCPoint(
      timestamp: current,
      open: open,
      high: high,
      low: low,
      close: close,
      volume: volume,
    ));

    previousClose = close;
    current = current.add(interval);
  }

  return points;
}