项目作者: ycharts

项目描述 :
YCharts API的Python客户端
高级语言: Python
项目地址: git://github.com/ycharts/pycharts.git


pycharts

Python client for the YCharts Market Data API.

Requirements

  • Python ( 3.x )
  • Valid API key from YCharts.

Installation

Install from github using pip.

  1. pip install git+https://github.com/ycharts/pycharts.git
  2. # or
  3. pip install pycharts

Documentation and Support

Full API documentation can be found at https://www.ycharts.com/api/docs/.

For questions and information about API access, visit YCharts or email us (sales@ycharts.com) for more info.

Examples

Below are some examples to get started with using the Python Client.

Setup

  1. from pycharts import CompanyClient, IndicatorClient, MutualFundClient
  2. ycharts_api_key = 'sample-api-key'
  3. company_client = CompanyClient(ycharts_api_key)
  4. mutual_fund_client = MutualFundClient(ycharts_api_key)
  5. indicator_client = IndicatorClient(ycharts_api_key)

Discovery Queries

Gets a paginated list of companies, mutual funds or indicators.

Data Point Queries

Get a single date-value pair of data from one or more securities for one or more calculations. It takes an optional date parameter and will return the latest value before the requested date.

  • Company Data Point
  • Mutual Fund Data Point
  • Indicator Data Point
    ```python

    Queries the latest price values for AAPL and MSFT

    point_rsp = company_client.get_points([‘AAPL’, ‘MSFT’], [‘price’])

    Queries the latest net asset value for M:FCNTX

    point_rsp = mutual_fund_client.get_points(‘M:FCNTX’, [‘net_asset_value’])

    Queries the latest value for I:USICUI

    point_rsp = indicator_client.get_points(‘I:USICUI’)

Queries the price values for AAPL 21 days ago

previous_point_rsp = company_client.get_points(‘AAPL’, ‘price’, query_date=-21)

Queries the net asset value for M:FCNTXPL 21 days ago

twenty_one_days_ago = datetime.datetime.now() - datetime.timedelta(days=21)
previous_point_rsp = mutual_fund_client.get_points(‘M:FCNTX’, [‘net_asset_value’],
query_date=twenty_one_days_ago)

Queries the value for I:USICUI 21 days ago

previous_point_rsp = indicator_client.get_points(‘I:USICUI’, query_date=-31)

  1. ### Data Series Queries
  2. Get a series of date-value pairs of data from one or more securities for one or more calculations. It takes optional start_date and end_date parameters and will return all values between the requested dates.
  3. - [Company Data Series](http://ycharts.com/api/docs/companies/company_data_series.html)
  4. - [Mutual Fund Data Series](http://ycharts.com/api/docs/mutual_funds/mutual_fund_data_series.html)
  5. - [Indicator Data Series](http://ycharts.com/api/docs/indicators/indicator_data_series.html)
  6. ```python
  7. now = datetime.datetime.now()
  8. past = now - datetime.timedelta(days=100)
  9. series_rsp = company_client.get_series(['AAPL', 'MSFT'], ['price'],
  10. query_start_date=past , query_end_date=now)
  11. series_rsp = mutual_fund_client.get_series('M:FCNTX', ['net_asset_value'],
  12. query_start_date=past , query_end_date=now)
  13. series_rsp = indicator_client.get_series('I:USICUI',
  14. query_start_date=past , query_end_date=now)
  15. # example resampling request
  16. series_rsp = company_client.get_series(['AAPL', 'MSFT'], ['price'], query_start_date=past,
  17. query_end_date=now, resampling_frequency='daily', resampling_function='mean')

Info Queries

Get information about one or more securities.

  • Company Info
  • Mutual Fund Info
  • Indicator Info
    1. info_rsp = company_client.get_info(['AAPL', 'MSFT'], ['description'])
    2. info_rsp = mutual_fund_client.get_info('M:FCNTX', ['inception_date', 'broad_asset_class'])
    3. info_rsp = indicator_client.get_info('I:USICUI', ['next_release'])

Dividend Queries

Get dividends from one or more companies or mutual funds.

dividend_rsp = company_client.get_dividends([‘AAPL’, ‘MSFT’], ex_start_date=start_date,
dividend_type=’special’)
dividend_rsp = mutual_fund_client.get_dividends(‘M:FCNTX’, ex_start_date=start_date)

  1. ### Stock Split and Spinoff Queries
  2. Get a list of stock split/spinoff objects that within the date range specified by the optional start and end date parameters.
  3. - [Company Stock Split](http://ycharts.com/api/docs/companies/company_splits.html)
  4. - [Company Spinoffs](http://ycharts.com/api/docs/companies/company_spinoffs.html)
  5. ```python
  6. split_spinoff_end_date = datetime.datetime(2014, 1, 1)
  7. split_rsp = company_client.get_stock_splits(['AAPL'], split_end_date=split_spinoff_end_date)
  8. spinoff_rsp = company_client.get_stock_spinoffs(['AAPL'], spinoff_end_date=split_spinoff_end_date)

Exceptions

  1. # More exception classes in pycharts/exceptions.py
  2. from pycharts import exceptions
  3. try:
  4. bad_point_rsp = company_client.get_points(['AAPL'], ['price'], query_date=45)
  5. except exceptions.PyChartsRequestException as pycharts_error:
  6. print(pycharts_error.error_message)