def get_dataframe(self, tickers, startDate=None, endDate=None, metric_name=None, frequency='daily', afterHours='false'): """ Return a pandas.DataFrame of historical prices for one or more ticker symbols. By default, return latest EOD Composite Price for a list of stock tickers. On average, each feed contains 3 data sources. Supported tickers + Available Day Ranges are here: https://apimedia.tiingo.com/docs/tiingo/daily/supported_tickers.zip or from the TiingoClient.list_tickers() method. Args: tickers (string/list): One or more unique identifiers for a stock ticker. startDate (string): Start of ticker range in YYYY-MM-DD format. endDate (string): End of ticker range in YYYY-MM-DD format. metric_name (string): Optional parameter specifying metric to be returned for each ticker. In the event of a single ticker, this is optional and if not specified all of the available data will be returned. In the event of a list of tickers, this parameter is required. frequency (string): Resample frequency (defaults to daily). afterHours (string): If set to true, includes pre and post market data if available. """ valid_columns = { 'open', 'high', 'low', 'close', 'volume', 'adjOpen', 'adjHigh', 'adjLow', 'adjClose', 'adjVolume', 'divCash', 'splitFactor' } if metric_name is not None and metric_name not in valid_columns: raise APIColumnNameError('Valid data items are: ' + str(valid_columns)) if metric_name is None and isinstance(tickers, list): raise MissingRequiredArgumentError( """When tickers is provided as a list, metric_name is a required argument. Please provide a metric_name, or call this method with one ticker at a time.""" ) params = { 'format': 'json', 'resampleFreq': frequency, 'afterHours': afterHours } if startDate: params['startDate'] = startDate if endDate: params['endDate'] = endDate if pandas_is_installed: if type(tickers) is str: stock = tickers url = self._get_url(stock, frequency) response = self._request('GET', url, params=params) df = pd.DataFrame(response.json()) if metric_name is not None: prices = df[metric_name] prices.index = df['date'] else: prices = df prices.index = df['date'] del (prices['date']) else: prices = pd.DataFrame() for stock in tickers: url = self._get_url(stock, frequency) response = self._request('GET', url, params=params) df = pd.DataFrame(response.json()) df.index = df['date'] df.rename(index=str, columns={metric_name: stock}, inplace=True) prices = pd.concat([prices, df[stock]], axis=1) prices.index = pd.to_datetime(prices.index) return prices else: error_message = ( "Pandas is not installed, but .get_ticker_price() was " "called with fmt=pandas. In order to install tiingo with " "pandas, reinstall with pandas as an optional dependency. \n" "Install tiingo with pandas dependency: \'pip install tiingo[pandas]\'\n" "Alternatively, just install pandas: pip install pandas.") raise InstallPandasException(error_message)
def get_dataframe( self, tickers, startDate=None, endDate=None, metric_name=None, frequency="daily", fmt="json", ): """Return a pandas.DataFrame of historical prices for one or more ticker symbols. By default, return latest EOD Composite Price for a list of stock tickers. On average, each feed contains 3 data sources. Supported tickers + Available Day Ranges are here: https://apimedia.tiingo.com/docs/tiingo/daily/supported_tickers.zip or from the TiingoClient.list_tickers() method. Args: tickers (string/list): One or more unique identifiers for a stock ticker. startDate (string): Start of ticker range in YYYY-MM-DD format. endDate (string): End of ticker range in YYYY-MM-DD format. metric_name (string): Optional parameter specifying metric to be returned for each ticker. In the event of a single ticker, this is optional and if not specified all of the available data will be returned. In the event of a list of tickers, this parameter is required. frequency (string): Resample frequency (defaults to daily). fmt (string): 'csv' or 'json' """ valid_columns = { "open", "high", "low", "close", "volume", "adjOpen", "adjHigh", "adjLow", "adjClose", "adjVolume", "divCash", "splitFactor", } if metric_name is not None and metric_name not in valid_columns: raise APIColumnNameError("Valid data items are: " + str(valid_columns)) if metric_name is None and isinstance(tickers, list): raise MissingRequiredArgumentError( """When tickers is provided as a list, metric_name is a required argument. Please provide a metric_name, or call this method with one ticker at a time.""" ) params = {"format": fmt, "resampleFreq": frequency} if startDate: params["startDate"] = startDate if endDate: params["endDate"] = endDate if pandas_is_installed: if type(tickers) is str: prices = self._request_pandas(ticker=tickers, params=params, metric_name=metric_name) else: prices = pd.DataFrame() for stock in tickers: ticker_series = self._request_pandas( ticker=stock, params=params, metric_name=metric_name) ticker_series = ticker_series.rename(stock) prices = pd.concat([prices, ticker_series], axis=1, sort=True) return prices else: error_message = ( "Pandas is not installed, but .get_ticker_price() was " "called with fmt=pandas. In order to install tiingo with " "pandas, reinstall with pandas as an optional dependency. \n" "Install tiingo with pandas dependency: 'pip install tiingo[pandas]'\n" "Alternatively, just install pandas: pip install pandas.") raise InstallPandasException(error_message)