fibonacci static method

FibonacciResult? fibonacci(
  1. List<TaBar> points
)

Implementation

static FibonacciResult? fibonacci(List<TaBar> points) {
  if (points.isEmpty) return null;

  // Clamp prices to reasonable range [0, 1e9] to handle corrupted API data
  double high = (points.first.high ?? points.first.close).clamp(0.0, 1e9);
  double low = (points.first.low ?? points.first.close).clamp(0.0, 1e9);

  for (final p in points) {
    final h = (p.high ?? p.close).clamp(0.0, 1e9);
    final l = (p.low ?? p.close).clamp(0.0, 1e9);
    if (h > high) high = h;
    if (l < low) low = l;
  }

  const ratios = [0.0, 0.236, 0.382, 0.500, 0.618, 0.786, 1.0];
  final range = high - low;

  // Degenerate case: flat range (single candle or all prices identical)
  // Return null to skip rendering rather than showing 7 stacked lines
  if (range < 0.01) return null;

  final levels = ratios.map((r) => FibonacciLevel(ratio: r, price: high - r * range)).toList();
  return FibonacciResult(high: high, low: low, levels: levels);
}