Tiingo Python Client

repository·master·Indexed 43 days ago

https://github.com/hydrosquall/tiingo-python

A Python client library for the Tiingo financial data platform. It provides programmatic access to stock market prices, ticker metadata, curated news, and fundamentals via RESTful and Real-Time Data APIs. Features include a TiingoWebsocketClient for real-time feeds, built-in support for returning data as Pandas DataFrames, and a TiingoDailyReader for historical stock data analysis.

Tokens
5.5K
Snippets
20
Records
22
Agent score
53%

What's inside Tiingo

  1. Overview of Tiingo Python

    master

    Tiingo Python is a client library for the Tiingo financial data platform. It provides access to Tiingo's RESTful and Real-Time Data API, which includes support for:

    • Stock Market Ticker Closing Prices + Metadata: Data includes full distribution details and is validated using a proprietary EOD Price Engine.
    • Curated News: Access to top financial news sources and curated blogs, with stories tagged by Tiingo's algorithms.
  2. Verify changes with linting and tests

    master

    Before submitting a pull request, ensure your code adheres to project standards by running flake8 for linting and tox for multi-version Python testing. You can also run the standard test suite via setup.py or py.test.

    Note: You must have a valid TIINGO_API_KEY exported in your environment for the tests to make valid API calls.

    $ flake8 tiingo tests
    $ python setup.py test
    $ py.test
    $ tox
  3. Manage test fixtures and API keys

    master

    To run tests, you need a valid TIINGO_API_KEY exported as an environment variable. If you regenerate fixtures by removing them from tests/fixtures, the new fixtures may contain your embedded API key. Use the provided tool to scrub the API key from the newly created test fixtures.

    # Set the API key for testing
    $ export TIINGO_API_KEY='...insert api key here...'
    
    # Regenerate a fixture
    $ rm tests/fixtures/NAME.yaml
    $ py.test
    
    # Remove the API key from the new test fixtures
    $ ./tools/api_key_tool.py
  4. Install Tiingo Python from source

    master

    You can install Tiingo Python from source by cloning the repository or downloading the tarball, then running the setup script.

    # Clone the repository
    $ git clone git://github.com/hydrosquall/tiingo-python
    
    # OR download the tarball
    $ curl -OL https://github.com/hydrosquall-python/tiingo/tarball/master
    
    # Install from the source directory
    $ python setup.py install
  5. Submit a pull request

    master

    When submitting a pull request, ensure the following requirements are met:

    • Include tests for your changes.
    • Update documentation if new functionality is added (include docstrings and update README.rst).
    • Ensure compatibility with Python 2.7, 3.6, and 3.7 (verified via GitHub Action checks).
  6. Set up tiingo for local development

    master

    To contribute to the tiingo repository, follow these steps to set up a local development environment using a virtualenv:

    1. Fork the repository on GitHub.
    2. Clone your fork locally.
    3. Create a virtualenv and install your local copy in development mode using python setup.py develop.
    4. Create a new branch for your changes.
    5. Verify changes using flake8, tox, and the test suite.
    6. Push your branch to GitHub and submit a pull request.
    $ git clone git@github.com:your_name_here/tiingo-python.git
    $ mkvirtualenv tiingo-python
    $ cd tiingo-python/
    $ python setup.py develop
    $ git checkout -b name-of-your-bugfix-or-feature
  7. Initialize TiingoClient

    master

    You can initialize the TiingoClient using an environment variable or a configuration dictionary.

    Using Environment Variables

    If you set TIINGO_API_KEY in your environment variables (e.g., in your .bash_profile), you can initialize the client without arguments:

    from tiingo import TiingoClient
    client = TiingoClient()

    Using a Configuration Dictionary

    If you prefer not to use environment variables, pass a dictionary containing your api_key. You can also enable session to reuse the same HTTP Session across API calls for better performance.

    from tiingo import TiingoClient
    
    config = {
        'api_key': "MY_SECRET_API_KEY",
        'session': True
    }
    client = TiingoClient(config)
  8. Initialize the TiingoClient

    master

    To use the Tiingo API, you must first initialize a TiingoClient. You can do this in two ways:

    1. Environment Variable: Set TIINGO_API_KEY in your environment (e.g., in your .bash_profile). If this is set, you can initialize the client without arguments.
    2. Configuration Dictionary: Pass a dictionary to TiingoClient containing your api_key.

    You can also include 'session': True in your configuration dictionary to reuse the same HTTP Session across API calls, which improves performance.

    from tiingo import TiingoClient
    
    # Option 1: Using environment variable TIINGO_API_KEY
    client = TiingoClient()
    
    # Option 2: Using a configuration dictionary
    config = {
        'api_key': "MY_SECRET_API_KEY",
        'session': True  # Enables HTTP session reuse for better performance
    }
    client = TiingoClient(config)
  9. Get stock market data with TiingoClient

    master

    Use TiingoClient to access various stock market endpoints. Most methods support parameters like frequency, startDate, endDate, and columns.

    Ticker Metadata and Prices

    • get_ticker_metadata(symbol): Returns metadata for a specific ticker.
    • get_ticker_price(symbol, ...): Returns price data. Supported frequencies include daily, weekly, monthly, annually, and intraday formats like 1min, 30Min, or 1Hour.

    News and Fundamentals

    • list_stock_tickers(): Lists available tickers and their metadata.
    • get_news(tickers, tags, sources, ...): Retrieves news articles.
    • get_fundamentals_definitions(ticker): Gets definitions for fundamentals fields.
    • get_fundamentals_daily(ticker, ...): Gets daily-updated fundamentals (e.g., marketCap).
    • get_fundamentals_statements(ticker, ..., asReported=True/False): Gets fundamentals from quarterly statements. Use asReported=True for SEC-reported data or False for corrected data.
    # Get Ticker Metadata
    ticker_metadata = client.get_ticker_metadata("GOOGL")
    
    # Get weekly prices
    ticker_price = client.get_ticker_price("GOOGL", frequency="weekly")
    
    # Get 1 min prices with specific columns
    ticker_price = client.get_ticker_price("GOOGL", frequency="1min", columns="open,close,volume")
    
    # Get historical daily prices as JSON
    historical_prices = client.get_ticker_price("GOOGL", fmt='json', startDate='2017-08-01', endDate='2017-08-31', frequency='daily')
    
    # Get news
    articles = client.get_news(tickers=['GOOGL', 'AAPL'], tags=['Laptops'], sources=['washingtonpost.com'], startDate='2017-01-01', endDate='2017-08-31')
  10. Use TiingoClient to fetch market data

    master

    The TiingoClient provides several methods to retrieve ticker metadata, prices, and news.

    Common methods include:

    • get_ticker_metadata(ticker): Returns metadata for a specific ticker.
    • get_ticker_price(ticker, ...): Retrieves price data. Supports parameters like frequency (e.g., 'weekly', 'daily'), fmt (e.g., 'json'), startDate, and endDate.
    • list_stock_tickers(): Returns a list of available tickers and their metadata.
    • get_news(tickers=[...], tags=[...], sources=[...], startDate='...', endDate='...'): Fetches news articles. Note that this requires a Pro account.
    import tiingo
    from tiingo import TiingoClient
    
    client = TiingoClient()
    
    # Get Ticker Metadata
    metadata = client.get_ticker_metadata("GOOGL")
    
    # Get latest prices (weekly)
    price_weekly = client.get_ticker_price("GOOGL", frequency="weekly")
    
    # Get historical prices (daily JSON)
    historical = client.get_ticker_price(
        "GOOGL",
        fmt='json',
        startDate='2017-08-01',
        endDate='2017-08-31',
        frequency='daily'
    )
    
    # List available tickers
    tickers = client.list_stock_tickers()
    
    # Get news (Requires Pro account)
    articles = client.get_news(
        tickers=['GOOGL', 'APPL'], 
        tags=['Bitcoin'], 
        sources=['washingtonpost.com'],
        startDate='2017-01-01',
        endDate='2017-08-31'
    )