YCharts API的Python客户端
Python client for the YCharts Market Data API.
Install from github using pip
.
pip install git+https://github.com/ycharts/pycharts.git
# or
pip install pycharts
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.
Below are some examples to get started with using the Python Client.
from pycharts import CompanyClient, IndicatorClient, MutualFundClient
ycharts_api_key = 'sample-api-key'
company_client = CompanyClient(ycharts_api_key)
mutual_fund_client = MutualFundClient(ycharts_api_key)
indicator_client = IndicatorClient(ycharts_api_key)
Gets a paginated list of companies, mutual funds or indicators.
companies = company_client.get_securities(exchange='NYSE')
mutual_funds = mutual_fund_client.get_securities(category='Technology')
indicators = indicator_client.get_securities(region='USA')
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.
previous_point_rsp = company_client.get_points(‘AAPL’, ‘price’, query_date=-21)
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)
previous_point_rsp = indicator_client.get_points(‘I:USICUI’, query_date=-31)
### Data Series Queries
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.
- [Company Data Series](http://ycharts.com/api/docs/companies/company_data_series.html)
- [Mutual Fund Data Series](http://ycharts.com/api/docs/mutual_funds/mutual_fund_data_series.html)
- [Indicator Data Series](http://ycharts.com/api/docs/indicators/indicator_data_series.html)
```python
now = datetime.datetime.now()
past = now - datetime.timedelta(days=100)
series_rsp = company_client.get_series(['AAPL', 'MSFT'], ['price'],
query_start_date=past , query_end_date=now)
series_rsp = mutual_fund_client.get_series('M:FCNTX', ['net_asset_value'],
query_start_date=past , query_end_date=now)
series_rsp = indicator_client.get_series('I:USICUI',
query_start_date=past , query_end_date=now)
# example resampling request
series_rsp = company_client.get_series(['AAPL', 'MSFT'], ['price'], query_start_date=past,
query_end_date=now, resampling_frequency='daily', resampling_function='mean')
Get information about one or more securities.
info_rsp = company_client.get_info(['AAPL', 'MSFT'], ['description'])
info_rsp = mutual_fund_client.get_info('M:FCNTX', ['inception_date', 'broad_asset_class'])
info_rsp = indicator_client.get_info('I:USICUI', ['next_release'])
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)
### Stock Split and Spinoff Queries
Get a list of stock split/spinoff objects that within the date range specified by the optional start and end date parameters.
- [Company Stock Split](http://ycharts.com/api/docs/companies/company_splits.html)
- [Company Spinoffs](http://ycharts.com/api/docs/companies/company_spinoffs.html)
```python
split_spinoff_end_date = datetime.datetime(2014, 1, 1)
split_rsp = company_client.get_stock_splits(['AAPL'], split_end_date=split_spinoff_end_date)
spinoff_rsp = company_client.get_stock_spinoffs(['AAPL'], spinoff_end_date=split_spinoff_end_date)
# More exception classes in pycharts/exceptions.py
from pycharts import exceptions
try:
bad_point_rsp = company_client.get_points(['AAPL'], ['price'], query_date=45)
except exceptions.PyChartsRequestException as pycharts_error:
print(pycharts_error.error_message)