getTickerData method

Future<List<YahooFinanceCandleData>> getTickerData(
  1. String symbol, {
  2. bool useCache = true,
  3. DateTime? startDate,
  4. bool adjust = false,
})

Gets the candles for a ticker

Implementation

Future<List<YahooFinanceCandleData>> getTickerData(
  String symbol, {
  bool useCache = true,
  DateTime? startDate,
  bool adjust = false,
}) async {
  if (symbol.contains('-')) {
    return getWeightedTickerData(
      symbol,
      useCache: useCache,
    );
  } else if (symbol.contains(',')) {
    final List<String> symbols = symbol.split(', ');
    return getTickerDataList(
      symbols,
      useCache: useCache,
    );
  }

  // Try to get data from cache
  List<dynamic>? pricesRaw;
  if (useCache) {
    pricesRaw = await YahooFinanceDAO().getAllDailyData(symbol);
  }

  List<YahooFinanceCandleData> prices = [];

  for (final priceRaw in pricesRaw ?? []) {
    final YahooFinanceCandleData price = YahooFinanceCandleData.fromJson(
      priceRaw as Map<String, dynamic>,
      adjust: adjust,
    );
    final bool isAfterStartDate =
        startDate == null || price.date.isAfter(startDate);

    if (isAfterStartDate) {
      prices.add(price);
    }
  }

  // If have no cached historical data
  if (prices.isEmpty) {
    prices = await getAllDataFromYahooFinance(
      symbol,
      useCache: useCache,
      startDate: startDate,
      adjust: adjust,
    );
  }

  // If there is offline data but is not up to date
  // try to get the remaining part
  else if (!StrategyTime.isUpToDate(prices, startDate)) {
    prices = await refreshData(
      prices,
      symbol,
      startDate: startDate,
      adjust: adjust,
    );
  }

  return prices;
}