parseOIAnalysis method

Future<OIAnalysisSnapshot> parseOIAnalysis({
  1. required String underlyingSymbolId,
  2. required String expiry,
  3. required int timeFrom,
  4. required int timeTo,
  5. required double atm,
  6. int eachSide = 5,
})

Convenience wrapper over MarketDataInterface.fetchOIAnalysis that handles the JSON decode and trims the result to the ATM +/- eachSide window (by strike count, not price distance) using atm -- see that method's doc for the underlying contract.

Implementation

Future<OIAnalysisSnapshot> parseOIAnalysis({
  required String underlyingSymbolId,
  required String expiry,
  required int timeFrom,
  required int timeTo,
  required double atm,
  int eachSide = 5,
}) async {
  final raw = await fetchOIAnalysis(
    underlyingSymbolId: underlyingSymbolId,
    expiry: expiry,
    timeFrom: timeFrom,
    timeTo: timeTo,
  );
  if (raw == null) return OIAnalysisSnapshot.empty;
  final decoded = jsonDecode(raw) as Map<String, dynamic>;

  Map<double, OIAnalysisPoint> parseSide(String key) {
    final side = decoded[key] as Map<String, dynamic>? ?? {};
    final parsed = side.map(
      (strike, v) => MapEntry(
        double.parse(strike),
        OIAnalysisPoint.fromJson(v as Map<String, dynamic>),
      ),
    );

    return _trimAroundAtm(parsed, atm, eachSide);
  }

  return OIAnalysisSnapshot(
    calls: parseSide('calls'),
    puts: parseSide('puts'),
  );
}