snapPointToCandle method

DrawingPoint snapPointToCandle(
  1. DrawingPoint rawPoint
)

Snaps a drawing point's price to the closest candle Open, High, Low, or Close if magnet mode is enabled and candles exist at or near rawPoint.candleIndex.

Implementation

DrawingPoint snapPointToCandle(DrawingPoint rawPoint) {
  if (!_magnetMode) return rawPoint;
  final candleList = candles;
  if (candleList.isEmpty) return rawPoint;

  final targetIndex = rawPoint.candleIndex.clamp(0, candleList.length - 1);
  final c = candleList[targetIndex];

  final ohlc = [c.open, c.high, c.low, c.close];
  double closest = ohlc[0];
  double minDiff = (rawPoint.price - closest).abs();

  for (int i = 1; i < ohlc.length; i++) {
    final diff = (rawPoint.price - ohlc[i]).abs();
    if (diff < minDiff) {
      minDiff = diff;
      closest = ohlc[i];
    }
  }

  return rawPoint.copyWith(price: closest);
}