Example #1
0
def get_data(symbol, start, end, adjust_price=True, interval='d'):
    """
    Returns DataFrame/Panel of historical stock prices from symbols, over date
    range, start to end. 
    
    Parameters
        ----------
        symbols : string, array-like object (list, tuple, Series), or DataFrame
            Single stock symbol (ticker), array-like object of symbols or
            DataFrame with index containing stock symbols.
        start : string, (defaults to '1/1/2010')
            Starting date, timestamp. Parses many different kind of date
            representations (e.g., 'JAN-01-2010', '1/1/10', 'Jan, 1, 1980')
        end : string, (defaults to today)
            Ending date, timestamp. Same format as starting date.
    
        adjust_price : bool, default False
            If True, adjusts all prices in hist_data ('Open', 'High', 'Low',
            'Close') based on 'Close' price. Adds 'Adj_Ratio' column and drops
            'Adj Close'.

        interval : string, default 'd'
            Time interval code, valid values are 'd' for daily, 'w' for weekly,
            'm' for monthly and 'v' for dividend.
    
    """

    data = YahooDailyReader(symbols=symbol,
                            start=start,
                            end=end,
                            adjust_price=adjust_price,
                            interval=interval).read()
    del data['Adj_Ratio']
    data['Return'] = (data['Close'] / data['Close'].shift(1)) - 1

    return data
Example #2
0
def DataReader(name,
               data_source=None,
               start=None,
               end=None,
               retry_count=3,
               pause=0.001,
               session=None):
    """
    Imports data from a number of online sources.

    Currently supports Yahoo! Finance, Google Finance, St. Louis FED (FRED),
    Kenneth French's data library, and the SEC's EDGAR Index.

    Parameters
    ----------
    name : str or list of strs
        the name of the dataset. Some data sources (yahoo, google, fred) will
        accept a list of names.
    data_source: {str, None}
        the data source ("yahoo", "yahoo-actions", "yahoo-dividends",
        "google", "fred", "ff", or "edgar-index")
    start : {datetime, None}
        left boundary for range (defaults to 1/1/2010)
    end : {datetime, None}
        right boundary for range (defaults to today)
    retry_count : {int, 3}
        Number of times to retry query request.
    pause : {numeric, 0.001}
        Time, in seconds, to pause between consecutive queries of chunks. If
        single value given for symbol, represents the pause between retries.
    session : Session, default None
            requests.sessions.Session instance to be used

    Examples
    ----------

    # Data from Yahoo! Finance
    gs = DataReader("GS", "yahoo")

    # Corporate Actions (Dividend and Split Data) with ex-dates from Yahoo! Finance
    gs = DataReader("GS", "yahoo-actions")

    # Data from Google Finance
    aapl = DataReader("AAPL", "google")

    # Data from FRED
    vix = DataReader("VIXCLS", "fred")

    # Data from Fama/French
    ff = DataReader("F-F_Research_Data_Factors", "famafrench")
    ff = DataReader("F-F_Research_Data_Factors_weekly", "famafrench")
    ff = DataReader("6_Portfolios_2x3", "famafrench")
    ff = DataReader("F-F_ST_Reversal_Factor", "famafrench")

    # Data from EDGAR index
    ed = DataReader("full", "edgar-index")
    ed2 = DataReader("daily", "edgar-index")
    """
    if data_source == "yahoo":
        return YahooDailyReader(symbols=name,
                                start=start,
                                end=end,
                                adjust_price=False,
                                chunksize=25,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()

    elif data_source == "yahoo-actions":
        return YahooActionReader(symbols=name,
                                 start=start,
                                 end=end,
                                 retry_count=retry_count,
                                 pause=pause,
                                 session=session).read()
    elif data_source == "yahoo-dividends":
        return YahooDailyReader(symbols=name,
                                start=start,
                                end=end,
                                adjust_price=False,
                                chunksize=25,
                                retry_count=retry_count,
                                pause=pause,
                                session=session,
                                interval='v').read()

    elif data_source == "google":
        return GoogleDailyReader(symbols=name,
                                 start=start,
                                 end=end,
                                 chunksize=25,
                                 retry_count=retry_count,
                                 pause=pause,
                                 session=session).read()

    elif data_source == "fred":
        return FredReader(symbols=name,
                          start=start,
                          end=end,
                          retry_count=retry_count,
                          pause=pause,
                          session=session).read()

    elif data_source == "famafrench":
        return FamaFrenchReader(symbols=name,
                                start=start,
                                end=end,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()

    elif data_source == "oecd":
        return OECDReader(symbols=name,
                          start=start,
                          end=end,
                          retry_count=retry_count,
                          pause=pause,
                          session=session).read()
    elif data_source == "eurostat":
        return EurostatReader(symbols=name,
                              start=start,
                              end=end,
                              retry_count=retry_count,
                              pause=pause,
                              session=session).read()
    elif data_source == "edgar-index":
        return EdgarIndexReader(symbols=name,
                                start=start,
                                end=end,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()
    elif data_source == "oanda":
        return get_oanda_currency_historical_rates(start,
                                                   end,
                                                   quote_currency="USD",
                                                   base_currency=name,
                                                   reversed=True,
                                                   session=session)
    else:
        msg = "data_source=%r is not implemented" % data_source
        raise NotImplementedError(msg)
Example #3
0
def get_data_yahoo(*args, **kwargs):
    return YahooDailyReader(*args, **kwargs).read()
Example #4
0
def DataReader(
    name,
    data_source=None,
    start=None,
    end=None,
    retry_count=3,
    pause=0.1,
    session=None,
    api_key=None,
):
    """
    Imports data from a number of online sources.

    Currently supports Google Finance, St. Louis FED (FRED),
    and Kenneth French's data library, among others.

    Parameters
    ----------
    name : str or list of strs
        the name of the dataset. Some data sources (IEX, fred) will
        accept a list of names.
    data_source: {str, None}
        the data source ("iex", "fred", "ff")
    start : {datetime, None}
        left boundary for range (defaults to 1/1/2010)
    end : {datetime, None}
        right boundary for range (defaults to today)
    retry_count : {int, 3}
        Number of times to retry query request.
    pause : {numeric, 0.001}
        Time, in seconds, to pause between consecutive queries of chunks. If
        single value given for symbol, represents the pause between retries.
    session : Session, default None
        requests.sessions.Session instance to be used
    api_key : (str, None)
        Optional parameter to specify an API key for certain data sources.

    Examples
    ----------
    # Data from Google Finance
    aapl = DataReader("AAPL", "iex")

    # Price and volume data from IEX
    tops = DataReader(["GS", "AAPL"], "iex-tops")
    # Top of book executions from IEX
    gs = DataReader("GS", "iex-last")
    # Real-time depth of book data from IEX
    gs = DataReader("GS", "iex-book")

    # Data from FRED
    vix = DataReader("VIXCLS", "fred")

    # Data from Fama/French
    ff = DataReader("F-F_Research_Data_Factors", "famafrench")
    ff = DataReader("F-F_Research_Data_Factors_weekly", "famafrench")
    ff = DataReader("6_Portfolios_2x3", "famafrench")
    ff = DataReader("F-F_ST_Reversal_Factor", "famafrench")
    """
    expected_source = [
        "yahoo",
        "iex",
        "iex-tops",
        "iex-last",
        "iex-last",
        "bankofcanada",
        "stooq",
        "iex-book",
        "enigma",
        "fred",
        "famafrench",
        "oecd",
        "eurostat",
        "nasdaq",
        "quandl",
        "moex",
        "robinhood",
        "tiingo",
        "yahoo-actions",
        "yahoo-dividends",
        "av-forex",
        "av-daily",
        "av-daily-adjusted",
        "av-weekly",
        "av-weekly-adjusted",
        "av-monthly",
        "av-monthly-adjusted",
        "av-intraday",
        "econdb",
    ]

    if data_source not in expected_source:
        msg = "data_source=%r is not implemented" % data_source
        raise NotImplementedError(msg)

    if data_source == "yahoo":
        return YahooDailyReader(
            symbols=name,
            start=start,
            end=end,
            adjust_price=False,
            chunksize=25,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "iex":
        return IEXDailyReader(
            symbols=name,
            start=start,
            end=end,
            chunksize=25,
            api_key=api_key,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "iex-tops":
        return IEXTops(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "iex-last":
        return IEXLasts(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "bankofcanada":
        return BankOfCanadaReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()
    elif data_source == "stooq":
        return StooqDailyReader(
            symbols=name,
            chunksize=25,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "iex-book":
        return IEXDeep(
            symbols=name,
            service="book",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "enigma":
        return EnigmaReader(dataset_id=name, api_key=api_key).read()

    elif data_source == "fred":
        return FredReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "famafrench":
        return FamaFrenchReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "oecd":
        return OECDReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()
    elif data_source == "eurostat":
        return EurostatReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()
    elif data_source == "nasdaq":
        if name != "symbols":
            raise ValueError("Only the string 'symbols' is supported for "
                             "Nasdaq, not %r" % (name, ))
        return get_nasdaq_symbols(retry_count=retry_count, pause=pause)

    elif data_source == "quandl":
        return QuandlReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()
    elif data_source == "moex":
        return MoexReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()
    elif data_source == "robinhood":
        return RobinhoodHistoricalReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()
    elif data_source == "tiingo":
        return TiingoDailyReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "yahoo-actions":
        return YahooActionReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    elif data_source == "yahoo-dividends":
        return YahooDivReader(
            symbols=name,
            start=start,
            end=end,
            adjust_price=False,
            chunksize=25,
            retry_count=retry_count,
            pause=pause,
            session=session,
            interval="d",
        ).read()

    elif data_source == "av-forex":
        return AVForexReader(
            symbols=name,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-daily":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_DAILY",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-daily-adjusted":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_DAILY_ADJUSTED",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-weekly":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_WEEKLY",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-weekly-adjusted":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_WEEKLY_ADJUSTED",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-monthly":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_MONTHLY",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-monthly-adjusted":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_MONTHLY_ADJUSTED",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "av-intraday":
        return AVTimeSeriesReader(
            symbols=name,
            function="TIME_SERIES_INTRADAY",
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
            api_key=api_key,
        ).read()

    elif data_source == "econdb":
        return EcondbReader(
            symbols=name,
            start=start,
            end=end,
            retry_count=retry_count,
            pause=pause,
            session=session,
        ).read()

    else:
        msg = "data_source=%r is not implemented" % data_source
        raise NotImplementedError(msg)
Example #5
0
def DataReader(name,
               data_source=None,
               start=None,
               end=None,
               retry_count=3,
               pause=0.001,
               session=None,
               access_key=None):
    """
    Imports data from a number of online sources.

    Currently supports Google Finance, St. Louis FED (FRED),
    and Kenneth French's data library, among others.

    Parameters
    ----------
    name : str or list of strs
        the name of the dataset. Some data sources (google, fred) will
        accept a list of names.
    data_source: {str, None}
        the data source ("google", "fred", "ff")
    start : {datetime, None}
        left boundary for range (defaults to 1/1/2010)
    end : {datetime, None}
        right boundary for range (defaults to today)
    retry_count : {int, 3}
        Number of times to retry query request.
    pause : {numeric, 0.001}
        Time, in seconds, to pause between consecutive queries of chunks. If
        single value given for symbol, represents the pause between retries.
    session : Session, default None
        requests.sessions.Session instance to be used
    access_key : (str, None)
        Optional parameter to specify an API key for certain data sources.

    Examples
    ----------
    # Data from Google Finance
    aapl = DataReader("AAPL", "google")

    # Price and volume data from IEX
    tops = DataReader(["GS", "AAPL"], "iex-tops")
    # Top of book executions from IEX
    gs = DataReader("GS", "iex-last")
    # Real-time depth of book data from IEX
    gs = DataReader("GS", "iex-book")

    # Data from FRED
    vix = DataReader("VIXCLS", "fred")

    # Data from Fama/French
    ff = DataReader("F-F_Research_Data_Factors", "famafrench")
    ff = DataReader("F-F_Research_Data_Factors_weekly", "famafrench")
    ff = DataReader("6_Portfolios_2x3", "famafrench")
    ff = DataReader("F-F_ST_Reversal_Factor", "famafrench")
    """
    if data_source == "yahoo":
        raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Daily'))
        return YahooDailyReader(symbols=name,
                                start=start,
                                end=end,
                                adjust_price=False,
                                chunksize=25,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()

    elif data_source == "yahoo-actions":
        raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Actions'))
        return YahooActionReader(symbols=name,
                                 start=start,
                                 end=end,
                                 retry_count=retry_count,
                                 pause=pause,
                                 session=session).read()

    elif data_source == "yahoo-dividends":
        comp = 'Yahoo Dividends'
        raise ImmediateDeprecationError(DEP_ERROR_MSG.format(comp))
        return YahooDivReader(symbols=name,
                              start=start,
                              end=end,
                              adjust_price=False,
                              chunksize=25,
                              retry_count=retry_count,
                              pause=pause,
                              session=session,
                              interval='d').read()

    elif data_source == "google":
        return GoogleDailyReader(symbols=name,
                                 start=start,
                                 end=end,
                                 chunksize=25,
                                 retry_count=retry_count,
                                 pause=pause,
                                 session=session).read()

    elif data_source == "iex":
        return IEXDailyReader(symbols=name,
                              start=start,
                              end=end,
                              chunksize=25,
                              retry_count=retry_count,
                              pause=pause,
                              session=session).read()

    elif data_source == "iex-tops":
        return IEXTops(symbols=name,
                       start=start,
                       end=end,
                       retry_count=retry_count,
                       pause=pause,
                       session=session).read()

    elif data_source == "iex-last":
        return IEXLasts(symbols=name,
                        start=start,
                        end=end,
                        retry_count=retry_count,
                        pause=pause,
                        session=session).read()

    elif data_source == "bankofcanada":
        return BankOfCanadaReader(symbols=name,
                                  start=start,
                                  end=end,
                                  retry_count=retry_count,
                                  pause=pause,
                                  session=session).read()
    elif data_source == "stooq":
        return StooqDailyReader(symbols=name,
                                chunksize=25,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()

    elif data_source == "iex-book":
        return IEXDeep(symbols=name,
                       service="book",
                       start=start,
                       end=end,
                       retry_count=retry_count,
                       pause=pause,
                       session=session).read()

    elif data_source == "enigma":
        return EnigmaReader(dataset_id=name, api_key=access_key).read()

    elif data_source == "fred":
        return FredReader(symbols=name,
                          start=start,
                          end=end,
                          retry_count=retry_count,
                          pause=pause,
                          session=session).read()

    elif data_source == "famafrench":
        return FamaFrenchReader(symbols=name,
                                start=start,
                                end=end,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()

    elif data_source == "oecd":
        return OECDReader(symbols=name,
                          start=start,
                          end=end,
                          retry_count=retry_count,
                          pause=pause,
                          session=session).read()
    elif data_source == "eurostat":
        return EurostatReader(symbols=name,
                              start=start,
                              end=end,
                              retry_count=retry_count,
                              pause=pause,
                              session=session).read()
    elif data_source == "edgar-index":
        raise ImmediateDeprecationError(DEP_ERROR_MSG.format('EDGAR'))
        return EdgarIndexReader(symbols=name,
                                start=start,
                                end=end,
                                retry_count=retry_count,
                                pause=pause,
                                session=session).read()
    elif data_source == 'nasdaq':
        if name != 'symbols':
            raise ValueError("Only the string 'symbols' is supported for "
                             "Nasdaq, not %r" % (name, ))
        return get_nasdaq_symbols(retry_count=retry_count, pause=pause)

    elif data_source == "quandl":
        return QuandlReader(symbols=name,
                            start=start,
                            end=end,
                            retry_count=retry_count,
                            pause=pause,
                            session=session).read()
    elif data_source == "moex":
        return MoexReader(symbols=name,
                          start=start,
                          end=end,
                          retry_count=retry_count,
                          pause=pause,
                          session=session).read()
    elif data_source == "morningstar":
        return MorningstarDailyReader(symbols=name,
                                      start=start,
                                      end=end,
                                      retry_count=retry_count,
                                      pause=pause,
                                      session=session,
                                      interval="d").read()
    elif data_source == 'robinhood':
        return RobinhoodHistoricalReader(symbols=name,
                                         start=start,
                                         end=end,
                                         retry_count=retry_count,
                                         pause=pause,
                                         session=session).read()
    elif data_source == 'tiingo':
        return TiingoDailyReader(symbols=name,
                                 start=start,
                                 end=end,
                                 retry_count=retry_count,
                                 pause=pause,
                                 session=session,
                                 api_key=access_key).read()
    else:
        msg = "data_source=%r is not implemented" % data_source
        raise NotImplementedError(msg)
Example #6
0
def get_data_yahoo(*args, **kwargs):
    raise ImmediateDeprecationError(DEP_ERROR_MSG.format('Yahoo Actions'))
    return YahooDailyReader(*args, **kwargs).read()
Example #7
0
def download_hist_prices(tickers):

    date_to = get_end_date()

    conn = pql.connect(host=DB_HOST,
                       database=DB_NAME,
                       user=DB_USER,
                       password='******')
    cur = conn.cursor()

    for t in tickers:

        # find the latest date for which a price exists
        cur.execute(f"SELECT max(recorded_at) FROM prices WHERE ticker='{t}'")

        # fetch functions always return tuples, hence the index [0]
        date_from = cur.fetchone()[0]

        if date_from is None:
            date_from = date(2010, 1, 1)
        else:
            date_from = date_from + datetime.timedelta(days=1)

        if (date_from <= date_to):
            try:
                reader = YahooDailyReader(t, date_from, date_to)
                new_prices = reader.read()

            except RemoteDataError:
                print(f"{t}: remote error")

            except KeyError:
                # There is a bug in pandas-datareader that doesn't check if the returned dataframe is empty
                # This is a workaround
                print(f'{t}: no new prices found')

            else:
                new_prices.rename(columns={
                    'High': 'high',
                    'Low': 'low',
                    'Open': 'open',
                    'Close': 'close',
                    'Volume': 'volume',
                    'Adj Close': 'adj_close'
                },
                                  inplace=True)
                new_prices.index.rename('recorded_at', inplace=True)
                new_prices = new_prices.astype({'volume': 'int32'})
                new_prices['ticker'] = t

                new_prices[[
                    'ticker', 'high', 'low', 'open', 'close', 'adj_close',
                    'volume'
                ]].to_csv(TEMP_FILE, header=False, float_format='%g')

                with open(TEMP_FILE) as temp_csv:
                    cur.copy_from(temp_csv, 'prices', sep=',')

                conn.commit()
                os.remove(TEMP_FILE)

                print(
                    f'{t}: new prices added for period [{date_from}, {date_to}]'
                )
        else:
            print(f'{t}: no new prices found')