getDailyPriceList method

  1. @override
Future<List<double>> getDailyPriceList(
  1. String ticker,
  2. int days
)
override

Implementation

@override
Future<List<double>> getDailyPriceList(String ticker, int days) async {
  // RSI 계산을 위해 'days'보다 많은 데이터를 가져옵니다. 일반적으로 2배수 정도면 충분합니다.
  // Yahoo Finance API는 range 파라미터를 더 선호하는 경향이 있습니다.
  final uri = Uri.parse(
      'https://query1.finance.yahoo.com/v8/finance/chart/$ticker?range=2mo&interval=1d');
  final response = await client.get(
    uri,
    headers: {'Content-Type': 'application/json'},
  );

  if (response.statusCode == 200) {
    final jsonBody = json.decode(response.body);
    // API 응답 구조에 따라 null 체크를 강화합니다.
    final result = jsonBody['chart']?['result']?[0];
    if (result != null) {
      final List<dynamic>? closePrices = result['indicators']?['quote']?[0]?['close'];
      if (closePrices != null) {
        // null 값을 필터링하고 double로 변환합니다.
        return closePrices.whereType<double>().toList();
      }
    }
    // 데이터가 없는 경우 빈 리스트를 반환하여 오류 대신 처리합니다.
    return [];
  } else {
    throw ServerException();
  }
}